GTK+- 3.x/4.x + Glade + MSYS2 安装配置
1.install MSYS2
Download the installer: msys2-x86_64-20221216.exe
modify the pacman config with tsinghua
sed -i "s#mirror.msys2.org/#mirrors.tuna.tsinghua.edu.cn/msys2/#g" /etc/pacman.d/mirrorlist*
2.install GCC and GTK+ - 3.x/4.x
$ pacman -S mingw-w64-x86_64-gcc
$ pacman -S mingw-w64-x86_64-gtk4 #install Gtk+-4.x or
$ pacman -S mingw-w64-x86_64-gtk3 #install Gtk+-3.x
3.configure .bashrc
export PATH=/../../mingw64/bin:/../../mingw64/share:$PATH
export PKG_CONFIG_PATH=/../../mingw64/lib/pkgconfig:$PKG_CONFIG_PATH
4.Hello World App in C
To begin our introduction to GTK, we’ll start with a simple Hello World
GTK application.
Create a new file with the following content named 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;
//Gtk3.0
//app = gtk_application_new ("org.gtk.example", G_APPLICATION_FLAGS_NONE);
//Gtk4.0
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;
}
You can compile the program above with GCC using:
#compiling gtk+-4.x source code
$ gcc $(pkg-config --cflags gtk4) -o hello-world-gtk hello-world-gtk.c $(pkg-config --libs gtk4)
# OR
# compiling gtk+-3.x source code like this
$ gcc $(pkg-config --cflags gtk+-3.0) -o hello-world-gtk hello-world-gtk.c $(pkg-config --libs gtk+-3.0)
5.install Glade
a RAD tool to enable quick & easy development of user interfaces for the GTK toolkit and the GNOME desktop environment.
$ pacman -S mingw-w64-x86_64-glade
6. run gtk-demo
$ gtk4-demo