【问题标题】:Issue with CMake when using glib library使用 glib 库时出现 CMake 问题
【发布时间】:2017-03-31 07:21:43
【问题描述】:

我正在编写一个 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 api。

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

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

【问题讨论】:

  • 找到 glib 了吗?您的 pkg_check_modules 没有 REQUIRED 选项,因此即使没有找到它也可能编译。您还可以查看${EXTRA_LIBS} 变量内容,如果它不为空。
  • 我没有看到你用 glib 链接 (target_link_libraries) 的行。
  • @Tsyvarev: target_link_libraries(module-bluez ${GLIB_LIBRARIES}) 解决了这个问题。但是现在出现的错误是scantest.c:(.text+0x5b8): undefined reference to g_dbus_setup_bus'`可能是什么问题? dbus 没有链接吗?此外,如果 glib 未链接,为什么我无法在 #include 行上出错?
  • @KarstenKoop :打印正常。
  • 包含和链接是两个不同的东西。如果你没有正确的包含,你会得到一个编译器错误,如果你没有链接到库,你会得到你所看到的链接器错误。

标签: cmake glib bluez


【解决方案1】:

看起来您缺少 gdbus 链接器标志。尝试使用 pkg_check_modules (DBUSGLIB REQUIRED dbus-glib-1) 并添加 target_link_libraries(module-bluez ${DBUS_LIBRARIES} ${DBUSGLIB_LIBRARIES}) 看看有没有帮助。

【讨论】:

  • 我尝试添加该模块。但仍然得到这个错误 CMake 文件是pkg_check_modules (DBUSGLIB REQUIRED dbus-glib-1) include_directories(${DBUSGLIB_INCLUDE_DIRS}) link_directories(${DBUSGLIB_LIBRARY_DIRS}) 并链接它target_link_libraries(module-bluez ${DBUSGLIB_LIBRARIES}) 。仍然得到函数 g_dbus_setup_bus 的链接错误似乎我缺少的库是 gdbus 知道吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-07-31
  • 2021-01-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-11
  • 2023-02-03
相关资源
最近更新 更多