交叉编译blueZ失败

发布于 2022-09-11 23:54:12 字数 1757 浏览 23 评论 0

首先我PC环境是Ubuntu16.04,自带了blueZ支持,我直接就可以用Bluetoothctl。
ARM板子是米尔IMX6UL,linux4.14,厂家给的BSP包也是支持blueZ的,我也已经测试在板子上用Bluetoothctl。
然后我在网上找到了这段代码,在用gcc编译后,可以在PC上正常运行了。代码的scan功能,PC上可以用,实测ok。
编译指令
gcc -o simplescan simplescan.c -lbluetooth


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/hci.h>
#include <bluetooth/hci_lib.h>

int main(int argc, char **argv)
{
    inquiry_info *ii = NULL;
    int max_rsp, num_rsp;
    int dev_id, sock, len, flags;
    int i;
    char addr[19] = { 0 };
    char name[248] = { 0 };

    dev_id = hci_get_route(NULL);
    sock = hci_open_dev( dev_id );
    if (dev_id < 0 || sock < 0) {
        perror("opening socket");
        exit(1);
    }

    len  = 8;
    max_rsp = 255;
    flags = IREQ_CACHE_FLUSH;
    ii = (inquiry_info*)malloc(max_rsp * sizeof(inquiry_info));

    num_rsp = hci_inquiry(dev_id, len, max_rsp, NULL, &ii, flags);
    if( num_rsp < 0 ) perror("hci_inquiry");

    for (i = 0; i < num_rsp; i++) {
        ba2str(&(ii+i)->bdaddr, addr);
        memset(name, 0, sizeof(name));
        if (hci_read_remote_name(sock, &(ii+i)->bdaddr, sizeof(name), 
            name, 0) < 0)
        strcpy(name, "[unknown]");
        printf("%s  %s\n", addr, name);
    }

    free( ii );
    close( sock );
    return 0;
}

然后我打算把这个程序放到ARM上运行,理论上来说修改gcc就可以了;
arm-linux-gnueabihf-gcc-o simplescan simplescan.c -lbluetooth.
之前的一些应用,比如一个串口收发应用,我就只改了GCC为ARMgcc就可以了。
但是这次碰到了asm/xxx.h NO such file。
看起来是框架的问题,我应该怎么弄呢?
我搜索了asm/xxx.h ,在PC上发现多个地方有这个h文件,应该如何选择呢?是否需要我ARM板的linux源代码或者什么其他的东西?
现在有点一头雾水,不知如何下手。

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

分开我的手 2022-09-18 23:54:12

Ubuntu自带的蓝牙库是用于X86系统环境的,即其原始的blueZ协议栈代码,使用gcc(X86)编译,这样编译得到的库只能用于X86环境下的编译使用。
所以如果直接用如下编译,编译是无法通过,会有各式的编译问题产生。
arm-linux-gnueabihf-gcc -o simplescan simplescan.c -lbluetooth //这样编译ARM版本是编不出来的

所以这里需要使用开发板厂家提供的BSP
BSP提供了用于yocto编译应用层代码的编译链,编译链中有用于ARM的blueZ蓝牙库和头文件,省去了非常繁琐的blueZ库编译问题。

按照如下方法即可

root@yjppc:/a_work# $CC -o simplescan simplescan.c --sysroot /opt/myir-imx-fb/4.1.15-2.0.1/sysroots/cortexa7hf-neon-poky-linux-gnueabi/ -lbluetooth
root@yjppc:/a_work# ls

apps material simplescan simplescan.c

期间遇到的各类问题:

1、首先根据mir的BSP,安装好适用于ARM的编译环境
这里的编译环境为了方便切换工具链,没有写死系统的环境变量,需要在使用前,source 一下,这样方便切换多种平台的开发环境。
source /opt/myir-imx-fb/4.1.15-2.0.1/environment-setup-cortexa7hf-neon-poky-linux-gnueabi

2、编译时,不要用如下的编译方式,这样会出现gnu/stubs-soft.h文件缺失
黄色的字体,表示其成功读取了对应的sysroot,即将BSP的环境当做系统环境来运行。
蓝色字体的编译GCC,不能直接用,这里不是很理解,可能sysroot中的环境配置有关系

root@yjp-pc:/workzone# arm-poky-linux-gnueabi-gcc -o simplescan simplescan.c --sysroot /opt/myir-imx-fb/4.1.15-2.0.1/sysroots/cortexa7hf-neon-poky-linux-gnueabi/
In file included from /opt/myir-imx-fb/4.1.15-2.0.1/sysroots/cortexa7hf-neon-poky-linux-gnueabi/usr/include/features.h:392:0,
from /opt/myir-imx-fb/4.1.15-2.0.1/sysroots/cortexa7hf-neon-poky-linux-gnueabi/usr/include/stdio.h:27,
from test.c:2:
/opt/myir-imx-fb/4.1.15-2.0.1/sysroots/cortexa7hf-neon-poky-linux-gnueabi/usr/include/gnu/stubs.h:7:29: fatal error: gnu/stubs-soft.h: No such file or directory

所以需要用到开头的编译指令
这里的$CC可能是sysroot环境变量中设置好的编译gcc
oot@yjppc:/a_work# $CC -o simplescan simplescan.c --sysroot /opt/myir-imx-fb/4.1.15-2.0.1/sysroots/cortexa7hf-neon-poky-linux-gnueabi/ -lbluetooth

3、确认ARM板载的库
动态链接库的特点即主程序和库中函数分离,所以要确定板载环境也有这个库。
通常来说,在编译文件系统时,yocto已经默认支持蓝牙,因为是验证功能板子,所以功能通常会全开。
目前确认ARM板子上是有库的。
确认蓝牙版本和板载库。
l2cap.o libbluetooth.so.2.11.2
start.sh libbluetooth.so
bnep.o libbluetooth.so.2
hci_usb.o
rfcomm.o

一身仙ぐ女味 2022-09-18 23:54:12

请把错误日志贴上来

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文