Install GTK4 on Windows

Contents
https://docs.gtk.org/gtk4/hello-world.png

Install GTK4 on Windows11

This is a guide to install and config GTK4 on Windows 11.

Note

All steps have been tested in Sandbox:

Edition Windows 11 Enterprise

Version 22H2

OS build 22621.1848

1. Download and install Msys2

This tutorial uses the default installation path, if you use a custom one, you need to pay attention when setting PATH later

Download from here, and install

After clicking Finish, the command line interface will pop up. According to the documentation of Msys2, we first install gcc:

pacman -S mingw-w64-ucrt-x86_64-gcc

Enter(Y) to install
/images/Msys2_install.png

Hint

Shift+Ins is the shortcut to paste in Msys2 Shell.

2. Install GTK4 and Mingw-toolchain

In order to install GTK4, type in the terminal:

pacman -S mingw-w64-x86_64-gtk4

To use GNU/make and other tools, type:

pacman -S mingw-w64-x86_64-toolchain base-devel

Enter until finished.
/images/Msys2_tool-chain.png

3. Setting Path

Edit System Variable (Follow here)

Add Your path to msys2mingw64bin, as default it's C:\msys64\mingw64\bin.

Add Your path to msys2usrbin, as default it's C:\msys64\usr\bin.

Note

Follow the sequence above, as Windows will follow the sequence on PATH table.

This will affect our latter input commands pkg-config and mkdir.

Given that we need to use make, however, Msys2 will name it as "mingw32-make.exe".

So we go to the directory we just put in PATH (C:\msys64\mingw64\bin), find the "mingw32-make.exe" and rename to "make.exe" (or create copy).

Now, when we open cmd or powershell, and type make -v, it works!

/images/cmd_make.png

Then, we try pkg-config --cflags --libs gtk4. We expect to have output like this:

/images/cmd_pkg-config.png

Hint

If not... "Follow" this

then, open Msys2 shell, run

cp -r /mingw64/lib/pkgconfig/* /usr/share/pkgconfig/

to copy "gtk4.pc" to the path that pkg-config can find.

4. Hello world

Open a new folder, and create a new file called demo.c.

We use the demo from GTK:

#include <gtk/gtk.h>

static void activate(GtkApplication* app, gpointer user_data) {
        GtkWidget* window;

        window = gtk_application_window_new(app);
        gtk_window_set_title(GTK_WINDOW(window), "Window");
        gtk_window_set_default_size(GTK_WINDOW(window), 200, 200);
        gtk_widget_show(window);
}

int main(int argc, char** argv) {
        GtkApplication* app;
        int status;

        app = gtk_application_new("org.gtk.example", G_APPLICATION_DEFAULT_FLAGS);
        g_signal_connect(app, "activate", G_CALLBACK(activate), NULL);
        status = g_application_run(G_APPLICATION(app), argc, argv);
        g_object_unref(app);

        return status;
}

And another file called makefile:

CC = gcc
CFLAGS = -O2 -Wall
GTK_CFLAGS = `pkg-config --cflags gtk4`
GTK_LIBS = `pkg-config --libs gtk4`

all:
        @mkdir -p ./out
        $(CC) $(GTK_CFLAGS) -o ./out/demo.exe demo.c $(GTK_LIBS)

Remember to save them, and open a cmd in this directory (Shift + Right click).

Run make, the .exe file will be generated under ./out. (Ignore warning message)

/images/GTK_hello_world.png

Summary

That's all the steps, looks like it's hard to config the basic environment for C development.

So, life is short, I choose Linux :)

0%