How to best identify if GTK is installed?

I want to create a software which depending on the WM will use either GTK or Qt.

For that I need to make a Makefile which will check what is installed.

In a Makefile you can use pkg-config to check if a (build) dependency exists and/or get the CFLAGS or LDFLAGS:

pkg-config --exists gtk+-3.0 or pkg-config --exists gtk4 will have success or error return codes.
you could f.e. have
GOT_GTK3 = $(shell pkg-config --exists gtk+-3.0 && echo 1 || echo 0) or better:

GTK3_CFLAGS = $(shell pkg-config --cflags gtk+3.0)
GTK3_LDLAGS = $(shell pkg-config --libs gtk+3.0)

Usually you want a little more advanced than pure Makefiles for this sort of thing: autoconf tools, cmake or meson.

For meson I recommend checking the docs on dependencies .

A few remarks though:
On a typical desktop you end up with both Qt and GTK anyway (f.e. installing Mumble on a GTK/GNOME based system would install Qt libraries).

Not a Qt developer, but Qt and GTK are sufficiently different making me think maintaining both in the same project sounds challenging. Why not just use one or the other?
Or if you really want to do both, you can do that as well, but I would suggest treating each as its own project. If you’re concerned about reusing common functionality and data structure you could always split those out into a separate (potentially statically linked) library and voilà now you have three different projects :wink:

I was actually looking for (hoping for) a way to identify the presence of the GTK regardless of the version.

Now, I will not maintain GTK/Qt itself - I’m not that “crazy”. But I do want to identify if the system has just Qt or just GTK+ and use the appropriate libraries for the build.

Granted - I can introduce a configure switch and simply rely on the user, but one never knows.

The default value will be GTK+, and so I might as well see if Qt is available to override the defaults…

Or you mean create a 2 source trees - one for GTK and one for Qt? A lot of stuff will be exactly the same in my case (like 95% of the code). I’m using wxWidgets so the code will diverge only in some very specific areas…

Thank you.

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