I've been trying to set up GTK4.0 on my C project but I haven't found anything that works yet, besides maybe brutaly including every directory in the gtk version I've installed but I understandably want to have a more elegant solution.
Sorry if the problem is simple, I really need to get this done... Here's the current CMakeLists file:
cmake_minimum_required(VERSION 3.12)
project(ProjectCESGI C)
set(CMAKE_C_STANDARD 99)
set(CMAKE_C_FLAGS "-Wall")
find_package(PkgConfig REQUIRED)
pkg_check_modules(GTK REQUIRED gtk-4.0)
include_directories(${GTK4_INCLUDE_DIRS})
link_directories(${GTK4_LIBRARY_DIRS})
add_definitions(${GTK4_CFLAGS_OTHER})
target_link_libraries(gtk_test ${GTK4_LIBRARIES})
link_directories(C:/gtk-build/gtk/x64/release/lib)
link_libraries(gtk)
add_executable(ProjectCESGI main.c)
target_link_libraries(ProjectCESGI gtk)
If you need something else let me know.
I tried to brute-force my way out of the not found headers, but stopped halfway through realising how ridiculous the process was. I also tried to look for a package which should somehow help me install the library, PkgConfig, but this doesn't seem to work either.
This should work:
cmake_minimum_required(VERSION 3.18)
project(ProjectCESGI LANGUAGES C)
find_package(PkgConfig REQUIRED)
pkg_check_modules(GTK4 REQUIRED IMPORTED_TARGET gtk4)
add_executable(ProjectCESGI main.c)
target_link_libraries(ProjectCESGI PRIVATE PkgConfig::GTK4)
It's always better to use an imported target when one is available, and pkg_check_modules
can create one for you with the IMPORTED_TARGET
option. When you link to an imported target via target_link_libraries
, CMake knows to set up the appropriate include directories, link directories, compile definitions, etc. to use the library.
Carefully consult the documentation if your build has trouble finding GTK4. Your build really should not be any more complex than this.