将GTK与CMake一起使用时的“未定义引用"

编程入门 行业动态 更新时间:2024-10-28 10:36:35
本文介绍了将GTK与CMake一起使用时的“未定义引用"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用CMake构建基本的GTK +代码(其教程之一),但是我一直在获得对*的未定义引用",其中*是代码中的任何GTK函数.我试图在这方面照顾指南/教程,但大多数都与我所拥有的相似.

I'm trying to build basic GTK+ code(one of its tutorials) with CMake, but I keep getting 'undefined reference to *' where * is any GTK function that is in the code. I tried to look after guides/tutorials on this, but most of them are mostly similar to what I have.

使用命令从终端进行常规编译不会出现问题:

Regular compilation from terminal works without a problem using command:

g++ ./test.cpp -o test `pkg-config --cflags --libs gtk+-3.0`

当我尝试使用CMake时,我的CMakeLists.txt是:

When I try using CMake my CMakeLists.txt is:

cmake_minimum_required(VERSION 3.0.2) project(gtktest) find_package(PkgConfig REQUIRED) pkg_check_modules(GTK3 REQUIRED gtk+-3.0) include_directories(${GTK3_INCLUDE_DIRS}) link_directories(${GTK3_LIBRARY_DIRS}) add_definitions(${GTK3_CFLAGS_OTHER}) add_executable(gtktest test.cpp) target_link_libraries(gtktest ${GKT3_LIBRARIES})

现在 cmake.似乎可以正常工作

cmake . -- The C compiler identification is GNU 7.4.0 -- The CXX compiler identification is GNU 7.4.0 -- Check for working C compiler: /usr/bin/cc -- Check for working C compiler: /usr/bin/cc -- works -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Detecting C compile features -- Detecting C compile features - done -- Check for working CXX compiler: /usr/bin/c++ -- Check for working CXX compiler: /usr/bin/c++ -- works -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info - done -- Detecting CXX compile features -- Detecting CXX compile features - done -- Found PkgConfig: /usr/bin/pkg-config (found version "0.29.1") -- Checking for module 'gtk+-3.0' -- Found gtk+-3.0, version 3.22.30 -- Configuring done -- Generating done -- Build files have been written to: /home/dfdark/Desktop/gtk test

但是在那之后执行命令就是问题:

But make command after that is the problem:

make Scanning dependencies of target gtktest [ 50%] Building CXX object CMakeFiles/gtktest.dir/test.cpp.o [100%] Linking CXX executable gtktest CMakeFiles/gtktest.dir/test.cpp.o: In function `activate(_GtkApplication*, void*)': test.cpp:(.text+0x27): undefined reference to `gtk_application_window_new' test.cpp:(.text+0x30): undefined reference to `gtk_window_get_type' test.cpp:(.text+0x42): undefined reference to `g_type_check_instance_cast' test.cpp:(.text+0x51): undefined reference to `gtk_window_set_title' test.cpp:(.text+0x56): undefined reference to `gtk_window_get_type' test.cpp:(.text+0x68): undefined reference to `g_type_check_instance_cast' test.cpp:(.text+0x7a): undefined reference to `gtk_window_set_default_size' test.cpp:(.text+0x86): undefined reference to `gtk_widget_show_all' CMakeFiles/gtktest.dir/test.cpp.o: In function `main': test.cpp:(.text+0xcc): undefined reference to `gtk_application_new' test.cpp:(.text+0xfb): undefined reference to `g_signal_connect_data' test.cpp:(.text+0x100): undefined reference to `g_application_get_type' test.cpp:(.text+0x112): undefined reference to `g_type_check_instance_cast' test.cpp:(.text+0x126): undefined reference to `g_application_run' test.cpp:(.text+0x135): undefined reference to `g_object_unref' collect2: error: ld returned 1 exit status CMakeFiles/gtktest.dir/build.make:94: recipe for target 'gtktest' failed make[2]: *** [gtktest] Error 1 CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/gtktest.dir/all' failed make[1]: *** [CMakeFiles/gtktest.dir/all] Error 2 Makefile:83: recipe for target 'all' failed make: *** [all] Error 2

test.cpp代码(摘自GTK +教程)

test.cpp code (From GTK+ tutorials)

#include <gtk/gtk.h> static void activate (GtkApplication* app, gpointer user_data) { GtkWidget *window; window = gtk_application_window_new (app); gtk_window_set_title (GTK_WINDOW (window), "Window"); gtk_window_set_default_size (GTK_WINDOW (window), 200, 200); gtk_widget_show_all (window); } int main (int argc, char **argv) { GtkApplication *app; int status; app = gtk_application_new ("org.gtk.example", G_APPLICATION_FLAGS_NONE); g_signal_connect (app, "activate", G_CALLBACK (activate), NULL); status = g_application_run (G_APPLICATION (app), argc, argv); g_object_unref (app); return status; }

问题是否弄乱了CMake命令的顺序?

Is the problem messed up order of CMake's commands?

推荐答案

在您的CMakeLists.txt中使用GTK代替GTK3:

Use GTK in your CMakeLists.txt instead of GTK3:

cmake_minimum_required(VERSION 3.0.2) project(gtktest) find_package(PkgConfig REQUIRED) pkg_check_modules(GTK REQUIRED gtk+-3.0) include_directories(${GTK_INCLUDE_DIRS}) link_directories(${GTK_LIBRARY_DIRS}) add_definitions(${GTK_CFLAGS_OTHER}) add_executable(gtktest test.cpp) target_link_libraries(gtktest ${GTK_LIBRARIES})

更多推荐

将GTK与CMake一起使用时的“未定义引用"

本文发布于:2023-08-04 01:26:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1291663.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:未定义   GTK   CMake   quot

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!