Before we get into the details of how to compile GTK, we should mention that in many cases, binary packages of GTK prebuilt for your operating system will be available, either from your operating system vendor or from independent sources. If such a set of packages is available, installing it will get you programming with GTK much faster than building it yourself. In fact, you may well already have GTK installed on your system already.
In order to build GTK, you will need meson installed on your system. On Linux, and other UNIX-like operating systems, you will also need ninja. This guide does not cover how to install these two requirements, but you can refer to the Meson website for more information. The Ninja build tool is also usable on various operating systems, so we will refer to it in the examples.
If you are building GTK from a source distribution or from a Git
clone, you will need to use meson to
configure the project. The most commonly useful argument is the
--prefix
one, which determines where the files
will go once installed. To install GTK under a prefix like
/opt/gtk
you would run Meson as:
meson setup --prefix /opt/gtk builddir
Meson will create the builddir
directory and
place all the build artefacts there.
You can get a list of all available options for the build by
running meson configure
.
After Meson successfully configured the build directory, you then can run the build, using Ninja:
cd builddir ninja ninja install
If you don’t have permission to write to the directory you are
installing in, you may have to change to root temporarily before
running ninja install
.
Several environment variables are useful to pass to set before
running meson. CPPFLAGS
contains options to pass to the C compiler, and is used to tell
the compiler where to look for include files. The
LDFLAGS
variable is used in a similar fashion
for the linker. Finally the PKG_CONFIG_PATH
environment variable contains a search path that
pkg-config
(see below) uses when looking for
files describing how to compile programs using different
libraries. If you were installing GTK and it’s dependencies into
/opt/gtk
, you might want to set these variables
as:
CPPFLAGS="-I/opt/gtk/include" LDFLAGS="-L/opt/gtk/lib" PKG_CONFIG_PATH="/opt/gtk/lib/pkgconfig" export CPPFLAGS LDFLAGS PKG_CONFIG_PATH
You may also need to set the LD_LIBRARY_PATH
environment variable so the systems dynamic linker can find the
newly installed libraries, and the PATH
environment program so that utility binaries installed by the
various libraries will be found.
LD_LIBRARY_PATH="/opt/gtk/lib" PATH="/opt/gtk/bin:$PATH" export LD_LIBRARY_PATH PATH