Create GUI with Gtkmm - Setup Eclipse for Gtkmm

gtkmm is the official C++ interface for the popular GUI library GTK+. You can create user interfaces either in code or with the Glade User Interface designer, using Gtk::Builder.
Still you can use gtk on C++, but C++ is the Object Oriented Programming language then Gtkmm enable you to use some OOP techniques such as inheritance, polymorphism, etc... Also enable you to use standard C++ libraries on Gtkmm calls. Also gtkmm can be use to create GUI in C++ with the large number of widgets which you need to decorate your GUI.

I am willing to write a series of articles/tutorials about Gtkmm and its variance widgets with practical examples. Because I used gtkmm for my final year project and I would like to share the knowledge that I garbed with you all. This article describe the initial steps that you should follow to setup gtkmm library on Ubuntu and configure eclipse for gtkmm developing. I am using Eclipse Helios on Ubuntu 11.04 (64-bit).

Step 1 (Install required packages)
As you know there are several ways to install packages on Ubuntu. I am using Synaptic package manager for that.
So open synaptic from application menu and search for gtkmm-dev and install the latest version from the search result. I have installed libgtkmm-2.4-dev package. When you install synaptic will indicate some dependencies such as gtk++, install them all to complete the installation. I hope that you have the working eclipse with CDT (C/C++ Development Tooling).


Step 2 (Setup Eclipse for Gtkmm)
I am using CDT installed Eclipse IDE‌ for C++ developments. Because it has more features which are more helpful to developer to write programs effectively and rapidly.
Eclipse needs to be setup in order to create gtkmm project. Eclipse needs to complete these setups to compile the program successfully and fill the auto complete menu with all the functions, variables, data types, etc...
Initially you need to select Linux GCC as the toolchain when you create a C++ project on eclipse.
  1. Right click on the project that you need to be configure and select Properties from the menu.
  2. Expand C/C++ Build from the left side pane and click on Settings.
  3. Then click on GCC C++ Compiler.
  4. Paste following command line options between ${COMMAND} and ${FLAGS} to Command line pattern: text box.
    `pkg-config --cflags --libs gtkmm-2.4`
    Note - 2.4 holds the installed version, replace it with your version, if you installed different version.

    Now you may have the full text of the Command line pattern as,
    ${COMMAND} `pkg-config --cflags --libs gtkmm-2.4` ${FLAGS} ${OUTPUT_FLAG}${OUTPUT_PREFIX}${OUTPUT} ${INPUTS}
  5. Click on GCC C++ Linker and follow the same procedure as previous step.
  6. Now click on Includes under GCC C++ Compiler.
  7. Add include paths to the list and let eclipse find the header files on these paths. Click on add button and add following paths to the list. You should change the location by considering your location which includes installed on and version.

    /usr/include/gtkmm-2.4
    /usr/include/gdkmm-2.4
    /usr/include/glibmm-2.4

    Somehow finally you should include paths for gtkmm, gdkmm and glibmm libraries header files.

Now Eclipse ready for gtkmm development. Let's write a simple testing program to check the gtkmm.

First you should include following header files to the program. As,
#include <gtkmm.h>

Your main function should look like this,

int main(int argc, char **argv) {
    Gtk::Main kit(argc,argv);//The constructor for this object initializes gtkmm
    Gtk::Window frmMain;

    kit.run(frmMain);

    return 0;
}

Code explained,
All the gtkmm programs accepts command line arguments list as above (int argc, char **argv). Therefore all the gtkmm programs should follow the same argument list as describe in here.

Gtk::Main kit(argc,argv);//The constructor for this object initializes gtkmm

This line will initialize the basic gtkmm functionalities and let program to run gtkmm functions. Also checks the arguments passed to your application on the command line, looking for standard options such as -display.


Gtk::Window frmMain;

This will create a new widget of Gtk::Window type as frmMain to hold the main window of the program.


kit.run(frmMain);

This is the main loop of gtkmm and let user to use the widgets. This line will display the window which we already created.

You can download the complete source code of this testing program. Resultant of the program is an empty window as,

Next article will describe the OO behavior of gtkmm and introduce the Glade UI designer for gtkmm.

5 comments:

  1. "Somehow finally you should include paths for gtkmm, gdkmm and glibmm libraries header files." - so not knowing what this means is why I'm getting a bunch of linker errors, right?

    ReplyDelete
    Replies
    1. yes, exactly. that is the thing doing on `pkg-config --cflags --libs gtkmm-2.4` command on Step2

      Delete
  2. Just wish to ѕay your article іs as astounding.
    The clearness in your post is just greɑt and i cɑn assume yⲟu
    are an expert onn this subject. Ϝine wirh yoսr permissijon let mee tߋ grab
    үօur feed tо қeep updated wіtһ forthcoming post.
    Τhanks a mіllion and pⅼease keep upp tһe rewarding ԝork.

    ReplyDelete