undefined reference to i2c_smbus_read_byte_data'

在测试树莓派控制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.

### 回答1: 函数i2c_smbus_read_byte_data是一个用于读取I2C从设备上指定寄存器的数据的 \u51fd\u6570i2c_smbus_read_byte_data\u662f\u4e00\u4e2a\u7528\u4e8e\u8bfb\u53d6I2C\u4ece\u8bbe\u5907\u4e0a\u6307\u5b9a\u5bc4\u5b58\u5668\u7684\u6570\u636e\u7684\u51fd\u6570\u3002 I2C\u662f\u4e00\u79cd\u901a\u4fe1\u534f\u8bae\uff0c\u7528\u4e8e\u8bbe\u5907\u4e4b\u95f4\u7684\u6570\u636e\u4ea4\u6362\u3002\u51fd\u6570i2c_smbus_read_byte_data\u7528\u4e8e\u4ece\u8bbe\u5907\u4e0a\u7684\u6307\u5b9a\u5bc4\u5b58\u5668\u8bfb\u53d6\u4e00\u4e2a\u5b57\u8282\u6570\u636e\uff0c\u5e76\u8fd4\u56de\u8be5\u6570\u636e\u3002 \u4f7f\u7528\u8be5\u51fd\u6570\u524d\uff0c\u9700\u8981\u5148\u786e\u5b9aI2C\u8bbe\u5907\u7684\u5730\u5740\u548c\u5b9a\u5740\uff0c\u6307\u5b9a\u8981\u8bfb\u53d6\u7684\u5bc4\u5b58\u5668\u7684\u5730\u5740\u3002\u7136\u540e\uff0c\u4f7f\u7528i2c_smbus_read_byte_data\u51fd\u6570\u53ef\u4ee5\u8bfb\u53d6\u6307\u5b9a\u5bc4\u5b58\u5668\u4e0a\u6307\u5b9a\u5730\u5740\u7684\u6570\u636e\uff0c\u5e76\u8fd4\u56de\u8be5\u6570\u636e\u3002 \u603b\u4e4b\u8005\uff0c\u51fd\u6570i2c_smbus_read_byte_data\u662f\u4e00\u4e2a\u5f88\u5bb9\u6613\u4f7f\u7528\u7684\u51fd\u6570\uff0c\u4e3aI2C\u4ea4\u6362\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7cbe\u5f69\u7684\u63a5\u53e3\u3002 ### 回答2i2c_smbus_read_byte_data是Linux内核中一种I2C设备驱动中可用的函数,提供了从I2C从设备读取单个字节数据的功能。其中,i2c表示I2C总线,smbus是System Management Bus的缩写,代表了一种I2C总线上的智能交互协议。 在使用i2c_smbus_read_byte_data函数时,需要传入3个参数。第一个参数为指向I2C设备结构体的指针,第二个参数是从设备的寄存器地址,第三个参数是用于存储读取数据的8位整数指针。 当调用i2c_smbus_read_byte_data函数后,内核会向I2C从设备发送一个读取数据的命令,并指定从设备中的哪个寄存器地址中读取数据。从设备接收到这个命令后,会将数据通过I2C总线发送给主设备,内核驱动程序会自动读取这个数据并将其作为一个整数类型返回给调用者。若发生错误,会返回负值。 当使用i2c_smbus_read_byte_data函数时,需要注意以下几点: 1. 需要对I2C从设备的寄存器地址非常熟悉,否则读取到的数据将可能不是正确的数据。 2. 内核会自动将标准I2C通信协议转换成SMBus交互协议,需要保证SMBus协议与I2C从设备的通信是兼容的。 3. 在调用函数前,需要通过i2c_slave_addr函数将主设备和从设备进行绑定。 ### 回答3: i2c_smbus_read_byte_data是Linux内核中的一个函数,它用于从I2C设备中读取一个字节的数据。 I2C是一种串行通信协议,可以连接多个设备,使用两个线路进行数据传输。I2C设备可以是传感器、控制器等各种电子设备,这些设备都有一个独特的地址。 i2c_smbus_read_byte_data函数的作用是从指定的I2C设备中读取一个字节的数据。这个函数需要传入三个参数,分别是指向I2C设备的指针、设备的地址、以及需要读取的寄存器的地址。 函数执行流程如下: 1. 首先,函数会向设备发送一个“寄存器地址”命令,并等待设备的应答。 2. 然后,函数会从设备中读取一个字节的数据,并返回该数据。 如果操作成功,i2c_smbus_read_byte_data函数会返回一个字节的数据,否则会返回一个错误码。 总之,i2c_smbus_read_byte_data函数是Linux内核中用于读取I2C设备数据的常用函数,它可以从指定的I2C设备中读取一个字节的数据,有助于实现对各种电子设备的控制和数据读取。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值