linuxbluetoothcmakebluez

How to include bluez latest version library using CMake in project


I want to include bluez latest version (5.44) to my C program project. I am using CMake for build purpose. I tried using pkg_check_modules as below

pkg_check_modules (BLUEZ REQUIRED bluez)
include_directories(${BLUEZ_INCLUDE_DIRS})
link_directories(${BLUEZ_LIBRARY_DIRS})

But It always finds the older version 5.37. How can I point this to take latest version ?

I already tried this as well but it throws the error as below.

pkg_check_modules (BLUEZ REQUIRED bluez=5.44)
include_directories(${BLUEZ_INCLUDE_DIRS})
link_directories(${BLUEZ_LIBRARY_DIRS})

Error getting :

Checking for module 'bluez=5.44'
--   
CMake Error at /usr/share/cmake-3.5/Modules/FindPkgConfig.cmake:367 (message):
  A required package was not found
Call Stack (most recent call first):
  /usr/share/cmake-3.5/Modules/FindPkgConfig.cmake:532 (_pkg_check_modules_internal)
  CMakeLists.txt:30 (pkg_check_modules)

I downloaded the latest bluez version and built and installed using sudo make install. I am able to find and use the bluetoothctl tool.


Solution

  • Firstly, you need to make sure that pkg-config can find your installed version of bluez. If it can't, then CMake won't be able to either.

    $ pkg-config --modversion bluez
    

    If that does not give you the version you expect, then you need to find the bluez.pc for the version that you want, and make sure its directory is at the beginning of PKG_CONFIG_PATH. Since you stated that you used sudo make install, the bluez.pc you want is most likely at /usr/local/lib/pkgconfig/bluez.pc. (You will need to look yourself to be sure.) If that's the case, then

    $ PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:${PKG_CONFIG_PATH} pkg-config --modversion bluez
    

    should return the the version your looking for. If so, do what's necessary to make that change permanent to your shell. Delete your CMake cache, and re-cmake your project.