Horus Coming
Horus Coming

Reputation: 13

How to include properly a gtk.h file for GTK linux development?

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>

Upvotes: 1

Views: 1467

Answers (1)

Gerhardh
Gerhardh

Reputation: 12404

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.

Upvotes: 1

Related Questions