与libbluetooth.so链接

时间:2014-11-04 13:48:38

标签: bluetooth linker

在Ubuntu 14.04上,我正在尝试做一个蓝牙设备列表的小例子,但是在编译这个简约的演示http://people.csail.mit.edu/albert/bluez-intro/c404.html时,我面临一个关于与蓝牙共享库链接的简单问题:

$ sudo apt-get install libbluetooth-dev

$ gcc -lbluetooth simplescan.c -o simplescan
/tmp/ccuwRsB5.o: In function `main':
simplescan.c:(.text+0x79): undefined reference to `hci_get_route'
simplescan.c:(.text+0x8c): undefined reference to `hci_open_dev'
simplescan.c:(.text+0x132): undefined reference to `hci_inquiry'
simplescan.c:(.text+0x18f): undefined reference to `ba2str'
simplescan.c:(.text+0x1f0): undefined reference to `hci_read_remote_name'
collect2: error: ld returned 1 exit status

$ nm -D /usr/lib/x86_64-linux-gnu/libbluetooth.so.3.13.0 | grep hci_get_route
0000000000008f00 T hci_get_route

似乎找到蓝牙共享库并包含所需的功能,但链接阶段无法实现。

1 个答案:

答案 0 :(得分:5)

解决方案让我感到惊讶(对我来说):给gcc的参数顺序很重要。 “-lbluetooth”应放在“simplescan.c”之后:

$ gcc simplescan.c -lbluetooth -o simplescan # Success
$ gcc -lbluetooth simplescan.c -o simplescan # Failure
/tmp/ccWhZFXs.o: In function `main':
simplescan.c:(.text+0x79): undefined reference to `hci_get_route'
simplescan.c:(.text+0x8c): undefined reference to `hci_open_dev'
simplescan.c:(.text+0x132): undefined reference to `hci_inquiry'
simplescan.c:(.text+0x18f): undefined reference to `ba2str'
simplescan.c:(.text+0x1f0): undefined reference to `hci_read_remote_name'
collect2: error: ld returned 1 exit status