Use C language and GTK4 Msys2 to development lightweight Windows GUI app
Environment Preparing
- Download and installation the Msys2
- After install the wsys2 open and create shortcut
ucrt64.exe
to desktop - In
ucrt64.exe
console shell usepacman
to install packages
1 2pacman -Syu 3#Repeat this step until all packages are up to date. 4#Follow any given instructions
- After install the wsys2 open and create shortcut
- Installation the essential packages
1 2 pacman -S mingw-w64-x86_64-gtk4 3 pacman -S mingw-w64-x86_64-toolchain base-devel
- Test and verify the packages
1 gcc --version 2 #Discovers the directories where the GTK include files are stored. 3 pkg-config gtk4 --cflags 4 #Discovers the names of the GTK library files. 5 pkg-config gtk4 --libs 6 #Of course we can combine both commands to receive a bundled output 7 pkg-config gtk4 --cflags --libs
Set system environment variable
- Open the
sysdm.cpl
in search bar or press WIN+R key - TO Advanced > Environment Variables
- Add
C:\msys64\ucrt64\bin
to path environment variable
Write a helloworld-gtk.c
gtk c code
1#include <gtk/gtk.h>
2
3static void
4print_hello (GtkWidget *widget,
5 gpointer data)
6{
7 g_print ("Hello World\n");
8}
9
10static void
11activate (GtkApplication *app,
12 gpointer user_data)
13{
14 GtkWidget *window;
15 GtkWidget *button;
16
17 window = gtk_application_window_new (app);
18 gtk_window_set_title (GTK_WINDOW (window), "Hello");
19 gtk_window_set_default_size (GTK_WINDOW (window), 200, 200);
20
21 button = gtk_button_new_with_label ("Hello World");
22 g_signal_connect (button, "clicked", G_CALLBACK (print_hello), NULL);
23 gtk_window_set_child (GTK_WINDOW (window), button);
24
25 gtk_window_present (GTK_WINDOW (window));
26}
27
28int
29main (int argc,
30 char **argv)
31{
32 GtkApplication *app;
33 int status;
34
35 app = gtk_application_new ("org.gtk.example", G_APPLICATION_DEFAULT_FLAGS);
36 g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
37 status = g_application_run (G_APPLICATION (app), argc, argv);
38 g_object_unref (app);
39
40 return status;
41}
Compile and running in ucrt64.exe
shell
- Change current folder to code’s path
1# change to my code folder
2cd /c/mycode
- Execute compile and running the program
1# compile the code
2gcc $(pkg-config --cflags gtk4) -o hello-world-gtk helloworld-gtk.c $(pkg-config --libs gtk4)
3# execute
4./hello-world-gtk.exe
Compile and running in cmd.exe
windows command prompt
- the compile arguments can acquire use
pkg-config gtk4 --cflags --libs
inucrt64.exe
shell - please replace the relative path to yourself code and installation path or disk driver name
Caution: the command line compile arguments is in one line, don’t multiple lines
1# exectue the compile in cmd.exe, please use Administrator permission in this cmd.exe indtance window
2D:\msys64\ucrt64\bin\gcc.exe c:\Users\tom\mygtk-project\main.c -o c:\Users\tom\mygtk-project\main.exe -I"D:/msys64/ucrt64/include" -I"D:/msys64/ucrt64/lib/gcc/x86_64-w64-mingw32/14.2.0/include" -I"D:/msys64/ucrt64/include/glib-2.0" -ID:/msys64/ucrt64/include/gtk-4.0 -ID:/msys64/ucrt64/include/pango-1.0 -ID:/msys64/ucrt64/include/fribidi -ID:/msys64/ucrt64/include/harfbuzz -ID:/msys64/ucrt64/include/gdk-pixbuf-2.0 -ID:/msys64/ucrt64/include/webp -DLIBDEFLATE_DLL -ID:/msys64/ucrt64/include/cairo -ID:/msys64/ucrt64/include/freetype2 -ID:/msys64/ucrt64/include/libpng16 -ID:/msys64/ucrt64/include/pixman-1 -ID:/msys64/ucrt64/include/graphene-1.0 -ID:/msys64/ucrt64/lib/graphene-1.0/include -mfpmath=sse -msse -msse2 -ID:/msys64/ucrt64/include/glib-2.0 -ID:/msys64/ucrt64/lib/glib-2.0/include -lgtk-4 -lpangowin32-1.0 -lpangocairo-1.0 -lpango-1.0 -lharfbuzz -lgdk_pixbuf-2.0 -lcairo-gobject -lcairo -lvulkan-1.dll -lgraphene-1.0 -lgio-2.0 -lgobject-2.0 -lglib-2.0 -lintl
Compile and debug in Dev-C++
IDE
- Please download suit windows 10 above Dev-C++ version in Win10 Dev-C++ version
- New a C project in Dev-C++
- Add the
main.c
source code - Setup the project property in
Project
menu andProject Options
->Parameters
tab - Paste the
C compiler
andLinker
arguments from above execution thepkg-config gtk4 --cflags
andpkg-config gtk4 --libs
acquire, must be one line in textbox - Create a new Compiler use
ucrt64
environments refere toTools
->Compiler options
->From folder to create compiler settings
button