【问题标题】:Undefined references when included GLIB in clion using cmake使用 cmake 在 clion 中包含 GLIB 时未定义的引用
【发布时间】:2019-06-05 22:39:07
【问题描述】:

我正在尝试为 gnome 编写通知服务,但 cmake 在构建时不断输出未定义的引用错误。我正在使用 clion IDE 和 glib2.0 库并在 Fedora 工作站 30 上运行。

到目前为止,我已经尝试过搜索大量堆栈溢出线程并在谷歌上搜索了几个小时。

我的CMakeLists.txt 文件是从另一个 StackOverflow 线程复制的,在这里:

cmake_minimum_required(VERSION 3.14)
project(gnome_notification C)

set(CMAKE_C_STANDARD 99)

include(FindPkgConfig)
pkg_check_modules(GLIB glib-2.0 REQUIRED)
include_directories(${GLIB_INCLUDE_DIRS})

set(SOURCE_FILES main.c)

add_executable(${PROJECT_NAME} ${SOURCE_FILES})
target_link_libraries(${PROJECT_NAME} ${GLIB_LIBRARIES})

我的 main.c 文件在这里:

#include <stdio.h>
#include <gio/gio.h>
int main() {
    GApplication* application = g_application_new ("Test", G_APPLICATION_FLAGS_NONE);
    GNotification* notification = g_notification_new("Test Title");
    g_notification_set_body(notification, "Test body");
    g_notification_set_priority(notification, G_NOTIFICATION_PRIORITY_NORMAL);
    g_application_send_notification(application, "Test Id", notification);
    return 0;
}

构建产生未定义的引用错误:

/home/xgao/bin/Clion/bin/cmake/linux/bin/cmake --build /home/xgao/Desktop/gnome_notification/cmake-build-debug --target gnome_notification -- -j 4
-- Configuring done
-- Generating done
-- Build files have been written to: /home/xgao/Desktop/gnome_notification/cmake-build-debug
[ 50%] Linking C executable gnome_notification
/usr/bin/ld: CMakeFiles/gnome_notification.dir/main.c.o: in function `main':
/home/xgao/Desktop/gnome_notification/main.c:4: undefined reference to `g_application_new'
/usr/bin/ld: /home/xgao/Desktop/gnome_notification/main.c:5: undefined reference to `g_notification_new'
/usr/bin/ld: /home/xgao/Desktop/gnome_notification/main.c:6: undefined reference to `g_notification_set_body'
/usr/bin/ld: /home/xgao/Desktop/gnome_notification/main.c:7: undefined reference to `g_notification_set_priority'
/usr/bin/ld: /home/xgao/Desktop/gnome_notification/main.c:8: undefined reference to `g_application_send_notification'
collect2: error: ld returned 1 exit status
gmake[3]: *** [CMakeFiles/gnome_notification.dir/build.make:84: gnome_notification] Error 1
gmake[2]: *** [CMakeFiles/Makefile2:73: CMakeFiles/gnome_notification.dir/all] Error 2
gmake[1]: *** [CMakeFiles/Makefile2:85: CMakeFiles/gnome_notification.dir/rule] Error 2
gmake: *** [Makefile:118: gnome_notification] Error 2

【问题讨论】:

  • 这不是您发布的 CMake 错误,而是链接器错误。但是,它可能是由您的 CMake 引起的,因为链接器找不到您正在进行的这些 g_notification_* 调用的定义。
  • 您可以尝试将message(STATUS ${GLIB_LIBRARIES}) 添加到您的 CMakeLists 以查看 CMake 是否正确找到了库。另外我建议查看post,并考虑改用find_package()
  • 欢迎来到 Stack Overflow!在这里,我们希望错误消息出现在问题帖子中(作为 text),而不是作为 image 链接。请edit您的问题帖子并将错误消息复制粘贴到其中。粘贴后,您可以将该消息格式化为代码。
  • 我仍然遇到同样的错误。 message(STATUS ${GLIB_LIBRARIES}) 似乎没有告诉我更多信息,除了相同的旧未定义错误。
  • 尝试添加你的 target_link_libraries ${GLIB _LDFLAGS}

标签: c cmake clion glib


【解决方案1】:

你需要类似的东西

pkg_check_modules(GLIB glib-2.0 gio-2.0 REQUIRED)

或额外

pkg_check_modules(GIO gio-2.0 REQUIRED)

因为GNotification 是由libgio 提供的,它是与libglib 分开的库,即使它们都来自同一个源代码树。

【讨论】:

    【解决方案2】:

    试试这个:

    gcc -o note `pkg-config --cflags --libs gio-2.0` note.c
    

    【讨论】:

      猜你喜欢
      • 2016-08-21
      • 1970-01-01
      • 1970-01-01
      • 2022-01-12
      • 1970-01-01
      • 1970-01-01
      • 2018-09-01
      • 2018-04-07
      • 1970-01-01
      相关资源
      最近更新 更多