使用cmake将GLIB包含在clion中时未定义的引用

时间:2019-06-05 22:39:07

标签: c cmake clion glib

我正在尝试为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

2 个答案:

答案 0 :(得分:1)

你需要类似的东西

pkg_check_modules(GLIB glib-2.0 gio-2.0 REQUIRED)

或另外

pkg_check_modules(GIO gio-2.0 REQUIRED)

因为 GNotification 是由 libgio 提供的,libglib 是一个独立于 export CON_DB_TECY=Username/Password@host:port/Servicename -> `abc/abc@local:123/orabc` 的库,即使它们都来自同一源代码树。

答案 1 :(得分:0)

试试这个:

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