Thursday, June 16, 2016

Gtk - How to Create Simple Window, Compile, and Run GTK+

In previous tutorial, Knowledge hub already guided step by step of how to install GTK+ library in window. Today, Knowledge hub will provide another tutorial of how to create simple window with GTK+ libary.

Moreover, Knowledge hub will explain how to compile and run GTK+ library with MinGW C Compiler. Mingw C Compiler is a compiler used to compile C program.

  • It is assumed that you already have Mingw installed on your machine
  • To Compile program MinGW in Command line, using the following command
gcc yourfilename.c -o yourprogramfile.exe
  • After that, we can run exe file that generated by compiler with the above command.
.\yourprogramfile.exe
  • But, in order to Compile GTK+ library, we need to add more command to link libraries and include path where the compiler can find those library
  • How to know which path should be included and which library should be linked?
  • *In order to know those command, we need to type the following command in command line
pkg-config --cflags --libs gtk+-3.0
  • After that, please Copy the result getting from above command and save it Notepad. Because, you will need it to compile your gtk+ library
  • Note: The result obtaining from the above command may vary depending on where you place your GTK+ library

  • After that, creating mainwindow.c and type the following code to that file and save it.
//mainwindow.c

#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), 
                        "Hello http://knowledgehubkh.blogspot.com/");
  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;
}


  • Open Command line and using cd command to go to where you locate your mainwindow.c file
  • Then, type the following command to compile mainwindow.c
gcc mainwindow.c -o mainwindow.exe -mms-bitfields -ID:/Libraries/gtk/include/gtk-3.0 -ID:/Libraries/gtk/include/cairo -ID:/Libraries/gtk/include/pango-1.0 -ID:/Libraries/gtk/include/atk-1.0 -ID:/Libraries/gtk/include/cairo -ID:/Libraries/gtk/include/pixman-1 -ID:/Libraries/gtk/include -ID:/Libraries/gtk/include/freetype2 -ID:/Libraries/gtk/include -ID:/Libraries/gtk/include/libpng15 -ID:/Libraries/gtk/include/gdk-pixbuf-2.0 -ID:/Libraries/gtk/include/libpng15 -ID:/Libraries/gtk/include/glib-2.0 -ID:/Libraries/gtk/lib/glib-2.0/include -LD:/Libraries/gtk/lib -lgtk-3 -lgdk-3 -lgdi32 -limm32 -lshell32 -lole32 -Wl,-luuid -lpangocairo-1.0 -lpangoft2-1.0 -lfreetype -lfontconfig -lpangowin32-1.0 -lgdi32 -lpango-1.0 -lm -latk-1.0 -lcairo-gobject -lcairo -lgdk_pixbuf-2.0 -lgio-2.0 -lgobject-2.0 -lglib-2.0 -lintl
  • Note: The command from -mms-bitfields ..... until the end (-lintl). Please replace it with the result you got from the step with asterisk (*) above

  • If there is no error, you can type the below command to see your simple window. You will see the window as shown in the following figure.
.\mainwindow.exe


No comments:

Post a Comment