User Tools

Site Tools


programming:gtk4

Gtk4

Gtk (source) is a GUI cross-platform toolkit.

Build from source

build_gtk4_notes.txt
# Install
sudo apt install ninja-build
sudo apt install meson # or `pip install meson` if too old
 
# You might find along the way you need some other dependencies
sudo apt install libgstreamer-plugins-bad1.0-dev
 
# Need to build Glib first:
# https://download.gnome.org/sources/glib/2.71/glib-2.71.2.tar.xz
# In glib2 source dir:
meson setup builddir
meson compile -c builddir
meson install -c builddir
 
# Need to build Glib-networking to get TLS
# https://download.gnome.org/sources/glib-networking/2.70/
# sudo apt install gsettings-desktop-schemas-dev
# sudo apt install libproxy-dev
# sudo apt install cmake
# In the glib-networking dir:
meson setup builddir
cd builddir
ninja
ninja install
 
# For GTK4, in Gtk4 source dir
# Get build options:
meson configure
 
# To build with `gtk_doc=true` option
pip install gi-docgen 
sudo apt install gir1.2-graphene-1.0 libgraphene-1.0-dev
 
# Omit prefix to let it install to install to system `/usr/local/`
meson setup -Dgtk_doc=true --prefix $HOME/gtk4 builddir
 
# Might need to install some deps like libjpeg,  or sudo apt install libgstreamer1.0-dev
 
cd builddir
ninja
 
# Documentation will now be in docs/
# For example: gtk-4.6.1/docs/reference/gtk/gtk4/index.html
 
# There are examples in source under examples/ directory 
 
# Install to prefix (use `sudo` if doing system install)
ninja install

Documentation

Offline

There is example code in the examples/ directory of the source. If you built from source and included the docs with the build, there will also be nice HTML pages in the docs/ directory.

  • API documentation - index.html - e.g. gtk-4.6.1/docs/reference/gtk/gtk4/index.html
  • Widget gallery: visual_index.html - e.g. gtk-4.6.1/docs/reference/gtk/gtk4/visual_index.html
  • Example code: examples/ directory - e.g. gtk-4.6.1/examples/

Online

Hello world

Example Makefile:

Makefile
all: main
 
main: main.c
	gcc `pkg-config --cflags gtk4` main.c `pkg-config --libs gtk4`
 
clean:
	rm a.out
main.c
// From https://www.gtk.org/
#include <gtk/gtk.h>
 
static void on_activate (GtkApplication *app) {
  // Create a new window
  GtkWidget *window = gtk_application_window_new (app);
  // Create a new button
  GtkWidget *button = gtk_button_new_with_label ("Hello, World!");
  // When the button is clicked, close the window passed as an argument
  g_signal_connect_swapped (button, "clicked", G_CALLBACK (gtk_window_close), window);
  gtk_window_set_child (GTK_WINDOW (window), button);
  gtk_window_present (GTK_WINDOW (window));
}
 
int main (int argc, char *argv[]) {
  // Create a new application
  GtkApplication *app = gtk_application_new ("com.example.GtkApplication",
                                             G_APPLICATION_FLAGS_NONE);
  g_signal_connect (app, "activate", G_CALLBACK (on_activate), NULL);
  return g_application_run (G_APPLICATION (app), argc, argv);
}

gtkmm (C++)

You can use gtkmm to write C++ code instead of C. On their wiki they have tutorials and build instructions for the source code.

You might need some other dependencies like:

  • sigc++-3.0
  • gtk4
  • glibmm-2.68
  • cairomm-1.16
  • pangomm-2.48

To build from source:

# Install mm-common
sudo apt install mm-common
 
# Build and install libsigc++3
# https://github.com/libsigcplusplus/libsigcplusplus
wget https://github.com/libsigcplusplus/libsigcplusplus/releases/download/3.2.0/libsigc++-3.2.0.tar.xz
tar xzf libsigc++-3.2.0
cd libsigc++-3.2.0
cmake .
./autogen.sh --prefix=/usr/local --enable-static
make
sudo make install
 
# glibmm
# https://gitlab.gnome.org/GNOME/glibmm
wget https://gitlab.gnome.org/GNOME/glibmm/-/archive/2.70.0/glibmm-2.70.0.tar.gz
tar xzf glibmm-2.70.0.tar.gz
cd glibmm-2.70.0
./autogen.sh --prefix=/usr/local --enable-static
make
sudo make install
 
# cairomm
# https://www.cairographics.org/releases/
wget https://www.cairographics.org/releases/cairomm-1.16.1.tar.xz
tar xBf cairomm-1.16.1.tar.xz
cd cairomm-1.16.1
./autogen.sh --prefix=/usr/local --enable-static
make
sudo make install
 
# pangomm
# https://gitlab.gnome.org/GNOME/pangomm
wget https://gitlab.gnome.org/GNOME/pangomm/-/archive/2.50.0/pangomm-2.50.0.tar.gz
tar xzf pangomm-2.50.0.tar.gz
cd pangomm-2.50.0
./autogen.sh --prefix=/usr/local --enable-static
make
sudo make install
 
 
# gtkmm
wget https://download.gnome.org/sources/gtkmm/4.6/gtkmm-4.6.0.tar.xz
tar xBf gtkmm-4.6.0.tar.xz
cd gtkmm-4.6.0
./autogen.sh --prefix=/usr/local --enable-static
make
sudo make install

When compiling, use -std=c++20 flag. When running, you may need to set LD_LIBRARY_PATH if you installed to a special prefix. Maybe able to add -static if you built all libs with –enable-static' configuration and a .a'' file is in the lib dir.

# Compile app with gtk4
g++ main.cpp `pkg-config gtkmm-4.0 --cflags --libs` -std=c++20
 
# Set path for shared library when running
LD_LIBRARY_PATH=/usr/local/lib ./a.out

Here is a gtkmm hello world:

main.cpp
// gtkmm4 example
 
// Compile with:
// g++ main.cpp `pkg-config gtkmm-4.0 --cflags --libs` -std=c++20
 
// Run with
// LD_LIBRARY_PATH=/usr/local/lib ./a.out
#include <gtkmm.h>
 
class MyWindow : public Gtk::Window
{
public:
  MyWindow();
};
 
MyWindow::MyWindow()
{
  set_title("Basic application");
  set_default_size(200, 200);
}
 
int main(int argc, char* argv[])
{
  auto app = Gtk::Application::create("org.gtkmm.examples.base");
 
  return app->make_window_and_run<MyWindow>(argc, argv);
}
programming/gtk4.txt · Last modified: 2022/03/21 05:19 by nanodano