Trying to compile GTK4 C code on Windows with Visual Studio 2022

Hello, I’m trying to compile my GTK4 C application on Windows using Visual Studio 2022. I built GTK4 using Collabora’s blog post and have now a c:\gnome directory. I setup my includes manually and needed to change all my dynamic array creations to <type> *array = g_new(<type>, size);. Now I’m facing an error that I don’t understand, maybe is it due to the fact that I missed some includes (I added includes directories and also lib/includes ones). This problem is that GFile is not handled, I have errorC2275 "GFile" : expecting expression instead of type, it’s on file dialogs and I’m using code from Gnome Tutorials :

static void exporteDepliage(GtkDialog* dialog, int response, gpointer data) {
  if (response == GTK_RESPONSE_ACCEPT) {
    DonneesDep* d = (DonneesDep*)data;
    GtkFileChooser* chooser = GTK_FILE_CHOOSER(dialog);
    g_autoptr(GFile) file = gtk_file_chooser_get_file(chooser);
    sauveDepliage(d, file);
  }

g_autoptr is recognized by the autocompletion and GFile too, so I don’t see what I can do, maybe do I need to change the syntax ?

But I’m maybe fighting in vain if from the start I’m not using the good way to compile my application with Visual Studio.

The g_autoptr() macro is not supported by MSVC, so you cannot use it with that toolchain. The GNOME examples assume you’re using GNOME, for obvious reasons, not Windows.

If you are using Windows, you can replace the use of g_autoptr() with an explicit g_object_unref() at the end of the scope:

static void exporteDepliage(GtkDialog* dialog, int response, gpointer data) {
  if (response == GTK_RESPONSE_ACCEPT) {
    DonneesDep* d = data; // No need to cast a `gpointer`
    GtkFileChooser* chooser = GTK_FILE_CHOOSER(dialog);
    GFile *file = gtk_file_chooser_get_file(chooser);
    sauveDepliage(d, file);
    g_object_unref (file);
  }

Thank you, Indeed I’m trying to make my code to be usable on both Linux (what do you call using GNOME ?) and Windows. Yesterday I built GTK4 using Collabor’s blog note that does it on MSVC and it worked (I run gtk-demo.exe and checked almost all the demos), or did I didn’t understand what it did ?

GTK by itself does not use g_autoptr() because it has to be built with MSVC—though we wish we didn’t have to choose; g_autoptr() is really useful.

The example code on the GNOME developers documentation website assumes you’re going to use the GNOME SDK, which comes with GCC and Clang, and both those compilers support the attribute used to implement the g_autoptr() macro: Miscellaneous Macros: GLib Reference Manual

Thanks for the explainations. I discovered GLib at the same time as GTK and it is really helpful. I’m only insisting on making my project cross-platform because I know that lots of Windows user would like to use it, but for myself only building it for Linux would have been enough, otherwise I would have made it using Builder that seems to be a very impressive tool.

I add this because I finally manage to have my code compiling with MSVC, I need to changes some of it because of deprecations, on Ubuntu I didn’t noticed that deprecations, so I must update my GTK4 there. I was thinking that updating to GNOME 43 would get me latest GTK4 but I was wrong. I’m starting to better understand how one need to do to make an app cross-platform.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.