Why can't I build a "hello world" for glib?

mike picture mike · Jul 18, 2009 · Viewed 58.9k times · Source

So here's the world's simplest glib program:

#include <glib.h>

I try to compile it with gcc test.c and I get:

test.c:1:18: error: glib.h: No such file or directory

So I make sure that I have the right packages:

# dpkg -l | grep libglib
ii  libglib-perl                              1:1.183-1                               Perl interface to the GLib and GObject libra
ii  libglib1.2-dev                            1.2.10-19build1                         The GLib library of C routines (development)
ii  libglib1.2ldbl                            1.2.10-19build1                         The GLib library of C routines
ii  libglib2.0-0                              2.20.1-0ubuntu2                         The GLib library of C routines
ii  libglib2.0-cil                            2.12.1-1ubuntu2                         CLI binding for the GLib utility library 2.1
ii  libglib2.0-data                           2.18.2-0ubuntu2                         Common files for GLib library
ii  libglib2.0-dev                            2.20.1-0ubuntu2                         Development files for the GLib library
ii  libglibmm-2.4-1c2a                        2.18.1-1                                C++ wrapper for the GLib toolkit (shared lib

Then I search for any "glib.h" anywhere under /usr/include. I get two, /usr/include/glib-1.2/glib.h and /usr/include/glib-2.0/glib.h. So I try:

$ gcc -I/usr/include/glib-2.0 -Wall test.c  
In file included from /usr/include/glib-2.0/glib/galloca.h:34,
             from /usr/include/glib-2.0/glib.h:32,
             from test.c:2:
/usr/include/glib-2.0/glib/gtypes.h:34:24: error: glibconfig.h: No such file or directory

(about 10,000 more errors snipped)

I don't seem to have a glibconfig.h anywhere on my computer.

What do I do now?

Answer

Chris Arguin picture Chris Arguin · Jul 18, 2009

glib tends to hide itself... Your include statement doesn't work because GCC doesn't automatically search subdirectories, and so cannot see the glib.h in glib-1.2 or glib-2.0.

Read the Compiling GLib Applications page in the GLIB manuals... you use commands like pkg-config --cflags glib-2.0 to get the right flags for GCC.

The canonical way to do what you are trying is

% gcc test.c -Wall -o test `pkg-config --cflags --libs glib-2.0`

Note the back-ticks, which tell the shell to run the pkg-config command "in-place".