我像这样设置CMakeLists:
project(physiotreapy)
set(HEDEAR_FILES Physiotreapy.h)
set(SOURCE_FILES Physiotreapy.cpp)
add_library(libphysiotreapy STATIC ${SOURCE_FILES})
target_include_directories(libphysiotreapy PUBLIC .)
add_executable(physiotreapy ${SOURCE_FILES} main.cpp)
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})
target_link_libraries(physiotreapy libphysiotreapy )
但是当我尝试运行此代码时:
#include <gtk/gtk.h>
int main (int argc, char *argv[]) {
GtkWidget *okno;
gtk_init(&argc,&argv);
okno = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_widget_show (okno);
gtk_main ();
return 0;
}
我收到一些错误:
/cygdrive/c/Users/Mateusz/Desktop/My project/projects/physiotreapy/main.cpp:11: undefined reference to `gtk_init_abi_check'
/cygdrive/c/Users/Mateusz/Desktop/My project/projects/physiotreapy/main.cpp:11:(.text+0x35): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `gtk_init_abi_check'
/cygdrive/c/Users/Mateusz/Desktop/My project/projects/physiotreapy/main.cpp:12: undefined reference to `gtk_window_new'
/cygdrive/c/Users/Mateusz/Desktop/My project/projects/physiotreapy/main.cpp:12:(.text+0x3f): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `gtk_window_new'
/cygdrive/c/Users/Mateusz/Desktop/My project/projects/physiotreapy/main.cpp:13: undefined reference to `gtk_widget_show'
/cygdrive/c/Users/Mateusz/Desktop/My project/projects/physiotreapy/main.cpp:13:(.text+0x4f): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `gtk_widget_show'
/cygdrive/c/Users/Mateusz/Desktop/My project/projects/physiotreapy/main.cpp:15: undefined reference to `gtk_main'
/cygdrive/c/Users/Mateusz/Desktop/My project/projects/physiotreapy/main.cpp:15:(.text+0x54): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `gtk_main'
最佳答案
看起来您那里有95%,但是我认为您忘记列出要链接的库了。我的“FooBar”项目的CMakeList.txt文件的相关部分包含以下内容,其中前3行与您的行几乎相同:
FIND_PACKAGE ( PkgConfig REQUIRED )
PKG_CHECK_MODULES( GTK REQUIRED gtk+-3.0 ) # or use gtk+-2.0, etc...
INCLUDE_DIRECTORIES ( ${GTK_INCLUDE_DIRS} )
SET ( FOOBAR_LIBS ${GTK_LIBRARIES} )
请注意,我在第四行跟踪需要哪些库。然后,当我创建可执行文件时,在我的CMakeLists.txt中:
ADD_EXECUTABLE ( foobar foobar.cpp )
TARGET_LINK_LIBRARIES( foobar ${CMAKE_THREAD_LIBS_INIT} ${FOOBAR_LIBS} )
请注意$ {FOOBAR_LIBS},在我的例子和许多实际示例中,它包含许多库,而不仅仅是GTK +库。另一种流行的方法是列出这样的库:
ADD_EXECUTABLE ( foobar foobar.cpp )
TARGET_LINK_LIBRARIES( foobar ${CMAKE_THREAD_LIBS_INIT} ${GTK_LIBRARIES} )
如果您需要链接多个包,则将它们全部列出,类似于以下内容:
ADD_EXECUTABLE ( foobar foobar.cpp )
TARGET_LINK_LIBRARIES( foobar ${CMAKE_THREAD_LIBS_INIT} ${GTK_LIBRARIES} ${POSTGRESQL_LIBRARIES} ${OPENGL_LIBRARIES} )
关于c++ - 如何使用GTK +设置CMakeList?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46791091/