什么是pkg-config?为什么使用pkg-config?

电子说

1.3w人已加入

描述

什么是pkg-config

pkg-config是一个linux下的命令,用于获得某一个库/模块的所有编译相关的信息,使用这个工具,我们可以很方便地编译一个项目。

为什么使用pkg-config

在linux中,经常会安装很多第三方的模块,我们在编译链接他们的时候需要制定头文件、库文件的位置,以及库文件的名字等等。

就以OpenCV为例子,如果我们用g++编译,就需要指定头文件库文件的相关信息,但是因为OpenCV依赖的库文件很多,我们指定的时候需要链接很多库,这就很麻烦了,比如

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

用pkg-config就可以解决这个问题,使用很简单:

g++ DisplayImage.cpp $(pkg-config --cflags --libs opencv4)

其实就是用pkg-config --cflags --libs opencv4这个命令代替了-I -L -lxxx.so等指令。

其实构建OpenCV项目还有一种方法是cmake,关于cmake的使用方法,我们以后再介绍。

怎么使用pkg-config

这篇文建是就是根据官方文档介绍写的,大家以后需要安装什么模块,查看使用方法的,尽量都去官网看一看。

pc文件

pkg-config读后缀为pc的配置文件的,pkg-config怎么知道pc文件在哪呢?有两个方法

1 官网中这样说的

On a typical Unix system, it will search in the directories /usr/lib/pkgconfig and /usr/share/pkgconfig

所以pc文件的路径就在

  • /usr/lib/pkgconfig
  • /usr/share/pkgconfig 这两个目录下。

2 第二种就是修改PKG_CONFIG_PATH环境变量,将pc文件的路径添加到这个环境变量中

我们可以执行pkg-config --variable pc_path pkg-config来查看当前pkg-config会搜索的路径

示例

接下来我们以opencv4.pc来简单介绍下。

# Package Information for pkg-config


prefix=/usr/local
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir_old=${prefix}/include/opencv4/opencv
includedir_new=${prefix}/include/opencv4


Name: OpenCV
Description: Open Source Computer Vision Library
Version: 4.2.0
Libs: -L${exec_prefix}/lib -lopencv_gapi -lopencv_stitching -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_cudabgsegm -lopencv_cudafeatures2d -lopencv_cudaobjdetect -lopencv_cudastereo -lopencv_dnn_objdetect -lopencv_dnn_superres -lopencv_dpm -lopencv_highgui -lopencv_face -lopencv_freetype -lopencv_fuzzy -lopencv_hfs -lopencv_img_hash -lopencv_line_descriptor -lopencv_quality -lopencv_reg -lopencv_rgbd -lopencv_saliency -lopencv_stereo -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_superres -lopencv_cudacodec -lopencv_surface_matching -lopencv_tracking -lopencv_datasets -lopencv_text -lopencv_dnn -lopencv_plot -lopencv_videostab -lopencv_cudaoptflow -lopencv_optflow -lopencv_cudalegacy -lopencv_videoio -lopencv_cudawarping -lopencv_xfeatures2d -lopencv_shape -lopencv_ml -lopencv_ximgproc -lopencv_video -lopencv_xobjdetect -lopencv_objdetect -lopencv_calib3d -lopencv_imgcodecs -lopencv_features2d -lopencv_flann -lopencv_xphoto -lopencv_photo -lopencv_cudaimgproc -lopencv_cudafilters -lopencv_imgproc -lopencv_cudaarithm -lopencv_core -lopencv_cudev
Libs.private: -lm -lpthread -lcudart_static -ldl -lrt -lnppc -lnppial -lnppicc -lnppicom -lnppidei -lnppif -lnppig -lnppim -lnppist -lnppisu -lnppitc -lnpps -lcublas -lcudnn -lcufft -L-L/usr/local/cuda -llib64 -L-L/usr/lib -lx86_64-linux-gnu
Cflags: -I${includedir_old} -I${includedir_new}

1 参数说明

  • Name:模块的名字OpenCV,注意,pkg-config是按照文件名字来查找,和这儿的name无关
  • Description:简单的描述
  • Version:模块的版本信息
  • Libs:模块依赖的lib的信息
  • Libs.private:木块需要但是并未公开出炉的链接库
  • Cflags:模块的编译器标志

2 执行

$ pkg-config  --modversion opencv4
4.2.0


$ pkg-config  --cflags opencv4
-I/usr/local/include/opencv4/opencv -I/usr/local/include/opencv4


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

最后再来看一下编译的指令,是不是就简单了呢

g++ DisplayImage.cpp $(pkg-config --cflags --libs opencv4)

审核编辑:刘清

打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分