Linux Create GUI Application in C++ using Eclipse and GTK+

posted on 11/22/2014 11:57 AM by Eric Butler


So, the other day, I decided to try and create a GUI application in Ubuntu Linux using Eclipse CDT and GTK+. It took some fiddling and googling (or binging - depending on your search engine choice), but I finally managed to compile and build a working GUI application. In order to point those of you who may want to start dabbling in Linux GUI programming, like me, in the right direction, I'm posting what i found scattered about on the internet.

First thing I did was download Eclipse for C++ Developers by going to eclipse.org.

Once you download Eclipse, unpackage the eclipse folder anywhere on your system.

Run Eclipse and set up your workspace location. You may or may not need the GCC compiler, as it may already be installed on your system, but if not, install the package from your app store.


Next, make sure the libgtk2 is installed (this includes the -dev packages). Again, use the software center and install the packages.

Let's make sure everything is there. Run this command in your terminal.

pkg-config --cflags --libs gtk+-2.0

You should get an output just like this:

-pthread -I/usr/include/gtk-2.0 -I/usr/lib/i386-linux-gnu/gtk-2.0/include -I/usr/include/atk-1.0 -I/usr/include/cairo -I/usr/include/gdk-pixbuf-2.0 -I/usr/include/pango-1.0 -I/usr/include/gio-unix-2.0/ -I/usr/include/glib-2.0 -I/usr/lib/i386-linux-gnu/glib-2.0/include -I/usr/include/pixman-1 -I/usr/include/freetype2 -I/usr/include/libpng12 -I/usr/include/harfbuzz  -lgtk-x11-2.0 -lgdk-x11-2.0 -latk-1.0 -lgio-2.0 -lpangoft2-1.0 -lpangocairo-1.0 -lgdk_pixbuf-2.0 -lcairo -lpango-1.0 -lfreetype -lfontconfig -lgobject-2.0 -lglib-2.0

Thats a bit of a mouthful, but that's what the compiler will need in order to build your app and link the libraries. If there is an error, you may need to download additional libraries. Take a look at the list above, noting any that start with "-l" and install them if you don't have them.

Now, create a C++ project in Eclipse. Create your sample application like the screenshot below:

I found this sample code somewhere on the internet, its pretty easy to find or just type in what's there.

Next, Open up the properties window of your project. Select the C/C++ Build properties and choose the GCC compiler. In the expert settings box, at the very end, place the pkg-config command we used in the terminal earlier in single quotes. When the compiler runs, it will use this output to build and link your application. I also did the same thing for the C++ Compiler and Linker.


 

 

 

 

You can specify the include directories as well, just click on the C/C++ General item and select Paths and Symbols. Choose the GNU C++ language and click add to add include paths. I used the output of pkg-config to see what paths I should include.


You only need to add these two paths so that the IDE will recognize the include files:

/usr/include/glib-2.0
/usr/include/gtk-2.0

Add those two paths and build your app. Go to Project -> Build All. If successful, your output should like this:




There you go. You can begin working with GTK+. I know this isn't using the C++ wrappers, but I'll save that for another topic as I experiment more :)

Happy coding.



As a thank-you, please consider sending a small donation. You can contribute as much as you'd like.

Comments

Log in to leave a comment

Log In