tags:

views:

401

answers:

3

I can't seem to compile this basic program using glib.h...

#include glib.h
#include stdio.h

int    main ()
{
return ((glib_major_version) || (glib_minor_version) || (glib_micro_version));  ;
return 0;
}

glib.h is located in /usr/local/include/glib-2.0

So I compiled with

$ gcc -v -c -mcpu=v9 -I/usr/local/include/glib-2.0 testme2.c 

Now I get missing glibconfig.h. But it is in /usr/local/lib/glib-2.0/include/glibconfig.h

Strangely glibconfig.h is the only file in /usr/local/lib/glib-2.0/include directory and more strangely it is not in /usr/local/include/glib-2.0 directory

Here are some more error messages...

from /usr/local/include/glib-2.0/glib.h:32,
            from testme.c:40:
:34:24: glibconfig.h: No such file or directory

Here is an extract of /usr/local/include/glib-2.0/glib/gtypes.h

ifndef __G_TYPES_H__
define __G_TYPES_H__

include glibconfig.h
include glib/gmacros.h

G_BEGIN_DECLS

typedef char   gchar;
typedef short  gshort;

The question is how is GCC supposed to find glibconfig.h?

A: 

There should be a program in the glib distribution called glib-config. If you run it with the --cflags argument, it will list all the gcc flags necessary. For example on my system:

$ glib-config --cflags
-I/usr/include/glib-1.2 -I/usr/lib/glib/include

As you can see, both directories are specified as include directories. There is also a --libs flags, which you can pass to your linker, so all the correct libs are linked, and the linker search path is correctly specified.

Peter Kovacs
Not with Glib-2.0, there isn't. They've moved to using http://pkg-config.freedesktop.org/ instead of their own wrapper.
ephemient
Ah, you're right. I (clearly) have an old distribution of glib. I'll leave the answer for future reference, in case anybody is dealing with an old version of glib.
Peter Kovacs
+3  A: 

Glib installs a glib-2.0.pc file that describes all the options necessary to compile and link.

export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig
g++ -c `pkg-config --cflags glib-2.0` testme2.c
g++ -o testme2 testme.o `pkg-config --libs glib-2.0`

Note the use of pkg-config within backquotes.

ephemient
I'm actually trying to install SYSLOGNG, which requires pkg-config and GLIB.I need to retest my GLIB installation....since '#include <glib.h>' should work out of the box.I shouldn't be passing a -I/usr/local/include/glib-2.0 to the compiler.
Gene Mat
Since your glib is installed to a non-standard location, you may need to set the environment variable `PKG_CONFIG_PATH` to include the appropriate location. No, glib is *not* supposed to work out of the box like that. It's supposed to be parallel-installable, and you must use compiler options from `pkg-config` to determine which installation to use.
ephemient
A: 

$ pkg-config --cflags --libs glib-2.0
-I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include -lglib-2.0

It is advisable to use pkg-config instead of manual configuration if .pc files for desired libraries exist and fall back to manual configuration if you have specific needs or no configuration for the library you are going to use exists. As you can see, pkg-config tells the compiler to put both glib-2.0 and glib-2.0/include directories into the search path as the root header searches in the global path.

You can infer pkg-config output into your compilation command via gcc `pkg-config ...` ...

.pc files are usually installed in /usr/include/pkgconfig

Damg
Given that OP appears to have a custom install of Glib to `/usr/local`, I would expect the appropriate `glib-2.0.pc` to exist in there as well.
ephemient
Good point. Should be probably somewhere in the local dir.
Damg