Friday, August 6, 2010

Configure CDT and GTK+ Eclipse on Ubuntu

I have found lot of people searching on the internet for the right way to install GTK and configure it with Eclipse, but found no perfect solution posted on the web (except for discussion in forums). I'm writing this down so that you guys might not suffer as i did in configuring GTK and Eclipse.

  1. First i would ask you guys to install Eclipse from the official website: http://www.eclipse.org/downloads/  . I would ask you to install from the Tarball available on the web instead of sudo apt-get install. This is my personal preference. Extract it into a folder.
  2. Install CDT from help->install new softwares or download the respective CDT file and install it on Eclipse.
  3. Install GTK development libraries from package manager. Give the following command:

    sudo apt-get install gnome-core-devel build-essential
    libgtk2.0-dev libgtk2.0-doc dev-help
      (The good thing about Step 3 is that you don't have to
    configure GTK i.e, extract, compile, install, configure
    and make  programs detect the existing library)
  4. Open Eclipse. Open a new Eclipse Project by going to new->c++ project->Executable->Hello World C++ project and select Linux GCC.
  5. Open the source file and copy paste the sample, hello world code in the editor (available at http://library.gnome.org/devel/gtk-tutorial/stable/c39.html) and save it.
  6. Go to the project properties->C/C++ build->settings-> GCC C++ Compiler and in Command line pattern paste the following: ${COMMAND} `pkg-config --cflags --libs gtk+-2.0` ${FLAGS} ${OUTPUT_FLAG}${OUTPUT_PREFIX}${OUTPUT} ${INPUTS}   all we are doing is adding `pkg-config --cflags --libs gtk+-2.0` to the existing command line pattern. We are adding this line so that while compiling it would find the libraries/headers.
  7. Now, do the above for GCC C++ Linker in properties->C/C++ build->settings-> GCC C++ Linker.
  8. Click on OK, go to Project->build project and it will build it successfully. Now, you can run your project.
Basically we are telling the compiler to add `pkg-config --cflags --libs gtk+-2.0` while compiling.

For compilation in Terminal, we can use the following:
g++ `pkg-config --cflags gtk+-2.0`  `pkg-config --libs gtk+-2.0` test.cpp -o test

Happy C++ programming on Eclipse.

Cheers,
Rahul Kavi.

4 comments:

  1. Thank You.
    Your post has solved my problem

    ReplyDelete
  2. Thanks. Your command line configuration kickstarted my learning in linux programming in eclipse+gnome setting.

    But, would you mind taking a little bit of time to explain how your insertion (`pkg-config ...`) functions?

    Just a little bit curious.

    Thanks !

    ReplyDelete
  3. @prism: We are telling where to look for libraries for compiling the source to an executable. We can use g++ `pkg-config --cflags gtk+-2.0` `pkg-config --libs gtk+-2.0` test.cpp -o test

    to compile from terminal directly without installing the gtk libraries (out of eclipse)

    ReplyDelete