如何使用 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 00:55:44  来源:igfitidea点击:

How to include needed C library using gcc?

clinuxgccindicator

提问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 appindicatorlibrary (vs. building it from source), did you only install the libappindicator1package or did you alsoinstall libappindicator-devto get the libappindicatorheader 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-devpackage 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 -lcommand line option. You can specify the library search path with the -Loption. E.g:

使用-l命令行选项。您可以使用该-L选项指定库搜索路径。例如:

gcc -o myprogram -lfoo -L/home/me/foo/lib myprogram.c

This will link myprogramwith the static library libfoo.ain the folder /home/me/foo/lib.

这将myprogramlibfoo.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 libappindicatorand it's dependencies. This assumes libappindictaor-devpackage is already installed.

pkg-config 将获取所需的 include 和 lib 标志libappindicator及其依赖项。这假设libappindictaor-dev包已经安装。