Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

RaspberryPi: undefined reference to `i2c_smbus_read*' functions in C userspace

I’m trying to compile a userspace C sourcefile which uses i2c_smbus_read_* and i2c_smbus_write_* functions in a Raspberry Pi 3B+ with kernel 5.10.63-v7+. It should be possible, according to this Linux kernel document. I already installed libi2c-dev.

If I use:

#include <linux/i2c.h> 
#include <linux/i2c-dev.h> 
#include <i2c/smbus.h>

and I run gcc myfile.c -o myfile, I get:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

/usr/bin/ld: /tmp/ccIWkqsK.o: in function `i2c_read':
myfile.c:(.text+0x84): undefined reference to `i2c_smbus_read_byte_data'
/usr/bin/ld: /tmp/ccIWkqsK.o: in function `i2c_write':
myfile.c:(.text+0x160): undefined reference to `i2c_smbus_write_block_data'
collect2: error: ld returned 1 exit status

Which could it be the problem?


Note that these functions are present in my /usr/include/i2c/smbus.h:

...
extern __s32 i2c_smbus_read_byte_data(int file, __u8 command);
extern __s32 i2c_smbus_write_byte_data(int file, __u8 command, __u8 value);
extern __s32 i2c_smbus_read_word_data(int file, __u8 command);
...

>Solution :

I agree you don’t need extern "C"in a C program. Check your headers and see if your header defines the function as inline or not. If it only declares it, you need to find out which library is the function defined in and link the library in your program.

Edit: I see you already updated the question. In this case you need to link the i2c library similar to what they do here. Just add a parameter to your gcc command: -li2c.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading