Reputation: 29
I set up CMakeLists like this :
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 )
but when I try to run this code :
#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;
}
I get some error:
/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'
Upvotes: 1
Views: 3447
Reputation: 20360
Looks like you were 95% there, but I think you forgot to list the libraries to link. The relevant part of my CMakeList.txt file for my 'FooBar' project contains this, where the first 3 lines are almost identical to yours:
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} )
Note that 4th line where I track what libraries are needed. Then later in my CMakeLists.txt when I create the executable:
ADD_EXECUTABLE ( foobar foobar.cpp )
TARGET_LINK_LIBRARIES( foobar ${CMAKE_THREAD_LIBS_INIT} ${FOOBAR_LIBS} )
Note the ${FOOBAR_LIBS}, which in my case and in many real-world examples contains many libraries, not just the GTK+ libs. Another popular way to do it would be to list the libraries like this:
ADD_EXECUTABLE ( foobar foobar.cpp )
TARGET_LINK_LIBRARIES( foobar ${CMAKE_THREAD_LIBS_INIT} ${GTK_LIBRARIES} )
If you had multiple packages you need to link against, you'd list them all, something similar to this:
ADD_EXECUTABLE ( foobar foobar.cpp )
TARGET_LINK_LIBRARIES( foobar ${CMAKE_THREAD_LIBS_INIT} ${GTK_LIBRARIES} ${POSTGRESQL_LIBRARIES} ${OPENGL_LIBRARIES} )
Upvotes: 1