如何使用 gcc 包含所需的 C 库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6016815/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to include needed C library using gcc?
提问by multiholle
I am trying to compile the simple C example from this Tutorialon Ubuntu using gcc. What do I have to use as argument for gcc to include the needed libraries for #include <libappindicator/app-indicator.h>
?
我正在尝试使用 gcc 在 Ubuntu 上编译本教程中的简单 C 示例。我必须使用什么作为 gcc 的参数来包含所需的库#include <libappindicator/app-indicator.h>
?
采纳答案by QuantumMechanic
If you used apt-get
, Synaptic Package Manager
, etc. to get the appindicator
library (vs. building it from source), did you only install the libappindicator1
package or did you alsoinstall libappindicator-dev
to get the libappindicator
header files? Linux packages very often have split the runtime libraries from the compile-time headers. That way people who only need the libraries to satisfy a dynamic link don't have to install unneeded headers. But since you're doing development you need those headers and therefore need the libappindicator-dev
package as well.
如果您使用apt-get
,Synaptic Package Manager
等来获取appindicator
库(与从源代码构建它相比),您是只安装libappindicator1
包还是还安装libappindicator-dev
以获取libappindicator
头文件?Linux 包经常将运行时库与编译时头文件分开。这样,只需要库来满足动态链接的人就不必安装不需要的头文件。但是由于您正在进行开发,因此您需要这些头文件,因此也需要libappindicator-dev
包。
回答by Kristofer
-I<search path to include files>
-L<search path to the lib file>
-l<libname>
回答by Vijay Mathew
Use the -l
command line option. You can specify the library search path with the -L
option. E.g:
使用-l
命令行选项。您可以使用该-L
选项指定库搜索路径。例如:
gcc -o myprogram -lfoo -L/home/me/foo/lib myprogram.c
This will link myprogram
with the static library libfoo.a
in the folder /home/me/foo/lib
.
这将myprogram
与libfoo.a
文件夹中的静态库链接/home/me/foo/lib
。
回答by manugupt1
What you are trying to do here is making a gtk app, the above solutions are as applicable anywhere like using the -l option and -I option,
您在这里尝试做的是制作一个 gtk 应用程序,上述解决方案适用于任何地方,例如使用 -l 选项和 -I 选项,
However for GTK apps you may also use pkg-config which make it easier as your paths can be predefined
但是,对于 GTK 应用程序,您也可以使用 pkg-config,因为您可以预定义路径
http://www.freedesktop.org/wiki/Software/pkg-config
http://www.freedesktop.org/wiki/Software/pkg-config
An interesting example can be found here http://developer.gnome.org/gtk/2.24/gtk-compiling.html
一个有趣的例子可以在这里找到 http://developer.gnome.org/gtk/2.24/gtk-compiling.html
回答by hytromo
What I do is:
我要做的是:
pkg-config --list-all | grep indicator
回答by lijo
gcc example.c -o example `pkg-config --cflags --libs appindicator-0.1`
pkg-config will fetch the required include and lib flags for libappindicator
and it's dependencies. This assumes libappindictaor-dev
package is already installed.
pkg-config 将获取所需的 include 和 lib 标志libappindicator
及其依赖项。这假设libappindictaor-dev
包已经安装。