As gcc automatically knows to enable the glib library

I am making a simple program in C that uses glib.h , but when I compile it, I get an error, for example:

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

So it seems that gcc cannot find the glib.h file (which is part of the libglib2.0-dev package and is already installed). So first I try to find the glib.h files on my system and find the output as shown 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 tried the 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 correct flags for GCC, but it also shows the same error.

So, try to find a solution from SO and find the exact solution, for example

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

this command solves my problem

But I have a question that I can not tell gcc to enable the glib library, so I do not need to specify the pkg-config --cflags --libs glib-2.0 flag after gcc and just compile my gcc test.c -o test file gcc test.c -o test ?

+1
source share
1 answer

Not.

The problem of finding the source code in C and figuring out which libraries it uses is very complex. It seems to me that "AI complete" is for me, so it is usually solved manually by the programmer, pointing to the exact correct libraries to satisfy the dependencies with.

Just for glib, it’s easy to imagine a system with installed versions of glib 1.x and 2.x, and some calls are called exactly the same. Now try to imagine a computer program that can reduce what you had in mind with which library to link. This is not possible since only the programmer knows.

The pkg-config tool helps with the mechanics of what each library needs to use, but still up to yuo, to tell (via the module argument (s)) which libraries to use.

+4
source

Source: https://habr.com/ru/post/1215616/


All Articles