【问题标题】:c++ bluez eclipse linker config时间:2019-04-01 标签:c++bluezeclipselinkerconfig
【发布时间】:2016-10-23 17:34:12
【问题描述】:

我正在尝试将 bluez 库添加到我在树莓派 3 (raspbian) 上的 eclipse 项目中。这就是我所做的:

  1. Bluez 已经安装在树莓派 3 上,但我没有找到头文件,所以我按照本教程进行操作:

https://learn.adafruit.com/install-bluez-on-the-raspberry-pi/installation

现在在目录 /usr/local/include/bluez-5.37/lib/bluetooth 我有 bluetooth.h, hci.h, ...

  1. 日食:

项目 > 属性 > C/C++ 构建 > 设置 > GCC C++ 编译器 > 包含

在包含路径 (-l) 中,我添加:/usr/local/include/bluez-5.37/lib/bluetooth

  1. 日食

项目 > 属性 > C/C++ 构建 > 设置 > GCC C++ 链接器 > 库

在库中 (-l) 我添加:蓝牙

在库搜索路径 (-L) 中添加:/usr/local/lib

这是我trying to build的代码

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <bluetooth.h>
#include <hci.h>
#include <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;
}

找到了标题,但控制台是这样说的:

02:25:09 **** Incremental Build of configuration Release for project ComputerVision ****
make all 
Building target: ComputerVision
Invoking: GCC C++ Linker
g++ -L/usr/local/lib -o "ComputerVision"  ./src/ComputerVision.o   -lopencv_core -lbluetooth -lopencv_imgproc -lopencv_highgui -lopencv_ml -lopencv_video -lopencv_features2d -lopencv_calib3d -lopencv_objdetect -lopencv_contrib -lopencv_legacy -lopencv_flann
/usr/bin/ld: cannot find -lbluetooth
makefile:45: recipe for target 'ComputerVision' failed
collect2: error: ld returned 1 exit status
make: *** [ComputerVision] Error 1

02:25:09 Build Finished (took 382ms)

这方面的新手......有什么帮助吗?谢谢!

【问题讨论】:

    标签: c++ eclipse bluetooth bluez


    【解决方案1】:

    我做了你提到@Drazz 的相同步骤。 请注意,我在 windows 上使用了 eclipse 并在我的 Raspberry Pi 上验证了代码,因此我使用了来自 SysProgs 的交叉编译工具链

    步骤如下:

    1- 我从bluez下载了最新的bluez 5.43版

    2- 我按照Adafruit 中提到的步骤在我的 pi 上编译了下载的文件

    3- 我将已编译的文件夹“bluez-5.43”从我的 Pi 复制到 PC,以便与 eclipse 一起使用。我正在使用 Sysgcc 交叉编译工具链开发Cross Compiling on windows using SyssGcc toolchain

    4- 我在安装 SyssGcc 工具链后使用Setting Up Cross-Compilation In Eclipse 上的步骤准备了 eclipse

    5-我创建了一个c++项目,复制了第一个网址中提到的蓝牙的主要代码,然后转到:

    项目 > 属性 > C/C++ 构建 > 设置 > GCC C++ 编译器 > 包含

    在包含路径 (-l) 中,我添加:“[从 pi 复制编译库的路径]\bluez-5.43\lib”

    在链接部分:

    在 Eclipse 项目 > 属性 > C/C++ 构建 > 设置 > GCC C++ 链接器 > 库

    在库中 (-l) 我添加:蓝牙内部

    在库搜索路径 (-L) 中,我添加:“[从 pi 复制编译库的路径]\bluez-5.43\lib.libs”

    在 Pi 上编译并运行最终的可执行文件。打开手机蓝牙并使其可见。您会发现 Pi 可以在屏幕上读取您的手机。

    【讨论】:

      【解决方案2】:

      尝试安装libbluetooth-dev,这样更容易,尤其是对于初学者。这样你就只需要-lbluetooth 标志。您唯一需要更改的是标题包含,您需要将bluetooth/ 添加到最后三个包含。带有标题的蓝牙文件夹的父文件夹很可能已经在您的库搜索路径中,因此您不应将其添加到搜索路径中,而应访问 #include &lt;bluetooth/bluetooth.h&gt; 之类的头文件。

      【讨论】:

      • 抱歉耽搁了。那很有用。谢谢
      猜你喜欢
      • 2020-05-07
      • 2011-01-31
      • 2013-04-11
      • 2015-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多