0

I am writing a bluez C program to read battery service. I am using CMake for building the code. My Cmake File is :

    # CMakeLists file for module-bluez project

cmake_minimum_required(VERSION 3.02)

project (bluez-module)

find_package(PkgConfig REQUIRED)

# Adding dbus library

pkg_check_modules(DBUS REQUIRED dbus-1>= 1.6)
include_directories(${DBUS_INCLUDE_DIRS})
link_directories(${DBUS_LIBRARY_DIRS})


#Adding glib library
pkg_check_modules(GLIB REQUIRED glib-2.0>=2.23)
include_directories(${GLIB_INCLUDE_DIRS})
link_directories(${GLIB_LIBRARY_DIRS})

pkg_check_modules (DBUSGLIB REQUIRED dbus-glib-1)
include_directories(${DBUSGLIB_INCLUDE_DIRS})
link_directories(${DBUSGLIB_LIBRARY_DIRS})

# Adding bluetooth using extra libs
list(APPEND EXTRA_LIBS "bluetooth")

# Expose 'gattlib.h' to all sub-directories
include_directories(include)


add_executable(bluez-module scantest.c)

# Linking libraries

message(${DBUSGLIB_LIBRARIES})

target_link_libraries(bluez-module ${EXTRA_LIBS})
#target_link_libraries(bluez-module ${DBUS_LIBRARIES})
target_link_libraries(bluez-module ${DBUSGLIB_LIBRARIES})
target_link_libraries(bluez-module ${GLIB_LIBRARIES})

I have to use g_main_loop in my code. But after building the source file I always get the below error :

[ 50%] Linking C executable bluez-module
CMakeFiles/bluez-module.dir/scantest.c.o: In function `read_battery_service':
scantest.c:(.text+0x5b8): undefined reference to `g_dbus_setup_bus'
collect2: error: ld returned 1 exit status

My read_battery function code is as below :

int read_battery_service(struct hci_state *current_hci_state , char *dev_addr)
{
  GError *error = NULL;
  GDBusClient *client;
    GOptionContext *context;
context = g_option_context_new(NULL);
  main_loop = g_main_loop_new(NULL, FALSE);
  dbus_conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, NULL, NULL);

  return 0;
}

Just trying to initialize for to access dbus apis.

I have included these headers in the code

#include <assert.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/hci.h>
#include <bluetooth/hci_lib.h>
#include <stdio.h>
#include <gdbus.h>
#include <glib/gmain.h>

What would be the issue ? Is glib.h contains the function g_main_loop_new ? Where should I find it ? Or Is CMake not linking glib properly ?

Bill Goldberg
  • 1,699
  • 5
  • 26
  • 50
  • Is glib found? Your `pkg_check_modules` does not have the `REQUIRED` option, so it might compile even without finding it. You could also have a look at the `${EXTRA_LIBS}` variable content, if it's non-empty. – Karsten Koop Mar 31 '17 at 07:26
  • I don't see a line where you **link** (`target_link_libraries`) with *glib*. – Tsyvarev Mar 31 '17 at 07:35
  • @Tsyvarev: `target_link_libraries(module-bluez ${GLIB_LIBRARIES})` solved the issue. But now error coming is `scantest.c:(.text+0x5b8): undefined reference to `g_dbus_setup_bus' ` What could be the problem ? Is dbus not linked ? Also If the glib is not linked why I could not get error on line #include ? – Bill Goldberg Mar 31 '17 at 08:57
  • @KarstenKoop : It is getting printed properly. – Bill Goldberg Mar 31 '17 at 09:03
  • 1
    Including and linking are two different things. If you don't have the correct include, you get a compiler error, if you don't link to the library, you get the linker error you are seeing. – Karsten Koop Mar 31 '17 at 09:05
  • Ohk. Thanks a lot. I got it now. Could you help me with the dbus error I mentioned above ? Is this not linked properly ? Is `target_link_libraries(omron ${DBUS_LIBRARIES})` not working ? How can I check it is linked or not properly ? – Bill Goldberg Mar 31 '17 at 10:05
  • On a related note, dbus-glib is a dead project dbus support is built into gio. – TingPing Apr 01 '17 at 14:02

1 Answers1

0

Looks like you are missing the gdbus linker flags. Try using pkg_check_modules (DBUSGLIB REQUIRED dbus-glib-1) and add target_link_libraries(module-bluez ${DBUS_LIBRARIES} ${DBUSGLIB_LIBRARIES}) and see if it helps.

  • I tried adding that module. But still gets this error CMake file is ``pkg_check_modules (DBUSGLIB REQUIRED dbus-glib-1) include_directories(${DBUSGLIB_INCLUDE_DIRS}) link_directories(${DBUSGLIB_LIBRARY_DIRS})`` And linked it `target_link_libraries(module-bluez ${DBUSGLIB_LIBRARIES}) `. Still gets the linkage error for function g_dbus_setup_bus Seems library I am missing is gdbus any idea ? – Bill Goldberg Mar 31 '17 at 13:11