pkg-config 查询已安装库的相关信息
简述
pkg-config 是一个在源代码编译时查询已安装的库的使用接口的计算机工具软件。
输出已安装库的相关信息:
- C/C++ 编译器需要的输入参数
- 链接器需要的输入参数
- 已安装软件包的版本信息
工作原理
当安装一个库时(例如从 RPM,deb 或其他二进制包管理系统),会包括一个后缀名为 pc 的文件,它会放入某个文件夹下(依赖于你的系统设置)。
例如,在 Linux 为该软件的库文件所在文件夹 lib 之下的子文件夹 pkgconfig。
并把该子文件夹加入 pkg-config 的环境变量 PKG_CONFIG_PATH
作为搜索路径,例如在 bash 配置文件中加入一行:
$ export PKG_CONFIG_PATH=/usr/local/`库的名字`/lib/pkgconfig:$PKG_CONFIG_PATH
比如查看一下 opencv 的 pkgconfig:
$ cat /usr/local/lib/pkgconfig/opencv.pc
# Package Information for pkg-config
prefix=/usr/local
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir_old=${prefix}/include/opencv
includedir_new=${prefix}/include
Name: OpenCV
Description: Open Source Computer Vision Library
Version: 3.4.5
Libs: -L${exec_prefix}/lib -lopencv_stitching -lopencv_superres -lopencv_videostab -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_dnn_objdetect -lopencv_dpm -lopencv_face -lopencv_freetype -lopencv_fuzzy -lopencv_hdf -lopencv_hfs -lopencv_img_hash -lopencv_line_descriptor -lopencv_optflow -lopencv_reg -lopencv_rgbd -lopencv_saliency -lopencv_stereo -lopencv_structured_light -lopencv_viz -lopencv_phase_unwrapping -lopencv_surface_matching -lopencv_tracking -lopencv_datasets -lopencv_text -lopencv_dnn -lopencv_plot -lopencv_xfeatures2d -lopencv_shape -lopencv_video -lopencv_ml -lopencv_ximgproc -lopencv_xobjdetect -lopencv_objdetect -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_flann -lopencv_xphoto -lopencv_photo -lopencv_imgproc -lopencv_core
Libs.private: -ldl -lm -lpthread -lrt
Cflags: -I${includedir_old} -I${includedir_new}
pkg-config 使用
查看 help :
$ pkg-config --help
Usage:
pkg-config [OPTION?]
Help Options:
-h, --help Show help options
Application Options:
--version output version of pkg-config
--modversion output version for package
--atleast-pkgconfig-version=VERSION require given version of pkg-config
--libs output all linker flags
--static output linker flags for static linking
--short-errors print short errors
--libs-only-l output -l flags
--libs-only-other output other libs (e.g. -pthread)
--libs-only-L output -L flags
--cflags output all pre-processor and compiler flags
--cflags-only-I output -I flags
--cflags-only-other output cflags not covered by the cflags-only-I option
--variable=NAME get the value of variable named NAME
--define-variable=NAME=VALUE set variable NAME to VALUE
--exists return 0 if the module(s) exist
--print-variables output list of variables defined by the module
--uninstalled return 0 if the uninstalled version of one or more module(s) or their dependencies will be used
--atleast-version=VERSION return 0 if the module is at least version VERSION
--exact-version=VERSION return 0 if the module is at exactly version VERSION
--max-version=VERSION return 0 if the module is at no newer than version VERSION
--list-all list all known packages
--debug show verbose debug information
--print-errors show verbose information about missing or conflicting packages (default unless --exists or --atleast/exact/max-version given on the command line)
--silence-errors be silent about errors (default when --exists or --atleast/exact/max-version given on the command line)
--errors-to-stdout print errors from --print-errors to stdout not stderr
--print-provides print which packages the package provides
--print-requires print which packages the package requires
--print-requires-private print which packages the package requires for static linking
--validate validate a package's .pc file
--define-prefix try to override the value of prefix for each .pc file found with a guesstimated value based on the location of the .pc file
--dont-define-prefix don't try to override the value of prefix for each .pc file found with a guesstimated value based on the location of the .pc file
--prefix-variable=PREFIX set the name of the variable that pkg-config automatically sets
- 查看 opencv 依赖的头文件路径:
$ pkg-config --cflags opencv
-I/usr/local/include/opencv -I/usr/local/include
- 查看 opencv 依赖的库文件路径:
$ pkg-config --libs opencv
-L/usr/local/lib -lopencv_stitching -lopencv_superres -lopencv_videostab -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_dnn_objdetect -lopencv_dpm -lopencv_face -lopencv_freetype -lopencv_fuzzy -lopencv_hdf -lopencv_hfs -lopencv_img_hash -lopencv_line_descriptor -lopencv_optflow -lopencv_reg -lopencv_rgbd -lopencv_saliency -lopencv_stereo -lopencv_structured_light -lopencv_viz -lopencv_phase_unwrapping -lopencv_surface_matching -lopencv_tracking -lopencv_datasets -lopencv_text -lopencv_dnn -lopencv_plot -lopencv_xfeatures2d -lopencv_shape -lopencv_video -lopencv_ml -lopencv_ximgproc -lopencv_xobjdetect -lopencv_objdetect -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_flann -lopencv_xphoto -lopencv_photo -lopencv_imgproc -lopencv_core
- 查看 opencv 版本:
$ pkg-config --modversion opencv
3.4.5
编译 opencv 测试样例
由于 opencv 依赖比较多,编译时管理的头文件和库文件也多。
通常的写法都比较复杂,可以利用 pkg-config
进行归纳。
# 编译
g++ test_opencv.cpp `pkg-config --libs --cflags opencv`
# 运行
./a.out
版权声明:本文为wilson1068原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。