gpt4 book ai didi

c - C & socket.h 中的 Linux 蓝牙编程

转载 作者:IT王子 更新时间:2023-10-29 01:24:19 34 4
gpt4 key购买 nike

我是一个新的 stackoverflow 用户!我写这篇文章是因为我在我的 KUbuntu 12.04 上用 C 编程蓝牙时遇到了一些问题。

我正在尝试通过我在此 pdf 中找到的程序(启动连接)将设备 (LEGO Mindstorm Brick) 连接到我的笔记本电脑: NXT_Bluetooth_Handout

我安装了以下软件包: - bluez-hcidump,通信调试工具 - 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

关于c - C & socket.h 中的 Linux 蓝牙编程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10961592/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com