ToshioCP / Gtk4-tutorial

GTK 4 tutorial for beginners
https://toshiocp.github.io/Gtk4-tutorial/
548 stars 50 forks source link

error: ‘G_APPLICATION_DEFAULT_FLAGS’ was not declared in this scope #39

Closed goluckyryan closed 1 year ago

goluckyryan commented 1 year ago

Hi Toshio,

My system is ubuntu22.04. I installed libgtk-4-dev.

And I copy the code for a blank window and save it to test.cpp

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

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

GtkApplication * 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; }

compile with g++ pkg-config --cflags gtk4 -o test test.cpp pkg-config --libs gtk4

I got an error say:

error: ‘G_APPLICATION_DEFAULT_FLAGS’ was not declared in this scope; did you mean ‘G_APPLICATION_GET_CLASS’? 17 | GtkApplication * app = gtk_application_new ("org.gtk.example", G_APPLICATION_DEFAULT_FLAGS); | ^~~~~~~ | G_APPLICATION_GET_CLASS

I am sorry to brother you with this. but I cannot google the solution. Please help.

Best regards, Ryan

ToshioCP commented 1 year ago

Thank you for the post, Ryan.

Ubuntu 22.04 has GLib-2.0 version 2.72. G_APPLICATION_DEFAULT_FLAGS is a new flag since GLib-2.0 version 2.74. So, the system issued an error.

In that case, there are two solutions.

  1. Use G_APPLICATION_FLAGS_NONE instead of G_APPLICATION_DEFAULT_FLAGS. It is the old flag replaced by G_APPLICATION_DEFAULT_FLAGS. And it is also possible with GLib2.0 version 2.74. But it is deprecated flag in the new version and it will be removed in the future version. So, when you update your system and your GLib becomes version 2.74 or after, you should use the new flag.
  2. Update your Ubuntu to 22.10 (kinetic). It includes GLib2.0 version 2.74. However, it is your choice. Because Ubuntu 22.04 is LTS (Long Term Support). On the other hand, Ubuntu 22.10 is NOT LTS and its supprot term is 9 months. Maybe 22.04 is more stable than 22.10.

Please try one of them. Thank you again, and feel free to post issues. Any question is welcome.

Toshio Sekiya

ToshioCP commented 1 year ago

The solution above is written in the section 3. So, this issue is closed.