Install GTK4 under Ubuntu20.04LTS(amd64)
First of all
root@ubuntu: apt-get update
Basic runtime installation
root@ubuntu: sudo apt-get install build-essential
root@ubuntu: sudo apt-get install libglib2.0-dev
root@ubuntu: sudo apt-get install cmake
root@ubuntu: sudo apt-get install libffi-dev
root@ubuntu: sudo apt-get install sassc
root@ubuntu: sudo apt-get install xsltproc
root@ubuntu: sudo apt-get install graphene-1.0
root@ubuntu: sudo apt-get install pixman-dev
root@ubuntu: sudo apt-get install python3-pip
root@ubuntu: sudo apt-get install flex
root@ubuntu: sudo apt-get install bison
root@ubuntu: sudo apt-get install ninja-build
root@ubuntu: sudo apt-get install git
root@ubuntu: sudo apt-get install libharfbuzz-dev
root@ubuntu: sudo apt-get install libfontconfig-dev
root@ubuntu: sudo apt-get install libxkbcommon-dev
root@ubuntu: sudo apt-get install libcairo-dev
root@ubuntu: sudo apt-get install libwayland-dev
root@ubuntu: sudo apt-get install libxrandr-dev
root@ubuntu: sudo apt-get install libxi-dev
root@ubuntu: sudo apt-get install libegl-dev
The following libraries need to be installed manually
- Install meson ( must ensure that the version is greater than or equal to 0.56.2 )
NOTE: The version of meson installed through apt is not necessarily greater than 0.56.2. Use the following command to check the version of meson
root@ubuntu: meson -v
meson download link Download
meson-0.56.2.tar.gz
root@ubuntu: tar -xvJf meson-0.56.2.tar.gz
root@ubuntu: cd meson-0.56.2
root@ubuntu: cd meson-0.56.2
root@ubuntu: pip3 install ./
root@ubuntu: meson -v #如果是0.56.2表示安装成功
- Install gobject-introspection
gobject-introspection download
link Download gobject-introspection-1.66.1.tar.xz
root@ubuntu: tar -xvJf gobject-introspection-1.66.1.tar.xz
root@ubuntu: cd gobject-introspection-1.66.1
root@ubuntu: meson _build
root@ubuntu: cd _build
root@ubuntu: ninja
root@ubuntu: ninja install
- Install pango
pango download
link Download pango-1.48.0.tar.xz
root@ubuntu: apt-get install libx11-dev libxext-dev libxtst-dev libxrender-dev libxmu-dev libxmuu-dev
root@ubuntu: tar -xvJf pango-1.48.0.tar.xz
root@ubuntu: cd pango-1.48.0
root@ubuntu: meson _build
root@ubuntu: cd _build
root@ubuntu: ninja
root@ubuntu: ninja install
- Install gdk-pixbuf
gdk-pixbuf download
link Download gdk-pixbuf-2.42.2.tar.xz
root@ubuntu: apt-get install docbook-xsl
root@ubuntu: tar -xvJf gdk-pixbuf-2.42.2.tar.xz
root@ubuntu: cd gdk-pixbuf-2.42.2
root@ubuntu: meson _build
root@ubuntu: cd _build
root@ubuntu: ninja
root@ubuntu: ninja install
- Install wayland-protocols (version must be greater than 1.20)
wayland-protocols
download link download wayland-protocols-1.20.tar.xz
root@ubuntu: tar -xvJf wayland-protocols-1.20.tar.xz
root@ubuntu: cd gwayland-protocols-1.20
root@ubuntu: ./configure
root@ubuntu: make
root@ubuntu: make install
Download GTK4 and install
gtk4 download link Download
gtk-4.0.1.tar.xz
root@ubuntu: tar -xvJf gtk-4.0.1.tar.xz
root@ubuntu: cd gtk-4.0.1
root@ubuntu: meson _build
root@ubuntu: cd _build
root@ubuntu: ninja
root@ubuntu: ninja install
NOTE: Some libraries may be missing during the ninja process (this is inevitable). For common errors , see the description of common err1 below. The general solution is to run the following command to install the library (if you cannot install it, post it in the comments) error mesgs)
root@ubuntu:sudo apt-get install libXXX-dev
After installing the missing library, you can continue to run the ninja command in _build. If the undefined error is displayed, reconfigure the project and install it again (see the common err2 below for the command line )
Common err1
PS: If this appears, it means that libharfbuzz-dev needs to be installed
|Subproject harfbuzz is buildable: NO (disabling)
|Dependency harfbuzz from subproject subprojects/harfbuzz found: NO (subproject failed to configure)
|WARNING: The version of CMake /usr/bin/cmake is 2.8.12.2 but version >=3.4 is required
|Run-time dependency harfbuzz found: NO (tried pkgconfig and cmake)
|Looking for a fallback subproject for the dependency harfbuzz
subprojects/pango/meson.build:264:2: ERROR: Subproject "subprojects/harfbuzz" required but not found.
Common err2
If it appears during the ninjaundefinedSuch errors can be reconfigured and then compiled, for example (assuming that it is currently in the _build directory of gtk4):
reconfigure the project and install
root@ubuntu: cd .. #退出_build目录
root@ubuntu: meson setup --wipe _build
root@ubuntu: cd _build
root@ubuntu: ninja
root@ubuntu: ninja install
Check if GTK4 is installed successfully, the following information is displayed, indicating that the installation is successful
pkg-config --cflags gtk4
-mfpmath=sse -msse -msse2 -pthread -I/usr/local/include/gtk-4.0 -I/usr/local/include/gio-unix-2.0 -I/usr/local/include/pango-1.0 -I/usr/local/include/fribidi -I/usr/local/include/gdk-pixbuf-2.0 -I/usr/local/include/graphene-1.0 -I/usr/local/lib/x86_64-linux-gnu/graphene-1.0/include -I/usr/local/include -I/usr/local/include/glib-2.0 -I/usr/local/lib/x86_64-linux-gnu/glib-2.0/include -I/usr/include/cairo -I/usr/include/harfbuzz -I/usr/include/cairo -I/usr/include/pixman-1 -I/usr/include/uuid -I/usr/include/freetype2 -I/usr/include/libpng16 -I/usr/include/libmount -I/usr/include/blkid
GTK4 test program
File:hello-world-gtk.c
#include <gtk/gtk.h>
static void
print_hello (GtkWidget *widget,
gpointer data)
{
g_print ("Hello World\n");
}
static void
activate (GtkApplication *app,
gpointer user_data)
{
GtkWidget *window;
GtkWidget *button;
window = gtk_application_window_new (app);
gtk_window_set_title (GTK_WINDOW (window), "Window");
gtk_window_set_default_size (GTK_WINDOW (window), 200, 200);
button = gtk_button_new_with_label ("Hello World");
g_signal_connect (button, "clicked", G_CALLBACK (print_hello), NULL);
gtk_window_set_child (GTK_WINDOW (window), button);
gtk_window_present (GTK_WINDOW (window));
}
int
main (int argc,
char **argv)
{
GtkApplication *app;
int status;
app = gtk_application_new ("org.gtk.example", G_APPLICATION_FLAGS_NONE);
g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
status = g_application_run (G_APPLICATION (app), argc, argv);
g_object_unref (app);
return status;
}
Compile program
gcc -o hello-world-gtk hello-world-gtk.c `pkg-config --cflags --libs gtk4`