简体   繁体   中英

c++ bluez eclipse linker config

I'm trying to add bluez library to my eclipse project on raspberry pi 3 (raspbian). This is what I've done:

  1. Bluez is already installed on raspberry pi 3 but i did not find the headers files so i followed this tutorial:

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

Now in directory /usr/local/include/bluez-5.37/lib/bluetooth i've got bluetooth.h, hci.h, ...

  1. On eclipse:

Project > Properties > C/C++ Build > Settings > GCC C++ Compiler > Includes

In Include paths (-l) i add: /usr/local/include/bluez-5.37/lib/bluetooth

  1. On eclipse

Project > Properties > C/C++ Build > Settings > GCC C++ Linker > Libraries

In libraries (-l) i add: bluetooth

In Library search path (-L) i add: /usr/local/lib

This is the code that i'm 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;
}

Headers are found but this is what console says:

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)

Bit newbie on this things... any help plz? thanks!

Try to install libbluetooth-dev , this is easier, especially for a beginner. That way you will only need the -lbluetooth flag. The only thing you will need to change is your header includes, you need to add bluetooth/ to your last three includes. The parent folder of the bluetooth folder with the headers is very likely already in your library search path, so you shouldn't add it to the search path, but instead access the header files like #include <bluetooth/bluetooth.h> .

I did the same steps you mentioned @Drazz. Notice that I used eclipse on windows and verified the code on my Raspberry Pi, for that reason i used cross compiling toolchain from SysProgs

The steps are:

1- I downloaded the latest bluez version 5.43 from bluez

2- I compiled the downloaded file on my pi following the steps mentioned at Adafruit

3- I copied the compiled folder "bluez-5.43" from my Pi to the Pc to use it with eclipse. I am developing using Sysgcc cross compiling toolchain Cross Compiling on windows using SyssGcc toolchain

4- I prepared the eclipse after installing the SyssGcc toolchain using the steps on Setting Up Cross-Compilation In Eclipse

5- I created a c++ project and copied the main code of the bluetooth mentioned in the first URL and went to:

Project > Properties > C/C++ Build > Settings > GCC C++ Compiler > Includes

In Include paths (-l) i add: "[path where you copied the compiled library from the pi]\\bluez-5.43\\lib"

In the linking section:

On eclipse Project > Properties > C/C++ Build > Settings > GCC C++ Linker > Libraries

In libraries (-l) i add: bluetooth-internal

In Library search path (-L) i add: "[path where you copied the compiled library from the pi]\\bluez-5.43\\lib.libs"

compile and run the final executable on the Pi.. Turn your phone Bluetooth and make it visible . You will find that the Pi can read your phone on the screen.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM