Get MinGW Cross Compiler at Fast, secure and Free Open Source software downloads

Hello World program using GTK+

Simple "Hello World" Progam

This sample is copied from Hello World in GTK in GTK+ 2.0 Tutorial. Note: All comments in the source are deleted.

List : hello_gtk.c

#include <gtk/gtk.h> 

static void 
hello (GtkWidget * widget, gpointer data) 
  g_print ("Hello World!\n"); 

static gboolean 
delete_event (GtkWidget * widget, GdkEvent * event, gpointer data) 
  g_print ("delete event occurred\n");
  return TRUE; 

static void 
destroy (GtkWidget * widget, gpointer data) 
  gtk_main_quit (); 
main (int argc, char *argv[]) 
  GtkWidget *window; 
  GtkWidget *button; 

  gtk_init (&argc, &argv); 
  window = gtk_window_new (GTK_WINDOW_TOPLEVEL); 

  g_signal_connect (G_OBJECT (window), "delete_event", 
                    G_CALLBACK (delete_event), NULL); 
  g_signal_connect (G_OBJECT (window), "destroy", G_CALLBACK (destroy), NULL); 

  gtk_container_set_border_width (GTK_CONTAINER (window), 10); 
  button = gtk_button_new_with_label ("Hello World!"); 

  g_signal_connect (G_OBJECT (button), "clicked", G_CALLBACK (hello), NULL); 
  g_signal_connect_swapped (G_OBJECT (button), "clicked", 
                            G_CALLBACK (gtk_widget_destroy), 
                            G_OBJECT (window)); 

  gtk_container_add (GTK_CONTAINER (window), button); 
  gtk_widget_show (button); 
  gtk_widget_show (window); 

  gtk_main (); 

  return 0; 

In order to compile this program with the MinGW Cross Compile environment on Linux, you need to use i686-pc-mingw32-pkg-config provided by mingw32-pkgconfig package instead of standard pkg-config.

Building hello.exe using i686-pc-mingw32-pkg-config

$ i686-pc-mingw32-gcc -Wall -g hello_gtk.c -o hello.exe \
> `i686-pc-mingw32-pkg-config gtk+-2.0 --cflags` \
> `i686-pc-mingw32-pkg-config gtk+-2.0 --libs`
$ ./hello.exe &

Or, you can just use standard pkg-config by setting correct path to PKG_CONFIG_PATH for MinGW Cross Compile environment.

Building hello.exe using pkg-config

$ export PKG_CONFIG_PATH=/usr/i686-pc-mingw32/sys-root/mingw/lib/pkgconfig
$ i686-pc-mingw32-gcc -Wall -g hello_gtk.c -o hello.exe \
> `pkg-config gtk+-2.0 --cflags` `pkg-config gtk+-2.0 --libs`
$ ./hello.exe &

figure : hello.exe program executed on Wine

Related Links

  1. GTK+ 2.0 Tutorial