问题描述
我做简单的程序中使用 glib.h
C,但是当我编译它,我得到这样一个错误:
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.
因此,从上面看来GCC找不到 glib.h
文件(这是一部分的 libglib2.0-dev的
包并已安装)。因此,首先我试图找到我的系统 glib.h
文件,发现输出如下:
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
要得到GCC正确的标记,但仍然表现出同样的错误。
to get the right flags for GCC but still it show same error.
因此,有尝试从SO找到解决方案,并发现像
So have try to find solution from SO and found exact solution like
$ gcc test.c -Wall -o test `pkg-config --cflags --libs glib-2.0`
这个命令解决我的问题。
this command solve my problem
但是我有疑问,是不是没有办法告诉GCC包括glib库所以不需要给标志 pkg配置--cflags --libs巧舌如簧-2.0
在 GCC
,只是我用我的编译文件的gcc -o test.c的测试
?
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
?
推荐答案
没有。
看着C源$ C $ C,并找出其使用的库是非常困难的问题。感觉那种对我来说,这就是为什么它通常通过手动解决指出确切的正确的库程序员,以满足与依赖
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.
只是为了巧舌如簧,很容易想象一个系统,同时安装了巧舌如簧的1.x和2.x版本,以及一些调用被命名为完全一样。现在试着想像能够decucing你的意思,而库链接的计算机程序。这是不可能的,因为只有程序员知道。
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 decucing what you meant, which library to link with. It's not possible, since only the programmer knows.
的 pkg配置
工具有助于什么每个库,以便需要使用的机制,但它仍高达宥告诉它(通过模块名参数(S))的这使用精确的库。
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.
这篇关于如何GCC自动知道包括glib库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!