我试图在c++中制作简单的程序,以便从SMBus中读取,但甚至不能构建程序示例。我和gcc(9.4.0)一起在Ubuntu20.04.5LTS上建造。
我安装了
代码:
#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;
}构建命令:gcc -li2c test_inc.cpp
但是我发现了一个错误:
test_inc.cpp:(.text+0xb9): undefined reference to `i2c_smbus_read_word_data'
collect2: error: ld returned 1 exit status 将链接i2c作为参数传递,共享对象存在libi2c。
有人能帮我解开这个谜团吗?
我试图更新apts并重新安装所有包,我还在gcc中添加了"-I path_to_so_file“。
发布于 2022-11-17 13:03:16
源文件和库的顺序是相关的。以下命令应该可以工作:
gcc test_inc.cpp -li2chttps://stackoverflow.com/questions/74475799
复制相似问题