Search code examples
c++gtkeclipse-cdtglibubuntu-17.04

How do I get GTK includes to work for C++?


I am trying to start learning to develop GTK+ applications on Ubuntu 17.04 64-bit using C++ with the Eclipse CDT IDE, and I have been running into issues when trying to include the GTK library. I am wondering why the includes aren't working. I have gtk 2.0 (DEV) installed, and recently installed gtk 3.0 to see if that would help to no avail. The following is my code:

#include <iostream>
#include <gtk/gtk.h>

int main()
{
    return 0;
}

I am using the following compiler flags:

-I/usr/include/gtk-3.0 -I/usr/include/glib-2.0 -O0 -g3 -Wall -c -
fmessage-length=0

I am getting the following error from my compiler:

g++ -I/usr/include/gtk-3.0 -I/usr/include/glib-2.0 -O0 -g3 -Wall -c -
fmessage-length=0 -MMD -MP -MF"main.d" -MT"main.o" -o "main.o" 
"../main.cpp"
In file included from /usr/include/glib-2.0/glib/galloca.h:32:0,
             from /usr/include/glib-2.0/glib.h:30,
             from /usr/include/gtk-3.0/gdk/gdkconfig.h:13,
             from /usr/include/gtk-3.0/gdk/gdk.h:30,
             from /usr/include/gtk-3.0/gtk/gtk.h:30,
             from ../main.cpp:2:
/usr/include/glib-2.0/glib/gtypes.h:32:24: fatal error: glibconfig.h: 
No such file or directory
 #include <glibconfig.h>
                    ^
compilation terminated.
make: *** [main.o] Error 1
subdir.mk:18: recipe for target 'main.o' failed

Solution

  • Using pkg-config on the compiler flags, you need to include the gtk+-2.0 library and its dependencies. Adding this to your compiler command fixes the problem.

    In Eclipse CDT, this can be found at Project Properties -> C/C++ Build -> Settings -> GCC C++ Compiler, and then editing the "Command".

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