查询工具pkg-config

在程序编译时候,通常需要引用外部库文件或者头文件,文件多了用gcc指定时候就显得麻烦,一个一个写在Makefile中也比较繁琐,这种情况下,能自动获取完整的依赖库信息就显得轻松不少了。

pkg-config简介及用法

关于pkg-config

pkg-config就是自动获取所需的库文件以及头文件的命令,原本设计用于Linux,不过后来其他系统如Win和Mac里也都有可用的版本。
主要用于输出C/C++的编译器需要的参数,链接器需要的参数以及已经安装软件版本等信息。

主要用法

其主要的命令参数以及解释如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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

其中用的比较多的就是--cflags--libs。主要用来输出头文件和lib文件。
如查找opencv头文件和lib库的位置:

1
pkg-config --cflags --libs opencv

输出:

1
-I/usr/local/include/opencv -I/usr/local/include -L/usr/local/lib -lopencv_cudabgsegm -lopencv_cudaobjdetect -lopencv_cudastereo -lopencv_stitching -lopencv_cudafeatures2d -lopencv_superres -lopencv_cudacodec -lopencv_videostab -lopencv_cudaoptflow -lopencv_cudalegacy -lopencv_cudawarping -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_dpm -lopencv_freetype -lopencv_fuzzy -lopencv_line_descriptor -lopencv_optflow -lopencv_reg -lopencv_saliency -lopencv_stereo -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_rgbd -lopencv_surface_matching -lopencv_tracking -lopencv_datasets -lopencv_text -lopencv_face -lopencv_plot -lopencv_dnn -lopencv_xfeatures2d -lopencv_shape -lopencv_video -lopencv_ximgproc -lopencv_calib3d -lopencv_features2d -lopencv_flann -lopencv_xobjdetect -lopencv_objdetect -lopencv_ml -lopencv_xphoto -lopencv_highgui -lopencv_videoio -lopencv_photo -lopencv_imgcodecs -lopencv_cudaimgproc -lopencv_cudafilters -lopencv_imgproc -lopencv_cudaarithm -lopencv_core -lopencv_cudev

再如Gstreamer的:

1
pkg-config --cflags --libs gstreamer-1.0

可以看到,

1
-pthread -I/usr/include/gstreamer-1.0 -I/usr/lib/x86_64-linux-gnu/gstreamer-1.0/include -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -lgstreamer-1.0 -lgobject-2.0 -lglib-2.0

关于路径的获取

pkg-config获取路径是通过查询第三方库的pc文件实现的。比如:

1
2
cd /usr/local/lib/pkgconfig
ls

输出:

1
2
flann.pc libavdevice.pc libavformat.pc libswresample.pc opencv.pc
libavcodec.pc libavfilter.pc libavutil.pc libswscale.pc

pc文件内容通常包含Name, Description, Version, Libs, Cflags等基本参数。如opencv的pc文件内容是:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 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.2.0
Libs: -L${exec_prefix}/lib -lopencv_cudabgsegm -lopencv_cudaobjdetect -lopencv_cudastereo -lopencv_stitching -lopencv_cudafeatures2d -lopencv_superres -lopencv_cudacodec -lopencv_videostab -lopencv_cudaoptflow -lopencv_cudalegacy -lopencv_cudawarping -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_dpm -lopencv_freetype -lopencv_fuzzy -lopencv_line_descriptor -lopencv_optflow -lopencv_reg -lopencv_saliency -lopencv_stereo -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_rgbd -lopencv_surface_matching -lopencv_tracking -lopencv_datasets -lopencv_text -lopencv_face -lopencv_plot -lopencv_dnn -lopencv_xfeatures2d -lopencv_shape -lopencv_video -lopencv_ximgproc -lopencv_calib3d -lopencv_features2d -lopencv_flann -lopencv_xobjdetect -lopencv_objdetect -lopencv_ml -lopencv_xphoto -lopencv_highgui -lopencv_videoio -lopencv_photo -lopencv_imgcodecs -lopencv_cudaimgproc -lopencv_cudafilters -lopencv_imgproc -lopencv_cudaarithm -lopencv_core -lopencv_cudev
Libs.private: -L/usr/lib/x86_64-linux-gnu -lpng -lz -ltiff -ljasper -ljpeg -lgtk-x11-2.0 -lgdk-x11-2.0 -lpangocairo-1.0 -latk-1.0 -lcairo -lgdk_pixbuf-2.0 -lgio-2.0 -lpangoft2-1.0 -lpango-1.0 -lgobject-2.0 -lglib-2.0 -lfontconfig -lfreetype -lgthread-2.0 -ldc1394 -lavcodec-ffmpeg -lavformat-ffmpeg -lavutil-ffmpeg -lswscale-ffmpeg -ldl -lm -lpthread -lrt -lcudart -lnppc -lnppi -lnpps -lcufft -L-L/usr/local/cuda -l64
Cflags: -I${includedir_old} -I${includedir_new}

所以也可以以同样的方式添加自己的第三方库来简化编译工作。

分享
匿名评论