使用glib库时CMake的问题

时间:2017-03-31 07:21:43

标签: cmake glib bluez

我正在写一个bluez C程序来读取电池服务。我正在使用CMake来构建代码。 我的Cmake文件是:

    # 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})

我必须在我的代码中使用g_main_loop。但在构建源文件后,我总是得到以下错误:

[ 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

我的read_battery功能代码如下:

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;
}

只是尝试初始化以访问dbus apis。

我已将这些标题包含在代码中

#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>

会出现什么问题? glib.h是否包含函数g_main_loop_new?我应该在哪里找到它?或者CMake没有正确连接glib吗?

1 个答案:

答案 0 :(得分:0)

看起来你错过了gdbus链接器标志。尝试使用     pkg_check_modules (DBUSGLIB REQUIRED dbus-glib-1)并添加     target_link_libraries(module-bluez ${DBUS_LIBRARIES} ${DBUSGLIB_LIBRARIES})  并看看它是否有帮助。