Hello World GTK3 program in Eclipse


Making a gtk3 application in eclipse can be somewhat tricky as one has to fix dependencies.

Lemme show you how to do it:

This tutorial is based on:

1) Install Eclipse Plugin “pkg-config”  (from MarketPlace)
2) Create new C “Hello World Ansii C” Project. (or some basic C hello world project).
2.1) Switch to the C/C++ perspective.
3) Go to Project Properties -> C/C++ Build -> Settings -> Pkg-Config tab ->
enable “gtk-+-3.0”.
(ensure that you have gtk3 installed on your system. (e.g “sudo yum install gtk3” or search on yum extender/google). (There should be something in /usr/include/gtk-3.0/).
4) Copy and pasta the code below: (from gtk3 tutorial)

#include <stdio.h>
#include <stdlib.h>
#include <gtk/gtk.h>

int main (int   argc, char *argv[]) {
	  GtkWidget *window;

	  gtk_init (&argc, &argv);

	  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
	  gtk_window_set_title (GTK_WINDOW (window), "Window :-)");

	  g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);

	  gtk_widget_show (window);

	  gtk_main ();

	  return 0;

4) Click on build project.
Make a mental note whether you build for debug or run:
bulild debug run

5) Running can be the crux.
Right click on the project -> run as -> local C/C++ application.

Sometimes you might run into the issue that you build a debug configuration but run the release configuration. Inspect the console:
Try to adjust your run configuration in case you get the wrong one.
You could always try to navigate to the right place and run the command from the terminal also:

gcc `pkg-config --cflags gtk+-3.0` -o example-0 example-0.c `pkg-config --libs gtk+-3.0`

If things went well, you should see a window:

hello world window

Feel free to post questions, I will try to reply.

Troubleshooting breakage

gtk.h cannot be resolved

Once in a while, I find that my sources fail to build telling me that it fails to resolve #include .
In this case, usually the pkg-config plugin broke, got lost or uninstalled somehow. I usually go to the marketplace and re-install it.
This happens to me once every other month or so.

One thought on “Hello World GTK3 program in Eclipse

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s