undefined reference to i2c_smbus_read_byte_data'

在尝试使用树莓派控制LED时遇到了编译错误,原因是缺少了必要的库。通过安装i2c-tools、libi2c-dev和python-smbus解决了问题。重新编译并运行程序后,成功控制了GPIO引脚。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在测试树莓派控制LED教程(http://www.codelast.com/?p=5155

GPIO测试编译时出现了这个问题

/usr/lib/gcc/arm-linux-gnueabi/4.6/../../../libwiringPi.so:undefined reference to `i2c_smbus_write_byte'
/usr/lib/gcc/arm-linux-gnueabi/4.6/../../../libwiringPi.so:undefined reference to `i2c_smbus_read_byte'
/usr/lib/gcc/arm-linux-gnueabi/4.6/../../../libwiringPi.so:undefined reference to `i2c_smbus_write_byte_data'
/usr/lib/gcc/arm-linux-gnueabi/4.6/../../../libwiringPi.so:undefined reference to `i2c_smbus_write_word_data'
/usr/lib/gcc/arm-linux-gnueabi/4.6/../../../libwiringPi.so:undefined reference to `i2c_smbus_read_word_data'
/usr/lib/gcc/arm-linux-gnueabi/4.6/../../../libwiringPi.so:undefined reference to `i2c_smbus_read_byte_data'
collect2: ld returned 1 exit status


官方论坛上说是少装了库的原因

安装这几个库: i2c-tools,libi2c-dev ,python-smbus

安装参考 sudo apt-get install libi2c-dev

然后重新

make clean

sudo make uninstall


make

sudo make install


重新编译下你的.c文件

gcc led.c -o led -lwiringPi

sudo ./led 4

虽然输入的是4

实际上是GPIO23不停输出高低电平

因为当初写库的时候重新定义了GPIO

(具体可参考https://projects.drogon.net/raspberry-pi/wiringpi/pins/

晒一张测试照片






(更多引脚信息可参考http://www.raspberrypi-spy.co.uk/2012/06/simple-guide-to-the-rpi-gpio-header-and-pins/)

实际上是

经测试果然编译通过


(附:WiringPiDownload and Install https://projects.drogon.net/raspberry-pi/wiringpi/download-and-install/





原文地址

http://www.raspberrypi.org/phpBB3/viewtopic.php?f=31&t=19680

Hi,
I am trying out the Ada I2c interface  http://sourceforge.net/projects/raspi-i2c-ada/ and have downloaded and built the library. However, when I try and build the example test program I get the following ld errors: -
CODE:  SELECT ALL
gprbuild test.gpr
gcc-4.6 t.o -o t
/home/pi/raspi-i2c-ada-code/lib-static//libi2c-ada.a(i2c_interface.o): In function `write_quick':
i2c_interface.c:(.text+0x24): undefined reference to `i2c_smbus_write_quick'
/home/pi/raspi-i2c-ada-code/lib-static//libi2c-ada.a(i2c_interface.o): In function `read_byte':
i2c_interface.c:(.text+0x4c): undefined reference to `i2c_smbus_read_byte'
/home/pi/raspi-i2c-ada-code/lib-static//libi2c-ada.a(i2c_interface.o): In function `write_byte':
i2c_interface.c:(.text+0x84): undefined reference to `i2c_smbus_write_byte'
/home/pi/raspi-i2c-ada-code/lib-static//libi2c-ada.a(i2c_interface.o): In function `read_byte_data':
i2c_interface.c:(.text+0xbc): undefined reference to `i2c_smbus_read_byte_data'
/home/pi/raspi-i2c-ada-code/lib-static//libi2c-ada.a(i2c_interface.o): In function `write_byte_data':
i2c_interface.c:(.text+0x104): undefined reference to `i2c_smbus_write_byte_data'
/home/pi/raspi-i2c-ada-code/lib-static//libi2c-ada.a(i2c_interface.o): In function `read_word_data':
i2c_interface.c:(.text+0x13c): undefined reference to `i2c_smbus_read_word_data'
/home/pi/raspi-i2c-ada-code/lib-static//libi2c-ada.a(i2c_interface.o): In function `write_word_data':
i2c_interface.c:(.text+0x184): undefined reference to `i2c_smbus_write_word_data'
/home/pi/raspi-i2c-ada-code/lib-static//libi2c-ada.a(i2c_interface.o): In function `process_call':
i2c_interface.c:(.text+0x1cc): undefined reference to `i2c_smbus_process_call'
/home/pi/raspi-i2c-ada-code/lib-static//libi2c-ada.a(i2c_interface.o): In function `read_block_data':
i2c_interface.c:(.text+0x20c): undefined reference to `i2c_smbus_read_block_data'
/home/pi/raspi-i2c-ada-code/lib-static//libi2c-ada.a(i2c_interface.o): In function `write_block_data':
i2c_interface.c:(.text+0x25c): undefined reference to `i2c_smbus_write_block_data'
/home/pi/raspi-i2c-ada-code/lib-static//libi2c-ada.a(i2c_interface.o): In function `read_i2c_block_data':
i2c_interface.c:(.text+0x2ac): undefined reference to `i2c_smbus_read_i2c_block_data'
/home/pi/raspi-i2c-ada-code/lib-static//libi2c-ada.a(i2c_interface.o): In function `write_i2c_block_data':
i2c_interface.c:(.text+0x2fc): undefined reference to `i2c_smbus_write_i2c_block_data'
/home/pi/raspi-i2c-ada-code/lib-static//libi2c-ada.a(i2c_interface.o): In function `block_process_call':
i2c_interface.c:(.text+0x34c): undefined reference to `i2c_smbus_block_process_call'
collect2: ld returned 1 exit status
gprbuild: link of t.adb failed

I have done some digging around and found that these undefined functions are defined in i2c-dev.h but the question is, where is and how should I link to their implementation/library  :?
Posts: 9
Joined: Tue Jun 05, 2012 8:35 pm
by  joan » Wed Oct 10, 2012 7:38 pm
Might be worth checking to see if libi2c-dev is present on your system.

sudo apt-get install libi2c-dev

may be all that you need.
Posts: 1341
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK
by  pygmy_giant » Wed Oct 10, 2012 7:51 pm
drivers schmivers

ada-fruit schmada fruit

libi2c-dev schmlibi2c-dev

I just did this:  http://www.njbsmith.net/miscellaneous/Ivansense_6050_test.c

hack it if you can
Posts: 1469
Joined: Sun Mar 04, 2012 12:49 am
by  richtoy » Wed Oct 10, 2012 7:56 pm
Thanks for the reply. I followed the instructions and have installed i2c-tools libi2c-dev and python-smbus. I can see the chip and successfully drive it from the command line with the following: -
CODE:  SELECT ALL
pi@raspberrypi ~ $ i2cset -y 0 0x20 0x00 0x00
pi@raspberrypi ~ $ i2cset -y 0 0x20 0x01 0x00
pi@raspberrypi ~ $ i2cset -y 0 0x20 0x12 0x01
pi@raspberrypi ~ $ i2cset -y 0 0x20 0x12 0x02
pi@raspberrypi ~ $ i2cset -y 0 0x20 0x12 0x03
pi@raspberrypi ~ $ i2cset -y 0 0x20 0x12 0x00


The only issue is linking because I don't appear to be able to find a library containing the missing symbols  :?
Last edited by  richtoy on Wed Oct 10, 2012 8:19 pm, edited 1 time in total.
Posts: 9
Joined: Tue Jun 05, 2012 8:35 pm
by  techpaul » Wed Oct 10, 2012 8:06 pm
richtoy wrote:Thanks for the reply. I followed the instructions and have installed i2c-tools libi2c-dev and python-smbus. I can see the chip and successfully drive it from the command line with the following: -
CODE:  SELECT ALL
pi@raspberrypi ~ $ i2cset -y 0 0x20 0x00 0x00
pi@raspberrypi ~ $ i2cset -y 0 0x20 0x01 0x00
pi@raspberrypi ~ $ i2cset -y 0 0x20 0x12 0x01
pi@raspberrypi ~ $ i2cset -y 0 0x20 0x12 0x02
pi@raspberrypi ~ $ i2cset -y 0 0x20 0x12 0x03
pi@raspberrypi ~ $ i2cset -y 0 0x20 0x12 0x00


The only issue is linking because I don't appear to be able to find a library containing the missing symbols  :?

Hmm I see you installed python-smbus which is for python, but you are building a gcc (C) porogramme, you have not by chance made reference in your code to python routines instead of C library functions?

Thus confusing the compiler.
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/
Posts: 764
Joined: Sat Jul 14, 2012 6:40 pm
Location: Reading, UK
by  sjw » Tue Nov 20, 2012 3:43 pm
I just came across this post, and helped out @richtoy already. But, for completeness, the problem is that the Linux I2C header <linux/i2c-dev.h> doesn't correspond to any library; instead, all the functions are declared inline. This is fine if you're writing C, but Ada doesn't understand C headers.

My solution was to create a C file which provides a callable interface to each declared function, so that it can be called from Ada using pragma Import.

The project's Makefile is supposed to compile this file and include it in the project library, but something went wrong on @richtoy's machine. AFAICR he retried it and it worked.

### 关于 SMBus 的代码实现 SMBus 是一种简单、两线式的串行总线协议,用于连接主板上的低速设备。Linux 内核提供了对 SMBus 协议的支持,并通过驱动程序实现了各种操作功能。 以下是基于 Linux 内核中的 `i2c_smbus` 接口的一个典型 SMBus 使用示例: #### 示例:读取 SMBus 设备的数据 下面是一个简单的 C 程序,演示如何使用 `i2c-dev.h` 和 `i2c/smbus.h` 来访问 SMBus 上的设备[^1]。 ```c #include <stdio.h> #include <stdlib.h> #include <fcntl.h> #include <unistd.h> #include <linux/i2c-dev.h> #include <sys/ioctl.h> #include <stdint.h> int main(int argc, char **argv) { int file; uint8_t address = 0x50; // 假设目标设备地址为 0x50 uint8_t command = 0x00; // 命令字节 uint8_t data; // 打开 I2C 设备文件 if ((file = open("/dev/i2c-1", O_RDWR)) < 0) { // 替换为实际使用的 I2C 总线编号 perror("Failed to open the bus"); exit(1); } // 设置要通信的目标设备地址 if (ioctl(file, I2C_SLAVE, address) < 0) { perror("Failed to acquire bus access and/or talk to slave."); exit(1); } // 发送命令并读取单个字节数据 if (read(file, &data, 1) != 1) { perror("Error reading from device"); close(file); exit(1); } printf("Data read from SMBus: 0x%02X\n", data); close(file); return 0; } ``` 此代码展示了如何打开 `/dev/i2c-X` 文件并与指定地址的 SMBus 设备交互。它还说明了如何设置目标设备地址以及执行基本的读写操作。 --- #### 解决未定义引用错误的方法 如果在编译过程中遇到类似于 “undefined reference to i2c_smbus_read_byte_data” 这样的链接器错误,则可能是缺少必要的库支持。解决方法如下: 1. 确保已安装 `libi2c-dev` 或类似的开发包。 2. 编译时显式链接 `-li2c` 库。例如: ```bash gcc -o smbus_example smbus_example.c -li2c ``` 如果没有找到该库,可以通过以下方式安装(适用于 Debian/Ubuntu): ```bash sudo apt-get install libi2c-dev ``` --- #### 记录内存 SPD 数据的应用场景 SPD(Serial Presence Detect)是一种存储模块的信息表,通常位于 DIMM 中并通过 SMBus 接口访问。为了获取这些信息,可以利用上述代码框架扩展其功能。例如,通过连续读取多个寄存器来提取完整的 SPD 表格内容[^3]。 --- #### 注意事项 - 在某些硬件平台上,可能需要额外配置权限才能访问 `/dev/i2c-*` 设备节点。 - 如果运行环境不支持直接调用 SMBus 函数,可考虑借助第三方工具如 `i2cdump` 或 `i2cget` 完成调试工作。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值