1

On Linux Mint 20.1 i've installed the package libgtk-3.0-dev using the command:

sudo apt install libgtk-3.0-dev

The installation was successful but when I try to compile a simple example:

 // Include gtk
#include <gtk/gtk.h>

static void on_activate (GtkApplication *app) {
  // Create a new window
  GtkWidget *window = gtk_application_window_new (app);
  // Create a new button
  GtkWidget *button = gtk_button_new_with_label ("Hello, World!");
  // When the button is clicked, close the window passed as an argument
  g_signal_connect_swapped (button, "clicked", G_CALLBACK (gtk_window_close), window);
  gtk_window_set_child (GTK_WINDOW (window), button);
  gtk_window_present (GTK_WINDOW (window));
}

int main (int argc, char *argv[]) {
  // Create a new application
  GtkApplication *app = gtk_application_new("com.example.GtkApplication", G_APPLICATION_FLAGS_NONE);
  g_signal_connect (app, "activate", G_CALLBACK (on_activate), NULL);
  return g_application_run (G_APPLICATION (app), argc, argv);
}

But the compiler gives me an error that it can't find the header files. I'm probably missing here something. Anybody, can you guide me how to solve the problem?

I compile using the following command:

  gcc -I/usr/include/gtk-3.0 -I/usr/include/glib-2.0  gtk_app.c

The error I get is:

In file included from /usr/include/glib-2.0/glib/galloca.h:32,
                 from /usr/include/glib-2.0/glib.h:30,
                 from /usr/include/gtk-3.0/gdk/gdkconfig.h:13,
                 from /usr/include/gtk-3.0/gdk/gdk.h:30,
                 from /usr/include/gtk-3.0/gtk/gtk.h:30,
                 from gtk_app.c:2:
/usr/include/glib-2.0/glib/gtypes.h:32:10: fatal error: glibconfig.h: No such file or directory
   32 | #include <glibconfig.h>

1 Answers1

1

You must provide path information for headers and libraries to your compiler.

For compiling this can be done via options (including the back ticks)

`pkg-config --cflags gtk+-3.0`

For linking you can add

`pkg-config --libs gtk+-3.0`

From your error message it seems your problem is not GTK but GLIB. You should add the same commands for package glib-2.0 as well.

Gerhardh
  • 11,688
  • 4
  • 17
  • 39
  • `pkg-config --cflags gtk+-3.0` This command gives me a lot (tens) of include directories. Is that correct? – Horus Coming Feb 22 '21 at 14:28
  • Yes, that's a rather lengthy list. – Gerhardh Feb 22 '21 at 14:29
  • It's somewhat a step forward, but now, I get a link error: `undefined reference to gtk_window_set_child`. I'm compiling using a command: gcc gtk_app.c `pkg-config --cflags gtk+-3.0` `pkg-config --libs gtk+-3.0` `pkg-config --cflags glib-2.0` `pkg-config --libs glib-2.0` – Horus Coming Feb 23 '21 at 13:12
  • 1
    Ah sorry.. my bad, I've been using example for GTK version 4. With the version 3 example, all works fine. Thanks! – Horus Coming Feb 23 '21 at 13:17