Score:5

How to compile a GTK4 application in Ubuntu 21.10?

kr flag

I am trying to compile a C GUI application using GTK4. I was previously told here that both Ubuntu 21.04 and 21.10 would have both GTK3 and GTK4. However, when I installed Ubuntu 21.04 only GTK3 was present. I then did a dist-upgrade to 21.10 and found that both versions of GTK are native:

user@user: dpkg -l libgtk* | grep -e '^i' | grep -e 'libgtk-*[0-9]'
ii  libgtk-3-0:amd64           3.24.30-1ubuntu1 amd64        GTK graphical user interface library
ii  libgtk-3-bin               3.24.30-1ubuntu1 amd64        programs for the GTK graphical user interface library
ii  libgtk-3-common            3.24.30-1ubuntu1 all          common files for the GTK graphical user interface library
ii  libgtk-3-dev:amd64         3.24.30-1ubuntu1 amd64        development files for the GTK library
ii  libgtk-3-doc               3.24.30-1ubuntu1 all          documentation for the GTK graphical user interface library
ii  libgtk-4-1:amd64           4.4.0+ds1-5      amd64        GTK graphical user interface library
ii  libgtk-4-bin               4.4.0+ds1-5      amd64        programs for the GTK graphical user interface library
ii  libgtk-4-common            4.4.0+ds1-5      all          common files for the GTK graphical user interface library
ii  libgtk2.0-0:amd64          2.24.33-2ubuntu1 amd64        GTK graphical user interface library - old version
ii  libgtk2.0-bin              2.24.33-2ubuntu1 amd64        programs for the GTK graphical user interface library
ii  libgtk2.0-common           2.24.33-2ubuntu1 all          common files for the GTK graphical user interface library
ii  libgtk3-perl               0.038-1          all          Perl bindings for the GTK+ graphical user interface library

Just double checking the version of Ubuntu yields:

user@user: lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 21.10
Release:    21.10
Codename:   impish

The GTK page says to use the following to compile a GTK4 app using pkg-config:

gcc $( pkg-config --cflags gtk4 ) -o example-0 example-0.c $( pkg-config --libs gtk4 )

This is very similar to previous versions of GTK. For GTK3 it was:

gcc $( pkg-config --cflags gtk+-3.0 ) -o example-0 example-0.c $( pkg-config --libs gtk+-3.0 )

However, trying to compile the program using GTK4 results in:

user@user: make
Package gtk4 was not found in the pkg-config search path.
Perhaps you should add the directory containing `gtk4.pc'
to the PKG_CONFIG_PATH environment variable
No package 'gtk4' found

So then based on here I ran:

user@user: pkg-config --variable pc_path pkg-config
/usr/local/lib/x86_64-linux-gnu/pkgconfig:/usr/local/lib/pkgconfig:/usr/local/share/pkgconfig:/usr/lib/x86_64-linux-gnu/pkgconfig:/usr/lib/pkgconfig:/usr/share/pkgconfig

Searching all of those paths, I only find GTK related .pc files in /usr/lib/x86_64-linux-gnu/pkgconfig and I do not find anything for GTK4. I do find gtk+-3.0.pc there though. So my question is, if GTK4 is native to Ubuntu 21.10, why would the pkg-config files be missing? Where do I obtain them so I can compile using pkg-config?

ASIDE: I do not have enough reputation to create the gtk4 tag. Could someone with enough reputation do so?

us flag
Do you have `libgtk-4-dev` installed? If not, install it with `sudo apt install libgtk-4-dev`
Leigh K avatar
kr flag
No, I did not, and that solved the problem. Not sure why that would be necessary when GTK4 was present on the OS without me downloading it.
us flag
It was not present, it is available in the repositories to be downloaded (just as, the KDE desktop is available in the repositories, but most likely you don't have it installed). It is not present by default because a very few percentage of users (only those who develop GTK4 apps) will require it.
us flag
I created the gtk4 tag.
Score:6
us flag

You will need to install libgtk-4-dev, with the command

sudo apt install libgtk-4-dev
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.