简体   繁体   English

gcc如何自动知道要包含glib库

[英]how gcc automatically know to include glib library

I make simple program in C which uses glib.h , but when I compile it I get an error like: 我用C使用glib.h编写了一个简单的程序,但是当我编译它时,出现如下错误:

$ 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). 因此从上面看来,gcc找不到glib.h文件(它是libglib2.0-dev软件包的一部分,并且已经安装了它)。 So first I try locate glib.h files in my system and found output as below: 因此,首先我尝试在系统中找到glib.h文件,并找到如下输出:

$ 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 show same error. 获取正确的GCC标志,但仍显示相同的错误。

So have try to find solution from SO and found exact solution like 因此,尝试从SO中找到解决方案,并找到确切的解决方案,例如

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

this command solve my problem 这个命令解决了我的问题

But I have question that, is not any way to tell gcc include glib library so not require to give flag pkg-config --cflags --libs glib-2.0 after gcc and simply I compile my file by gcc test.c -o test ? 但是我有一个问题,那就是没有办法告诉gcc包含glib库,所以不需要在gcc之后给出pkg-config --cflags --libs glib-2.0标志,而我只是通过gcc test.c -o test编译文件?

No. 没有。

The problem of looking at C source code and figuring out which libraries it uses is very hard. 查看C源代码并弄清楚它使用哪个库的问题非常困难。 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. 在我看来,这是“ AI完整的” ,这就是为什么通常由程序员手动指出要满足其依赖性的正确库来手动解决的原因。

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. 仅对于glib,很容易想象同时安装了glib 1.x和2.x版本的系统,并且某些调用的命名是完全相同的。 Now try to imagine a computer program capable of decucing 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 yuo to tell it (via the module name argument(s)) which exact libraries to use. pkg-config工具可帮助您了解每个库需要使用的机制,但是仍然需要(通过模块名称参数)告诉您要使用哪个库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM