Linux 蓝牙编程 C 和 socket.h



我是一个新的堆栈溢出用户!我正在写作是因为我在 KUbuntu 12.04 上使用 C 编程蓝牙时遇到了一些问题。

我正在尝试通过在此pdf中找到的程序将设备(乐高头脑风暴积木)连接到我的笔记本电脑(启动连接):NXT_Bluetooth_Handout

我安装了以下软件包: - Bluez-HCI转储,通信调试工具 - bluez,Linux蓝牙堆栈和相关工具 - libBluetooth3,BlueZ库 - libBluetooth-dev,用于链接到BlueZ库的开发文件

// Socket, used for Bluetooth socket
#include <sys/socket.h>
#include <sys/types.h>
// Bluetooth headers
#include <bluetooth/bluetooth.h>
#include <bluetooth/rfcomm.h>

主要是我遇到了两个问题:1)如果我只是尝试编译程序,我会得到:

$ gcc -lm -lbluetooth nxt_bt_connect.c -o nxt_bt_connect
/tmp/ccSLdkpn.o: In function `init_bluetooth':
nxt_bt_connect.c:(.text+0x60): undefined reference to `str2ba'
collect2: ld returned 1 exit status

其中 str2ba 是一个 BlueZ 函数,它应该可以工作......ba2str 是该库提供的另一个功能,它可以毫无问题地工作。

2) 如果我修改代码以使用 ba2str 而不是 str2ba 函数,我会收到与套接字相关的错误:

$ gcc -lm -lbluetooth 1.c -o nxt_bt_connect2
1.c: In function ‘main’:
1.c:101:23: error: called object ‘socket’ is not a function

问题是我没有套接字标头是 sys/,实际上如果我运行:

find /usr/include/ -name socket.h
 /usr/include/gtkmm-2.4/gtkmm/socket.h
 /usr/include/linux/socket.h
 /usr/include/asm-generic/socket.h
 /usr/include/giomm-2.4/giomm/socket.h
 /usr/include/x86_64-linux-gnu/sys/socket.h
 /usr/include/x86_64-linux-gnu/asm/socket.h
 /usr/include/x86_64-linux-gnu/bits/socket.h

有没有人知道如何解决这些问题?我希望以足够清晰的方式描述所有情况......我为我的英语不好道歉!

谢谢!!

对于第二个问题:你必须发布你编写的代码。否则我们无法弄清楚什么是不好的。

对于第一个问题:调用 GCC 时,您需要将库链接器标志作为最后一个参数:

gcc nxt_bt_connect.c -o nxt_bt_connect -lm -lbluetooth

假设 KUbuntu 使用 Debian 打包,<sys/socket.h> 又名 /usr/include/sys/socket.hlibc6-dev包一起提供。

最新更新