tags:

views:

1967

answers:

3

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?

(I'm using Ubuntu, BTW)

+12  A: 

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".

Chris Arguin
gcc test.c -Wall -o test ``pkg-config --cflags --libs glib-2.0``
Tyler
Note the backticks `. They are not single quotes.
Tyler
@Tyler - Thanks, I included that in the answer.
Chris Arguin
A: 

"apt-get build-dep" is your friend -- nobody can remember all the packages you need to build something.

Ken
+6  A: 
> > The canonical way to do what you are trying is

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

Sorry, but no. That is a common misconception, that just happens to work in most cases on ELF-based systems, Linux in particular. The canonical way is to pass in the cflags and libraries separately, in the correct and traditional locations on the command line, like this:

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

It is a pity that pkg-config accepts both the --cflags and --libs options at the same time, as it means this incorrect meme will never die, and people used to it on Linux will continue to be baffled when they then try the same on other platforms.

tml
+1 for bravery! Strictly speaking the name of the source code file should go at the end, after all the options.
Chris Huang-Leaver
Sorry, but that is just not true.Have you actually tried doing it that way on some non-Linux platform, for instance MinGW?See for instance http://gcc.gnu.org/onlinedocs/gcc/Link-Options.html which clearly says "It makes a difference where in the command you write this option; the linker searches and processes libraries and object files in the order they are specified. Thus, `foo.o -lz bar.o' searches library `z' after file foo.o but before bar.o. If bar.o refers to functions in `z', those functions may not be loaded".
tml