Search code examples
cubuntugtk4

How should I set up to develop with GTK4?


I'm running Ubuntu 21.10 and have installed build-essential, libgtk-4-dev and Gnome Builder as IDE. I'm starting with a basic tutorial that starts out #include <gtk/gtk.h> but it gives the error:

1:10: error: 'gtk/gtk.h' file not found

I read somewhere that it should be in /usr/include/ but when I looked I found it was at /usr/include/gtk-4.0/gtk/gtk.h

What's the best way to set up my system so that the default include line works? I read https://askubuntu.com/questions/1374329/how-to-compile-a-gtk4-application-in-ubuntu-21-10 but his fix was installing libgtk-4-dev which I've already done. I thought of creating a symlink from /usr/include/gtk-4.0/gtk/gtk.h to /usr/include/ but that seems like a rabbit hole I shouldn't go down. I'm stuck before I even got started! Any help appreciated.


Solution

  • Per the official C documentation on compiling programs with GTK4 on Unix you need to run pkg-config with the requisite paths before running the compiler:

    Include:

    $ pkg-config --cflags gtk4 -pthread -I/usr/include/gtk-4.0 \
        -I/usr/lib64/gtk-4.0/include -I/usr/include/cairo \
        -I/usr/include/pango-1.0 -I/usr/include/glib-2.0 \
        -I/usr/lib64/glib-2.0/include -I/usr/include/pixman-1 \
        -I/usr/include/freetype2 -I/usr/include/libpng12
    

    Libraries:

    $ pkg-config --libs gtk4 -pthread -lgtk-4 -lgdk-4 -lgio-2.0 \
        -lpangoft2-1.0 -lgdk_pixbuf-2.0 -lpangocairo-1.0 -lcairo \
        -lpango-1.0 -lfreetype -lfontconfig -lgobject-2.0 \
        -lgmodule-2.0 -lgthread-2.0 -lrt -lglib-2.0
    

    Compile:

    $ cc `pkg-config --cflags gtk4` hello.c -o hello `pkg-config --libs gtk4`
    

    Make sure to replace cc with your respective compiler and hello with your source file and desired executable name. Also you may need to remove the -pthread flags.