Skip to content

Instantly share code, notes, and snippets.

@arbalest
Last active February 22, 2021 04:13
Show Gist options
  • Save arbalest/cc75a0ee03a50917f86cccb426e42203 to your computer and use it in GitHub Desktop.
Save arbalest/cc75a0ee03a50917f86cccb426e42203 to your computer and use it in GitHub Desktop.
Notes and instructions for installing and packaging a GTK+ 3 / Vala app on Windows

GTK+ 3 / Vala Windows Instructions

Description

This will primarily focus on building a somewhat portable dev environment for developing GTK+ 3 apps using Vala, targeting Windows.

Requirements

  1. Download the .tar.xz archive of MSYS2 from MSYS2 Installation
  2. Extract the archive and open "msys2.exe"
  3. Close and re-open if prompted
  4. Run pacman -Syuu to update. Close and re-open if prompted
  5. Run pacman -S mingw-w64-x86_64-gtk3 to install GTK+3 and its dependencies
  6. Run pacman -S mingw-w64-x86_64-toolchain base-devel to download other dev tools as needed (gcc, pkgconfig needed at a minium, installing all is simplest)
  7. Run pacman -S mingw-w64-x86_64-vala to download and install valac

References

Compiling a Simple App in C

First, try the tools out using a basic C app and gcc.

Given the following simple app (as example.c):

#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_all (window);
}

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

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

  return status;
}

Run the following to compile:

gcc `pkg-config --cflags gtk+-3.0` -o example-0 example-0.c `pkg-config --libs gtk+-3.0`

Note the backticks ("`") used.

References

Compiling a Simple App in Vala

To try out an app in Vala, given the following in example.vala:

using Gtk;

int main (string[] args) {
    Gtk.init (ref args);

    var window = new Window ();
    window.title = "First GTK+ Program";
    window.border_width = 10;
    window.window_position = WindowPosition.CENTER;
    window.set_default_size (350, 70);
    window.destroy.connect (Gtk.main_quit);

    var button = new Button.with_label ("Click me!");
    button.clicked.connect (() => {
        button.label = "Thank you";
    });

    window.add (button);
    window.show_all ();

    Gtk.main ();
    return 0;
}

Run the following to compile:

valac --pkg gtk+-3.0 gtk-hello.vala

Optionally, pass the -mwindows flag to gcc to keep the Windows cmd prompt from appearing when running the generated exe:

valac --pkg gtk+-3.0 --cc=gcc --Xcc=-mwindows gtk-hello.vala

Resources

Adding a Window Icon

Assuming an icon file named "icon.png" in the same folder as the source code, add the following code to the window:

window.icon = new Gdk.Pixbuf.from_file("icon.png");

Adding a Windows App Icon

First, convert your icon image to a Windows .ico file. Then, create a resources.rc file in the following format:

1 ICON icon.ico

...where "icon.ico" is the icon file to use. Note the leading "1" on the line.

Next, use gcc to compile the resource file:

windres resource.rc resource.o

Finally, link the compiled resource file when compiling the Vala program:

valac --pkg gtk+-3.0 --cc=gcc --Xcc=resource.o example.vala

Creating a Distributable Package

Copy the following DLLs from msys64\mingw64\bin, and place them in the root of the package folder:

"libatk-1.0-0.dll",
"libbz2-1.dll",
"libcairo-2.dll",
"libcairo-gobject-2.dll",
"libcairo-script-interpreter-2.dll",
"libcroco-0.6-3.dll",
"libepoxy-0.dll",
"libexpat-1.dll",
"libffi-6.dll",
"libfontconfig-1.dll",
"libfreetype-6.dll",
"libgcc_s_seh-1.dll",
"libgdk_pixbuf-2.0-0.dll",
"libgdk-3-0.dll",
"libgio-2.0-0.dll",
"libglib-2.0-0.dll",
"libgmodule-2.0-0.dll",
"libgobject-2.0-0.dll",
"libgraphite2.dll",
"libgthread-2.0-0.dll",
"libgtk-3-0.dll",
"libharfbuzz-0.dll",
"libharfbuzz-gobject-0.dll",
"libharfbuzz-icu-0.dll",
"libiconv-2.dll",
"libintl-8.dll",
"libjasper-1.dll",
"libjpeg-8.dll",
"liblzma-5.dll",
"libpango-1.0-0.dll",
"libpangocairo-1.0-0.dll",
"libpangoft2-1.0-0.dll",
"libpangowin32-1.0-0.dll",
"libpcre-1.dll",
"libpcre16-0.dll",
"libpcre32-0.dll",
"libpcrecpp-0.dll",
"libpcreposix-0.dll",
"libpixman-1-0.dll",
"libpng16-16.dll",
"librsvg-2-2.dll",
"libstdc++-6.dll",
"libvala-0.34-0.dll",
"libwinpthread-1.dll",
"libxml2-2.dll",
"ssleay32.dll",
"zlib1.dll"

Copy the following additional files/folders from msys64\mingw64\, placing them in the root of the package folder:

"share/icons/Adwaita",
"share/icons/hicolor"

Finally, copy the compiled exe file into the root of the package folder, giving a folder consisting of something like the following:

|- app.exe
|- share
	|- icons
	      |- Adwaita
	            |- index.theme
	            |- 8x8
	            |- [...]
	      |- hicolor
	            |- index.theme
	            |- 8x8
	            |- [...]
|- libatk-1.0-0.dll
|- libbz2-1.dll
|- [...]
|- zlib1.dll

Double-clicking the .exe file to run it should now work.

Additional Resources

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment