未定义对`i2c_smbus_read_block_dataint,unsigned char,unsigned char *'的引用

问题描述

我正在尝试从imu(mpu9250)读取数据块,但是使用 g++ mpu.cpp -o mpu我收到以下错误:

/tmp/cckh5V8w.o: In function 'imu::read_accel()': 
mpu_mine9250.cpp:(.text._ZN3imu10read_accelEv[_ZN3imu10read_accelEv]+0x94): undefined reference to 'i2c_smbus_read_block_data(int,unsigned char,unsigned char*)'
collect2: error: ld returned 1 exit status ```
int addr = 0x68;
int mpu_file;
char mpu_filename[250];
snprintf(mpu_filename,250,"/dev/i2c-0");
if (ioctl(mpu_file,I2C_SLAVE,addr) < 0){
     exit(1);
}
__u8 buf[14];
__u8 reg = 0x3B;
int ans= i2c_smbus_read_block_data(mpu_file,reg,buf);

要包含我尝试过的库:

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

还有:

extern "C" {
#include <linux/i2c.h>
#include <linux/i2c-dev.h>
#include <i2c/smbus.h>
}

我已经安装了libi2c-dev,libi2c0和i2c-tools软件包。

使用write(mpu_file,buf,2)read(mpu_file,1)时确实有效。

谢谢!

解决方法

解决方案是:

1。使用extern C: 我正在使用extern C,但也以常规方式包括它们,这引起了问题。

extern "C"
{
#include <linux/i2c.h>
#include <linux/i2c-dev.h>
#include <i2c/smbus.h>
}

2。构建时链接库:

g++ -o mpu mpu_mine9250.cpp -li2c

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...