I'm trying to make simple program in c++ to read from SMBus, but can't even build program sample. I'm building with gcc(9.4.0) on Ubuntu 20.04.5 LTS.
I've installed
Code:
#include <cstdio>
extern "C" {
#include <linux/i2c.h>
#include <linux/i2c-dev.h>
#include <i2c/smbus.h>
}
#include <fcntl.h>
#include <sys/ioctl.h>
int main(int argc, char ** argv)
{
int file;
int adapter_nr = 6;
char filename[20];
snprintf(filename, 19, "/dev/i2c-%d", adapter_nr);
file = open(filename, O_RDWR);
if (file < 0) {
return(1);
}
int addr = 0x0b;
if (ioctl(file, I2C_SLAVE, addr) < 0) {
return(1);
return(1);
}
__u8 reg = 0x0d;
__s32 res;
char buf[10];
res = i2c_smbus_read_word_data(file, reg);
return 0;
}
Build command: gcc -li2c test_inc.cpp
but I'm getting following error:
test_inc.cpp:(.text+0xb9): undefined reference to `i2c_smbus_read_word_data'
collect2: error: ld returned 1 exit status
Linking i2c is passed as argument, and shared object exists libi2c
Can anyone help me to solve this riddle ?
I tried to update apts and reinstalled all packages I also added "-I path_to_so_file" to gcc
The order of the source file and the library is relevant. The following command should work:
gcc test_inc.cpp -li2c