We're hiring!
*

Build and run GTK 4 applications with Visual Studio

Xavier Claessens avatar

Xavier Claessens
March 18, 2021

Share this post:

Over the past few months, I've been working on a side project to improve Meson sub-project support. The best stress test is to build projects on Windows, without msys2, because it has no dependencies or tools installed (e.g. pkg-config). Meson has to build everything itself from scratch.

Here are step-by-step instructions, starting from a fresh Windows 10 installation, on how to run a gtk4-demo application.

Install Visual Studio

Download from https://visualstudio.microsoft.com/vs/community. Make sure to check the box "Desktop development with C++".

Collabora - Installing Visual Studio

Install Python >= 3.6

Download from https://www.python.org/downloads. Make sure to check the box "Add python to PATH".

Collabora - Installing Python

Install Git for Windows

Download from https://gitforwindows.org. It asks many questions, however the default is always good enough, so next, next, next...

Collabora - Installing Git for Windows

Open developer terminal

From the Windows start menu, search for x64 Native Tools Command Prompt for VS 2019. That will open a terminal configured to use MSVC x64 tools.

Collabora - Visual Studio Developer Command Prompt

First let's create a directory where we'll fetch the source code:

mkdir C:\src
cd C:\src

Let's also configure git:

git config --global user.email "xavier.claessens@collabora.com"
git config --global user.name "Xavier Claessens"

Install Meson

Installing the latest release of Meson (requires >=0.57.0) can be done with a simple command:

C:\src>pip install meson

Fetch GTK4 source

Fetch gtk from git:

C:\src>git clone https://gitlab.gnome.org/GNOME/gtk.git
C:\src>cd gtk

At the time of writing !3135 is not yet merged, so we need to use my branch:

(Update March 22: This has now been merged so my branch is no longer needed.)

C:\src\gtk>git fetch https://gitlab.gnome.org/xclaesse/gtk.git meson-install-script
C:\src\gtk>git checkout -b meson-install-script FETCH_HEAD

Build GTK4 with all its dependencies

During the GTK4 configuration, Meson will download and configure all dependencies. Great efforts have been made across all dependencies to make them work out of the box on Windows.

C:\src\gtk>meson setup build --prefix C:/gnome
C:\src\gtk>meson compile -C build
C:\src\gtk>meson install -C build

Collabora - GTK4 configuration

Run gtk4-demo

That's all, you can now run demo applications!

C:\src\gtk>C:\gnome\bin\gtk4-demo.exe

Collabora - Running gtk4-demo.exe

It works out of the box on my VirtualBox machine, however on my physical PC with an intel GPU, I get a crash at startup:

Unhandled exception at 0x5211345E (ig4icd32.dll) in gtk4-demo.exe: 0xC0000005: Access violation reading location 0x00000050

This can be worked around by using cairo rendering:

C:\src\gtk>set GSK_RENDERER=cairo
C:\src\gtk>C:\gnome\bin\gtk4-demo.exe

Debug in Visual Studio

Since this is a complete build using MSVC, it is also fully debuggable.

  • Open Visual Studio
  • Click "Open a project or solution"
  • Browse to C:\gnome\bin\gtk4-demo.exe
  • Click "Start" button

If you break execution, or hit a crash, it will open the source code and you can debug step by step, see the value of any variable, whether it is in gtk or in any of its dependencies.

Debugging in Visual Studio.

What's next?

Here's a list of some of the items I would like to eventually look into:

  • See if we can use GTK4 as sub-project of a real application. Which applications do we have that are ported to GTK4 already and runs on Windows? Gedit?
  • Add GStreamer gst-build as a sub-project of GTK4 to build the media player? This would probably lead to a world record of number of Meson subprojects built at once.
  • Add Windows CI to GTK4?

As usual, if you have any questions about GTK4 or GStreamer on Windows, or any other platform, please contact us!

Comments (5)

  1. Lindomar:
    Mar 19, 2021 at 07:23 PM

    Is it possible to use GTK4 in Python 3.9 on Windows?

    Reply to this comment

    Reply to this comment

    1. Xavier Claessens:
      Mar 22, 2021 at 12:38 PM

      With this tutorial it is not building gobject-introspection bindings. It should be possible to add g-i as a subproject and set GI_TYPELIB_PATH but I haven't tried it yet.

      Reply to this comment

      Reply to this comment

  2. Christian Ohrfandl:
    Mar 20, 2021 at 12:12 AM

    Hi,

    thank you for the tutorial!

    However, I face the following issues when running the command "meson compile -C build" (multiple times):

    FAILED: subprojects/freetype2/freetype-6.dll.p/src_gzip_ftgzip.c.obj
    "cl" "-Isubprojects\freetype2\freetype-6.dll.p" "-Isubprojects\freetype2" "-I..\subprojects\freetype2" "-I..\subprojects\freetype2\include" "-IC:/dev/StrawberryPerl/c/lib/pkgconfig/../../include/libpng16" "/MD" "/nologo" "/showIncludes" "/W2" "/O2" "/Zi" "-DFT2_BUILD_LIBRARY" "-DFT_CONFIG_CONFIG_H=" "-DFT_CONFIG_OPTIONS_H=" "-DDLL_EXPORT" "-D_CRT_SECURE_NO_WARNINGS" "-D_CRT_NONSTDC_NO_WARNINGS" "/Fdsubprojects\freetype2\freetype-6.dll.p\src_gzip_ftgzip.c.pdb" /Fosubprojects/freetype2/freetype-6.dll.p/src_gzip_ftgzip.c.obj "/c" ../subprojects/freetype2/src/gzip/ftgzip.c
    ../subprojects/freetype2/src/gzip/ftgzip.c(46): fatal error C1083: Datei (Include) kann nicht geöffnet werden: "zlib.h": No such file or directory
    [93/3580] Compiling C object subprojects/freetype2/freetype-6.dll.p/src_psaux_psaux.c.obj
    ninja: build stopped: subcommand failed.

    FAILED: subprojects/freetype2/freetype-6.dll.p/src_gzip_ftgzip.c.obj
    "cl" "-Isubprojects\freetype2\freetype-6.dll.p" "-Isubprojects\freetype2" "-I..\subprojects\freetype2" "-I..\subprojects\freetype2\include" "-IC:/dev/StrawberryPerl/c/lib/pkgconfig/../../include/libpng16" "/MD" "/nologo" "/showIncludes" "/W2" "/O2" "/Zi" "-DFT2_BUILD_LIBRARY" "-DFT_CONFIG_CONFIG_H=" "-DFT_CONFIG_OPTIONS_H=" "-DDLL_EXPORT" "-D_CRT_SECURE_NO_WARNINGS" "-D_CRT_NONSTDC_NO_WARNINGS" "/Fdsubprojects\freetype2\freetype-6.dll.p\src_gzip_ftgzip.c.pdb" /Fosubprojects/freetype2/freetype-6.dll.p/src_gzip_ftgzip.c.obj "/c" ../subprojects/freetype2/src/gzip/ftgzip.c
    ../subprojects/freetype2/src/gzip/ftgzip.c(46): fatal error C1083: Datei (Include) kann nicht geöffnet werden: "zlib.h": No such file or directory

    FAILED: subprojects/glib/gobject/gobject-2.0-0.dll.p/gclosure.c.obj
    "cl" "-Isubprojects\glib\gobject\gobject-2.0-0.dll.p" "-Isubprojects\glib\gobject" "-I..\subprojects\glib\gobject" "-Isubprojects\glib" "-I..\subprojects\glib" "-Isubprojects\glib\glib" "-I..\subprojects\glib\glib" "-Isubprojects\proxy-libintl" "-I..\subprojects\proxy-libintl" "/MD" "/nologo" "/showIncludes" "/W2" "/O2" "/Zi" "/wd4035" "/wd4715" "/wd4116" "/wd4046" "/wd4068" "/wo4090" "/FImsvc_recommended_pragmas.h" "/utf-8" "-D_GNU_SOURCE" "-DG_ENABLE_DEBUG" "-DG_LOG_DOMAIN=\"GLib-GObject\"" "-DGOBJECT_COMPILATION" "/Fdsubprojects\glib\gobject\gobject-2.0-0.dll.p\gclosure.c.pdb" /Fosubprojects/glib/gobject/gobject-2.0-0.dll.p/gclosure.c.obj "/c" ../subprojects/glib/gobject/gclosure.c
    ../subprojects/glib/gobject/gclosure.c(28): fatal error C1083: Datei (Include) kann nicht geöffnet werden: "ffi.h": No such file or directory

    It is interesting though, e.g. zlib.h is located in the place where it should reside. However, ffi.h is not there...

    Anyway, here you can find the whole log from compilation: https://pastebin.com/T0QRUZya

    Do you have any suggestions?

    Cheers,
    Christian

    Reply to this comment

    Reply to this comment

    1. LINDOMAR BEGA:
      Mar 20, 2021 at 01:00 AM

      The tutorial works perfectly, but no commands can be left. I had a problem compiling twice and since I was not an expert I remade everything.

      Reply to this comment

      Reply to this comment

    2. Xavier Claessens:
      Mar 22, 2021 at 12:43 PM

      Check your logs to see if it has setup zlib subproject, or if it found it on your system somewhere. Could you attach your /meson-logs/meson-logs.txt for more info, please?

      Reply to this comment

      Reply to this comment


Add a Comment






Allowed tags: <b><i><br>Add a new comment:


Search the newsroom

Latest Blog Posts

Quick hack: Patching kernel modules using DKMS

05/05/2021

DKMS is a framework that is mostly used to build and install external kernel modules. It can also be used to install a specific patch to…

Build your own application with GTK 4 as a Meson subproject!

29/04/2021

Building GTK 4 as a Meson subproject for your own application is not only useful for Windows builds, but also for many Linux distributions…

Profiling virtualized GPU acceleration with Perfetto

22/04/2021

Recently, we have been using Perfetto to successfully profile Apitrace traces in crosvm through VirGL renderer. We have now added perfetto…

Continuous 3D Hand Pose Tracking using Machine Learning & Monado OpenXR

20/04/2021

As part of a project backed by INVEST-AI, a program managed by IVADO Labs, we have developed a multi-stage neural network-based solution…

An easy to use MTP implementation for your next embedded Linux project

13/04/2021

Did you know you could run a permissively-licensed MTP implementation with minimal dependencies on an embedded device? Here's a step-by-step…

Rust: integrating LLVM source-base code coverage with GitLab

24/03/2021

Earlier this year, the Rust compiler gained support for LLVM source-base code coverage. In this post we'll explain how to setup a CI job…

Open Since 2005 logo

We use cookies on this website to ensure that you get the best experience. By continuing to use this website you are consenting to the use of these cookies. To find out more please follow this link.

Collabora Ltd © 2005-2021. All rights reserved. Privacy Notice. Sitemap.

Collabora Limited is registered in England and Wales. Company Registration number: 5513718. Registered office: The Platinum Building, St John's Innovation Park, Cambridge, CB4 0DS, United Kingdom. VAT number: 874 1630 19.