Sunday, February 6, 2011

Configuring Codeblocks to compile Gtk based programs

Problem: Getting "ERROR: gtk/gtk.h: No such file or directory" while trying to compile code using Gtk library.

Information: The problem with above error is that the code which you have written asks to insert gtk/gtk.h contents, but the preprocessor is unable to find that file. The compiler goes through the search path defined by your IDE and could not find the file.
This problem can come when either your system does not contain these files and/or the search path is not defined properly.To solve the first issue, you need to download the Gtk+-2.0 development package. Then add search path to find the gtk.h file.
Once you have done this, expect it to work as far as compiling process. The linker will then complain about "undefined references". This will indicate that the compiler correctly understood the name and prototypes of all the functions, but now the linker cannot actually find the libraries (actual already compiled libraries) that contain those functions.
At this point, you will have to find the setting in your IDE that dictates which libraries you are linking against. Then you need to add search path to find the necessary libraries in LIKER option.
To help in searching path a toll called pkg-config can be used.pkg-config will automatically look for the Gtk libraries and will provide an easy mechanism to compile the Gtk programs.

1) Downloading the gtk+-2.0 development package:
    Open the synaptic manager and install the 'libgtk2.0-dev' package from it.
  2.1) If you want to compile the code from the terminal type the following command(in red color)
         gcc `pkg-config --cflags --libs gtk+-2.0` 1.c -o hello to compile the .c file

        There should not be any error after this.

 2.2) Configuring Code::blocks
        I set up options globally since I wanted to use codeblocks just to practice gtk.
        Go to Settings->Compilers and Debuggers and added:
        To edit compiler search path, go to Other options in compiler setting and add this
        `pkg-config --cflags gtk+-2.0 `

        To edit linker search path, go to Other link options inlinker setting and add this
        `pkg-config --libs gtk+-2.0 `

        Note : bactick`` substitues its output before executing the whole command

Screenshots :

Compiler setting

Linker Setting:

References :
Information about compiling GTK+ applications-- :
Codeblok configuration(Commnet no #3) :
More information about compilers and linker (Read first comment of Moschops) :


  1. It worked for me! Thankyou thankyou thankyou. :)

  2. Thanks, it worked!

  3. Thanks for sharing. It worked!