Skip to content

GTK 4 Library Setup

VerzatileDev edited this page Jan 14, 2024 · 4 revisions

How to setup GTK 4 Library within the project:

Download Msys2 Terminal https://www.msys2.org/

Step 1: in Msys2 terminal, write:

pacman -S mingw-w64-x86_64-gtk4

Step 2:

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

Step 3:

C Project codebase (New Project/ Existing.

Step 4:

In main.c paste the following (Right-click, Paste) ->

#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_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;
}

Step 5:

Open c_cpp_proprties.json if you don't have one make it!

Step 6: Add the exact promt, make sure that the root directory is as you installed MSYS2 (Otherwise change directory pathing). Add ->

{
  "configurations": [
      {
          "name": "Win32",
          "includePath": [
              "${workspaceFolder}/**",
              "C:/msys64/mingw64/include/gtk-4.0",
              "C:/msys64/mingw64/include/glib-2.0",
              "C:/msys64/mingw64/include/**",
              "C:/msys64/mingw64/lib/**"
          ],
          "defines": [
              "_DEBUG",
              "UNICODE",
              "_UNICODE"
          ],
          "windowsSdkVersion": "10.0.22000.0",
          "compilerPath": "C:/msys64/mingw64/bin/gcc.exe",
          "cStandard": "c17",
          "cppStandard": "c++17",
          "intelliSenseMode": "windows-gcc-x64"
      }
  ],
  "version": 4
}

Step 7: Add the following to windows "Edit environment settings” Under System Environment add ->

-> PATH -> Edit -> new

C:\msys64\mingw64\bin

<- Add the path to the directory it was installed to by default it is (As written), though if you change it remember to Change it in Tasks & c_cpp_properties.json where required.

Add the bin folder that you downloaded ‘mingw64’ to and the gtk4 will be there as well

Make sure that the bin path to the mingw64 is right after Program Files or it might not work!

Step 8: Add tasks.json

{
 "version": "2.0.0",
 "tasks": [
  {
   "type": "cppbuild",
   "label": "C/C++: gcc.exe build active file",
   "command": "C:/msys64/mingw64/bin/gcc.exe",
   "args": [
    "-fdiagnostics-color=always",
    "-g",
    "-IC:/msys64/mingw64/include/gtk-4.0",
    "-IC:/msys64/mingw64/include/pango-1.0",
    "-IC:/msys64/mingw64/include",
    "-IC:/msys64/mingw64/include/glib-2.0",
    "-IC:/msys64/mingw64/lib/glib-2.0/include",
    "-IC:/msys64/mingw64/include/harfbuzz",
    "-IC:/msys64/mingw64/include/freetype2",
    "-IC:/msys64/mingw64/include/libpng16",
    "-IC:/msys64/mingw64/include/fribidi",
    "-IC:/msys64/mingw64/include/cairo",
    "-IC:/msys64/mingw64/include/pixman-1",
    "-IC:/msys64/mingw64/include/gdk-pixbuf-2.0",
    "-IC:/msys64/mingw64/include/webp",
    "-DLIBDEFLATE_DLL",
    "-IC:/msys64/mingw64/include/graphene-1.0",
    "-IC:/msys64/mingw64/lib/graphene-1.0/include",
    "-mfpmath=sse",
    "-msse",
    "-msse2",

    "${file}",

    "-LC:/msys64/mingw64/lib",
    "-lgtk-4",
    "-lpangowin32-1.0",
    "-lpangocairo-1.0",
    "-lpango-1.0",
    "-lharfbuzz",
    "-lgdk_pixbuf-2.0",
    "-lcairo-gobject",
    "-lcairo",
    "-lgraphene-1.0",
    "-lgio-2.0",
    "-lgobject-2.0",
    "-lglib-2.0",
    "-lintl",

    "-o",
    "${fileDirname}\\${fileBasenameNoExtension}.exe"
   ],
   "options": {
    "cwd": "C:/msys64/mingw64/bin"
   },
   "problemMatcher": [
    "$gcc"
   ],
   "group": "build",
   "detail": "compiler: C:/msys64/mingw64/bin/gcc.exe"
  }
 ]
} 
{
 "version": "2.0.0",
 "tasks": [
  {
   "type": "cppbuild",
   "label": "C/C++: gcc.exe build active file",
   "command": "C:/msys64/mingw64/bin/gcc.exe",
   "args": [
    "-fdiagnostics-color=always",
    "-g",
    "-IC:/msys64/mingw64/include/gtk-4.0",
    "-IC:/msys64/mingw64/include/pango-1.0",
    "-IC:/msys64/mingw64/include",
    "-IC:/msys64/mingw64/include/glib-2.0",
    "-IC:/msys64/mingw64/lib/glib-2.0/include",
    "-IC:/msys64/mingw64/include/harfbuzz",
    "-IC:/msys64/mingw64/include/freetype2",
    "-IC:/msys64/mingw64/include/libpng16",
    "-IC:/msys64/mingw64/include/fribidi",
    "-IC:/msys64/mingw64/include/cairo",
    "-IC:/msys64/mingw64/include/pixman-1",
    "-IC:/msys64/mingw64/include/gdk-pixbuf-2.0",
    "-IC:/msys64/mingw64/include/webp",
    "-DLIBDEFLATE_DLL",
    "-IC:/msys64/mingw64/include/graphene-1.0",
    "-IC:/msys64/mingw64/lib/graphene-1.0/include",
    "-mfpmath=sse",
    "-msse",
    "-msse2",

    "${file}",

    "-LC:/msys64/mingw64/lib",
    "-lgtk-4",
    "-lpangowin32-1.0",
    "-lpangocairo-1.0",
    "-lpango-1.0",
    "-lharfbuzz",
    "-lgdk_pixbuf-2.0",
    "-lcairo-gobject",
    "-lcairo",
    "-lgraphene-1.0",
    "-lgio-2.0",
    "-lgobject-2.0",
    "-lglib-2.0",
    "-lintl",

    "-o",
    "${fileDirname}\\${fileBasenameNoExtension}.exe"
   ],
   "options": {
    "cwd": "C:/msys64/mingw64/bin"
   },
   "problemMatcher": [
    "$gcc"
   ],
   "group": "build",
   "detail": "compiler: C:/msys64/mingw64/bin/gcc.exe"
  }
 ]
} 

Resources Used:


Setup GTK 4 Cmake / without Cmake

https://www.youtube.com/watch?v=TyFXwHklg6A

GTK commands to download libraries through msys2

https://www.gtk.org/docs/installations/windows/

Another way to Download GTK 4 for windows (Don't use this one, but might work as well)

https://www.youtube.com/watch?v=78j-Gvx2MP0

Code and instructions for mingw Installation

https://code.visualstudio.com/docs/cpp/config-mingw

Edit Environment variables (This is so you can setup the Bin folder for mingw etc)

https://docs.oracle.com/en/database/oracle/machine-learning/oml4r/1.5.1/oread/creating-and-modifying-environment-variables-on-windows.html#GUID-DD6F9982-60D5-48F6-8270-A27EC53807D0

Some GTK instructions and usage

https://www.collabora.com/news-and-blog/blog/2021/03/18/build-and-run-gtk-4-applications-with-visual-studio/

Clone this wiki locally