首页 > 编程知识 正文

pkg-config安装,如何安装pkg命令

时间:2023-05-04 00:54:34 阅读:242306 作者:18

pkg-config是在编译应用程序和库时使用的辅助工具。它可以帮助您在命令行中插入正确的编译器选项,以便应用程序可以使用 gcc -o test test.c pkg-config --libs --cflags glib-2.0,而不是在何处找到glib(或其他库)的硬编码值。这是语言无关的,所以它可以用于定义文档工具的位置。

概念

pkg-config的主要用途是提供编译和链接程序到库的必要细节。此元数据存储在pkg-config文件中。这些文件具有后缀 .pc并且驻留在pkg-config工具已知的特定位置

prefix=/usr/localexec_prefix=${prefix}includedir=${prefix}/includelibdir=${exec_prefix}/libName: fooDescription: The foo libraryVersion: 1.0.0Cflags: -I${includedir}/fooLibs: -L${libdir} -lfoo

各参数的意义:

Name: 库或软件包的可读名称Description: 简要说明URL: 一个网址,人们可以获取更多的信息和下载包Version: 专门定义包的版本的字符串Requires: 此包所需的包的列表。可以使用比较运算符=,<,>,<=或> =来指定这些包的版本(依赖关系)Requires.private: 该包所需的私有包的列表,但不暴露于应用程序。Conflicts: 描述与此冲突的软件包的可选字段Cflags: 该包的特定编译器标志以及不支持pkg-config的所有库。如果所需的库支持pkg-config,则应将其添加到 Requires或Requires.privateLibs: 特定于此程序包的链接标志以及不支持pkg-config的所有库 编写pkg-config文件

当为一个包创建pkg-config文件时,首先需要决定如何分发它们。每个文件最好用于描述单个库,因此每个包应至少与安装库一样多的 pkg-config文件。

特点:

pkg-config文件最好每个文件对应一个库文件。

包名通常通过pkg-config元数据文件的名称来确定,.pc后缀的文件名称和库的名称相匹配。

例如:libfoo.so的软件包 将具有一个包含pkg-config元数据的对应libfoo.pc文件

Name, Description 和 URL 是纯信息描述容易添加,version比较棘手,要确保用户数据使用。

Requires, Requires.private, Cflags, Libs 和 Libs.private将定义外部项目使用的元数据的编译和链接库

Requires, Requires.private 都是确定依赖关系的,Requires链接的库文件会暴露出来,Requires.private依赖的静态的链接库。
libs 使用该库的链接标志
Cflags 使用库的编译器标志

使用pkg-config

系统两个模块foo和bar,pc文件如下:

foo.pc:prefix=/usrexec_prefix=${prefix}includedir=${prefix}/includelibdir=${exec_prefix}/libName: fooDescription: The foo libraryVersion: 1.0.0Cflags: -I${includedir}/fooLibs: -L${libdir} -lfoobar.pc:prefix=/usrexec_prefix=${prefix}includedir=${prefix}/includelibdir=${exec_prefix}/libName: barDescription: The bar libraryVersion: 2.1.2Requires.private: foo >= 0.7Cflags: -I${includedir}Libs: -L${libdir} -lbar –modversion 显示版本号 $ pkg-config --modversion foo1.0.0$ pkg-config --modversion bar2.1.2 –libs 打印每个模块所需的链接标志 $ pkg-config --libs foo-lfoo$ pkg-config --libs bar-lbar –static 显示静态链接库

模块bar使用的lfoo库,但是只使用–libs没有显示,但是在系统调用的使用在libdir下查找对应的库文件。

$ pkg-config --libs --static bar-lbar -lfoo

输出所有的cflags

$ pkg-config --cflags bar-I/usr/include/foo$ pkg-config --cflags --static bar-I/usr/include/foo –exists 查看模块的可用性 $ pkg-config --exists foo$ echo $?0 依赖模块版本检查 $ pkg-config --libs "bar >= 2.7"Requested 'bar >= 2.7' but version of bar is 2.1.2 –print-errors 打印详细的结果 $ pkg-config --exists --print-errors xoxoPackage xoxo was not found in the pkg-config search path.Perhaps you should add the directory containing `xoxo.pc'to the PKG_CONFIG_PATH environment variableNo package 'xoxo' found pkg-config命令的环境变量

PKG_CONFIG_PATH 是pkg-config的查找路径,在unix系统中查找路径为/usr/lib/pkgconfig 和/usr/share/pkgconfig 但是有的安装在/usr/lib 目录下,所以最好提前制定好环境变量

$ pkg-config --modversion hello ``` Package hello was not found in the pkg-config search path. Perhaps you should add the directory containing `hello.pc' to the PKG_CONFIG_PATH environment variable No package 'hello' found ```$ export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig$ pkg-config --modversion hello1.0.0 参考链接

指导文档

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。