0

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.

  • Can you please provide a sample/minimal reproducer code and steps to reproduce the issue? Let me help you by trying it from my end – Vidyalatha_Intel Dec 26 '22 at 17:48
  • The first parameter passed to `pkg_check_modules` denotes the prefix for all variables created by that call. Since you pass `GTK` here, then the call will create variables like `GTK_INCLUDE_DIRS`. If you want to use `GTK4_INCLUDE_DIRS`, then instead pass `GTK4` as the first parameter. – Tsyvarev Dec 26 '22 at 21:53

1 Answers1

0

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.

Alex Reinking
  • 16,724
  • 5
  • 52
  • 86
  • It works! I can't thank you enough for this! Although I did have to change "gtk-4.0" to just "gtk4", but it finally works as intended... Thank you again! – Chachoune963 Dec 26 '22 at 18:27