Search code examples
cmakegtkgtk3fedora

Cannot find gtk/gtk.h when building nativefiledialog on Fedora 37


I am using nativefiledialog-cmake in my C++ project as a submodule. When I generate the build files using CMake, it does not generate properly as it cannot find the gtk/gtk.h file needed for one of nativefiledialog's source files : nfd_gtk.c.


I have installed the following GTK and GTK dependency packages:

gtk2
gtk3
gtk4
gtk2-devel
gtk3-devel
gtk4-devel
gtk4-devel-tools

glib
glib-devel
gdk-pixbuf2-devel
atk
atk-devel
gobject-introspection
gobject-introspection-devel
libepoxy
libepoxy-devel

and ls /usr/include | grep gtk returns

gtk-2.0
gtk-3.0
gtk-4.0
gtk-unix-print-2.0

The GTK headers are inside these folders.


This is nativefiledialog-cmake's CMakeLists.txt file:

include(CheckIncludeFile)
set(SOURCES src/nfd_common.c)

macro(REQUIRE_INCLUDE_FILE path name)
    CHECK_INCLUDE_FILE(${path} ${name})
    if (NOT ${name})
        message(FATAL_ERROR "${path} not found")
    endif ()
endmacro()
    
# add specific implementations
if (WIN32)
    REQUIRE_INCLUDE_FILE(windows.h HAS_WINDOWS)
    list(APPEND SOURCES src/nfd_win.cpp)
elseif (APPLE)
    REQUIRE_INCLUDE_FILE(AppKit/AppKit.h HAS_APPKIT)
    list(APPEND SOURCES src/nfd_cocoa.m)
elseif (UNIX)
    REQUIRE_INCLUDE_FILE(gtk/gtk.h HAS_GTK)
    list(APPEND SOURCES src/nfd_gtk.c)
elseif (UNIX)
    message(FATAL_ERROR "Cannot detect your system, please report to https://github.com/aarcangeli/nativefiledialog-cmake/issues")
endif ()

add_library(nativefiledialog ${SOURCES})
target_include_directories(nativefiledialog PUBLIC src/include)

I have tried adding this line (returned from pkg-config --cflags gtk+-3.0) to tell the compiler where the GTK headers are located with no avail:

set(FLAGS "${FLAGS} -I/usr/include/gtk-3.0 -I/usr/include/pango-1.0 -I/usr/include/glib-2.0 -I/usr/lib64/glib-2.0/include -I/usr/include/sysprof-4 -I/usr/include/harfbuzz -I/usr/include/freetype2 -I/usr/include/libpng16 -I/usr/include/libmount -I/usr/include/blkid -I/usr/include/fribidi -I/usr/include/libxml2 -I/usr/include/cairo -I/usr/include/pixman-1 -I/usr/include/gdk-pixbuf-2.0 -I/usr/include/gio-unix-2.0 -I/usr/include/cloudproviders -I/usr/include/atk-1.0 -I/usr/include/at-spi2-atk/2.0 -I/usr/include/dbus-1.0 -I/usr/lib64/dbus-1.0/include -I/usr/include/at-spi-2.0 -pthread")

How do I get CMake to generate properly?


Solution

  • First of all I am quite not sure how you arrived at the set(FLAGS "${FLAGS} ...") line, because unless you've specified some additional logic, that just creates a variable FLAGS, it is not a standard CMake variable IIRC.

    Perhaps what you meant to use was target_compile_options(), where you can specify additional compile options to a given target which in your case is most likely nativefiledialog. That might fix your issue i.e.:

    target_compile_options(nativefiledialog PUBLIC "$<$<CONFIG:DEBUG>:${DEBUG_FLAGS}>")
    target_compile_options(nativefiledialog PUBLIC "$<$<CONFIG:RELEASE>:${RELEASE_FLAGS}>")
    

    Or in your case:

    target_compile_options(nativefiledialog PUBLIC "${FLAGS}")
    

    This however is not good practice and I would recommend rewriting nativefiledialog's CMakeLists.txt.

    Bare in mind that this is in no way your fault, the nativefiledialog has a very bad CMakeLists.txt because the author didn't use any prebuild CMake functionalities like find_package(GTK).