pkg-config

pkg-config

问什么需要pkg-config

在使用 CMake 作为项目构建工具时,有一些库并没有提供 cmake 文件,使用Pkg-Config维护它依赖库路径、头文件路径、编译选项、链接选项等信息,往提供的是 pkg-config 的 .pc 文件,虽然可以在 cmake 中用 include_directories 和 link_directories 来手动指定查找目录,但这样写并不能保证跨平台,甚至同一个库在不同Linux发行版中的位置也不一样,这个时候最好的解决方法就是能够在 cmake 中(优雅地)使用pkg-config提供的信息。

pkg-config基本使用

官方说明

DESCRIPTION
       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.  Here is a typical  usage  sce‐
       nario in a Makefile:

       program: program.c
            cc program.c $(pkg-config --cflags --libs gnomeui)

       pkg-config  retrieves  information about packages from special metadata files. These files are named
       after the package, and has a .pc extension.  On most systems, pkg-config looks  in  /usr/lib/pkgcon‐
       fig,  /usr/share/pkgconfig, /usr/local/lib/pkgconfig and /usr/local/share/pkgconfig for these files.
       It will additionally look in the colon-separated (on Windows, semicolon-separated) list of  directo‐
       ries specified by the PKG_CONFIG_PATH environment variable.

       The  package name specified on the pkg-config command line is defined to be the name of the metadata
       file, minus the .pc extension. If a library can install multiple versions  simultaneously,  it  must
       give  each  version  its own name (for example, GTK 1.2 might have the package name "gtk+" while GTK
       2.0 has "gtk+-2.0").

       In addition to specifying a package name on the command line, the full path to a given .pc file  may
       be given instead. This allows a user to directly query a particular .pc file.

也就是说,pkg-config默认会在以下路径中查找指定的包(库)对应的.pc文件:

  • /usr/lib/pkgconfig目录
  • /usr/share/pkgconfig目录
  • /usr/local/lib/pkgconfig目录
  • /usr/local/share/pkgconfig目录
  • PKG_CONFIG_PATH环境变量里的目录(可通过export PKG_CONFIG_PATH=XXX来修改)
  • 给pkg-config传入的.pc文件绝对路径

root 对pkg-config的影响

我的OpenCV安装在/home/topeet/programfile/opencv2,明显非默认安装。
列出一层目录: tree -L 1 -F
.
├── bin/
├── include/
├── lib/
└── share/

1: 非root情况下执行 -- pkg-config --list-all | grep opencv
		结果为空,
2: root情况下执行 --   pkg-config --list-all | grep opencv
		opencv                         OpenCV - Open Source Computer Vision Library
		

结论:

需要注意的是:不管你是不是root,pkg-config都会搜索/usr/lib和/usr/local/lib下所有.pc文件,所以并不是非root执行pkg-config就啥都没有。换句话说/usr/lib和/usr/local/lib下所有.pc文件,root与否pkg-config都能找到。 1: 很明显非root条件下,pkg-config找不到opencv.pc,所以仅这样处理的CMake/Makefile,直接报错! 2: 那么root可以直接搜索到/home/topeet/programfile/opencv2/lib/pkg-config/opencv.pc,说明了什么?它能搜索到非root下的opencv.pc。故如果你是root你不用担心pkg-config找不到你需要的.pc文件 3: 对于非root情况下,看似有点糟糕,但是PKG_CONFIG_PATH就能帮助非root用户了。很明显,你怎么能获得root?root就一个啊,所以讨论这个问题很有必要。

经过上面的分析,我们知道了非root要想在CMake/Makefile中导入pkg-config,必须用PKG_CONFIG_PATH传你的.pc文件在哪?

Makefile环境变量的问题

基本常识

默认情况下,传递顺序(你不做覆盖的情况下):
操作系统的环境变量--->shell(终端)--->根Makefile--->子Makefile
1: 明显,你另起一个终端,其他的终端设置的环境变量是不会传过来的,不同的终端彼此隔离,都继承操作系统的环境变量
2: 马上会有一个想法,我在终端设置opencv.pc文件的路径,再make,就可以啦,是的,根/子Makefile都会知道你的opencv.pc文件的路径
3: 如果我不在终端设置opencv.pc文件的路径呢?我就是想在CMake/Makefile文件弄呢?下面是源码。

需要注意的点

在Makefile引入shell命令,该命令执行完该shell就over了,所以export PKG_CONFIG_PATH=/home/topeet/programfile/opencv2/lib/pkgconfig写了两遍,每个shell是独立的,上面讲了,OpenCV_INC 获取OpenCV头文件路径,OpenCV_LIBS 获取OpenCV链接路径

Cmake添加

SET(ENV{PKG_CONFIG_PATH} /home/topeet/programfile/opencv2/lib/pkgconfig)

不同背景色

参考链接

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值