Linux操作系统中pkg-config的用法示例

本文主要介绍Linux操作系统中pkg-config的用法示例。

1 概述

pkgconfig软件包的描述信息如下:

A tool for determining compilation options.

Manual中对于pkg-config的介绍信息如下:

pkg-config - Return metainformation about installed libraries.

The pkg-config program is used to retrieve information about installed libraries in the system. It is typically used to compile and link against one or more libraries.

pkg-config retrieves information about packages from special metadata files. These files are named after the package, and has a .pc extension. It will additionally look in the colon-separated list of directories specified by the PKG_CONFIG_PATH environment variable.

pkg-config官网中的介绍信息如下:

pkg-config is a helper tool used when compiling applications and libraries. It helps you insert the correct compiler options on the command line so an application can use gcc -o test test.c `pkg-config --libs --cflags glib-2.0` for instance, rather than hard-coding values on where to find glib (or other libraries). It is language-agnostic, so it can be used for defining the location of documentation tools, for instance.

总结上述介绍信息,可知:pkg-config用于返回系统上已安装的库的相关信息,这些信息在编译和连接与该库相关的程序或库的过程中使用。

需要强调的是,pkg-config用于程序或库的编译和连接过程,而非运行过程。

2 示例

下面通过一个示例,介绍pkg-config的使用场景及用法。

假设操作系统中安装了x265库,安装位置为“/usr/local”,那么其对应的库文件为“/usr/local/lib/libx265.so”,头文件为“/usr/local/include/x265.h”,这些在编译和连接其他程序或库(如ffmpeg)时所需的相关信息,都包含在“/usr/local/lib/pkgconfig/x265.pc”文件中。x265.pc文件的内容如下:

[root@livestreamserver /usr/local]# cat /usr/local/lib/pkgconfig/x265.pc 
prefix=/usr/local
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir=${prefix}/include

Name: x265
Description: H.265/HEVC video encoder
Version: 3.5
Libs: -L${libdir} -lx265
Libs.private: -lstdc++ -lm -lgcc_s -lgcc -lgcc_s -lgcc -lrt -ldl
Cflags: -I${includedir}
[root@livestreamserver /usr/local]# 

接下来以构建启用x265编码功能的ffmpeg为例,介绍pkg-config的具体用法。

在构建ffmpeg时,首先需要通过“./configure --enable-gpl --enable-libx265”命令创建构建选项。由于指定了要使用x265库,所以该命令运行过程中会通过pkg-config寻找x265库的相关信息。

在不进行任何pkg-config配置的情况下,上面的configure命令会报错,提示在使用pkg-config找不到x265的相关信息,错误信息如下图所示:

出现上图所示错误,是由于pkg-config没有找到x265.pc文件,获取不到编译ffmpeg所需的x265库的相关信息。

pkg-config工具会通过以下两种途径寻找库的“.pc”文件:

  • 在系统默认的“.pc”文件目录(如“/usr/lib64/pkgconfig/”)下寻找;
  • 通过环境变量PKG_CONFIG_PATH寻找。

在本文中,采用向环境变量PKG_CONFIG_PATH中添加x265.pc所在目录(即,/usr/local/lib/pkgconfig/)的方式解决configure命令的报错问题。修改环境变量PKG_CONFIG_PATH的命令如下:

export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:$PKG_CONFIG_PATH

说明:可以将上面的命令添加到系统配置文件(如“/etc/profile”)或者用户配置文件中(如“~/.bash_profile”)中,以避免PKG_CONFIG_PATH的值在切换终端或系统重启后失效。

完成上述操作后,再次运行“./configure --enable-gpl --enable-libx265”命令,就可以找到x265库的相关信息,也就不会再出现错误信息了。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

liitdar

赠人玫瑰,手有余香,君与吾共勉

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值