Ubuntu – How to install latest GTK for creating C programs

cgtk3software installation

I want to install latest version GTK to make small c programs. I've just finished "let us c" book which is very basic.Now I want to make GUI programs.while compiling the c program I got the following error by gcc-7 test.c

   test.c:1:10: fatal error: gtk/gtk.h: No such file or directory
   #include <gtk/gtk.h>
             ^~~~~~~~~~~

Thus I want to install latest version GTK to make simple GUI programs. I had gone to there website but there were so may files to download of which I don't know which to download. I want the latest version. so that my gtk would get upgraded when I typed apt update && apt upgrade

I am using Xubuntu

Here is the source code from Getting Started with GTK+.

#include 

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

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

  app = gtk_application_new ("org.gtk.example", G_APPLICATION_FLAGS_NONE);
  g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
  status = g_application_run (G_APPLICATION (app), argc, argv);
  g_object_unref (app);

  return status;
}

Best Answer

You are missing a dependency required to run this program. Open the terminal and type:

sudo apt install libgtk-3-dev

Then compile the source code test.c with the following command:

gcc-5 `pkg-config --cflags gtk+-3.0` -o test test.c `pkg-config --libs gtk+-3.0`  

I used gcc-5 instead of gcc-7 in Ubuntu 16.0-4 and test.c compiled successfully. In the upcoming release of Ubuntu 17.10 the gcc-7 package will be included in the default Ubuntu repositories and it will be possible to install gcc-7 quickly and easily with apt instead of gcc-5 which I have installed with apt in 16.04. In Ubuntu 18.04 GCC 7 is the default version of GCC and gcc-8-base can be installed from the default Ubuntu 18.04 repositories.

Then run the program with this command:

./test  

and the results will be that an empty 200 × 200 pixel window with the title Window will appear which you can resize or close by clicking on the X. The window looks similar to the window in the Getting Started with GTK+ tutorial except that it has the default theme for your operating system.

Related Question