1

I make simple program in C which uses glib.h, but when I compile it I get an error like:

$ gcc test.c -o test

test.c:3:18: fatal error: glib.h: No such file or directory
compilation terminated.

So from above it seems GCC can't find glib.h file (which is a part of the libglib2.0-dev package and have already installed it). So first I try locate glib.h files in my system and found output as below:

$ locate glib.h

/usr/src/linux-headers-3.2.0-29-generic-pae/include/config/blk/dev/bsglib.h
/usr/src/linux-headers-3.2.0-48-generic-pae/include/config/blk/dev/bsglib.h
/usr/src/linux-headers-3.2.0-57-generic-pae/include/config/blk/dev/bsglib.h

then I have tried command:

$ pkg-config --cflags glib-2.0

-I/usr/include/glib-2.0 -I/usr/lib/i386-linux-gnu/glib-2.0/include

to get the right flags for GCC, but still it shows the same error.

So I have tried to find a solution from SO and found an exact solution like:

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

This command solves my problem.

But is there a way to tell GCC to include the glib library, so not to require to give the flag pkg-config --cflags --libs glib-2.0 after gcc and simply I compile my file by gcc test.c -o test?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jayesh Bhoi
  • 24,694
  • 15
  • 58
  • 73
  • Learn how to use `make`. See [this answer](http://stackoverflow.com/a/20146082/841108) to a very similar question. And don't name your executable `test`; it is a useful shell builtin, or `/usr/bin/test`, see [test(1)](http://man7.org/linux/man-pages/man1/test.1.html)... – Basile Starynkevitch Feb 05 '14 at 11:57
  • yes i know about `make`. I simply compile my source by `make test pkg-config --cflags --libs glib-2.0`. And thanks to suggest for name conflation between binary and shell builtin. – Jayesh Bhoi Feb 05 '14 at 12:03
  • Just write a `Makefile` (once, and upgrade it with the code) and type `make` to build your program. – Basile Starynkevitch Feb 05 '14 at 12:11

1 Answers1

4

No.

The problem of looking at C source code and figuring out which libraries it uses is very hard. It feels kind of "AI complete" to me, which is why it's typically solved manually by the programmer pointing out the exact right libraries to satisfy the dependencies with.

Just for glib, it's easy to imagine a system with both glib 1.x and 2.x versions installed, and some calls are named exactly the same. Now try to imagine a computer program capable of deducing what you meant, which library to link with. It's not possible, since only the programmer knows.

The pkg-config tool helps with the mechanics of what each library requires in order to be used, but it's still up to you to tell it (via the module name argument(s)) which exact libraries to use.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
unwind
  • 391,730
  • 64
  • 469
  • 606
  • ok but if i execute `pkg-config --cflags glib-2.0` command from terminal then is it include path for glib library? If yes then after it my command `gcc test.c -o test` must work. – Jayesh Bhoi Feb 05 '14 at 11:59
  • Yes, it gives you the include path for the `glib-2.0` package, which is the 2.x version of the glib library. But that information of course must be passed to gcc somehow, it's not as if gcc is there on your shoulder reading your terminal ... – unwind Feb 05 '14 at 12:04