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 gtk - How to compile a GTK4 application in Ubuntu 21.10? - Ask Ubuntu 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.

You can find the instructions on how to compile GTK applications in the project’s documentation.

I also recommend you start from the “getting started” tutorial on the GNOME developer documentation website.

Thanks for responding but the first command on the doc site caused an error:

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
-pthread: command not found

I removed the line-break and got:

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
Unknown option -pthread

I tried sudo with the same outcome. Am I doing something wrong?

You need to pass the result of the pkg-config invocation to your compiler:

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

Just like the documentation says.

Again, I would strongly recommend you use Builder.

Sorry I don’t understand. Doesn’t the page say to run:

pkg-config --cflags gtk4
 -pthread -I/usr/include/gtk-4.0 ...

…then:

pkg-config --libs gtk4
 -pthread -lgtk-4 ...

Then run cc ...

The first two commands are returning errors relating to -pthread.

First of all, you need to ignore StackOverflow.

The first two examples show what pkg-config --cflags gtk4 and pkg-config --libs gtk4 print out to the terminal. For instance, this is what they print out in my environment:

The command line is:

pkg-config --cflags gtk4

and:

pkg-config --libs gtk4

This is only needed if you’re manually compiling with the C compiler from the terminal.

If you’re writing an application with GNOME Builder, you can follow the “Getting Started” tutorial on the GNOME developer documentation website, as I already said in my reply.

Follow the instructions there on how to create a template application; Builder will download all the dependencies inside the build environment, and you won’t have to deal with the command line.

OK, I’m with you now. A little more magic than I was hoping for but it works.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.