什么是pkg-config
pkg-config是一個(gè)linux下的命令,用于獲得某一個(gè)庫(kù)/模塊的所有編譯相關(guān)的信息,使用這個(gè)工具,我們可以很方便地編譯一個(gè)項(xiàng)目。
為什么使用pkg-config
在linux中,經(jīng)常會(huì)安裝很多第三方的模塊,我們?cè)诰幾g鏈接他們的時(shí)候需要制定頭文件、庫(kù)文件的位置,以及庫(kù)文件的名字等等。
就以O(shè)penCV為例子,如果我們用g++編譯,就需要指定頭文件庫(kù)文件的相關(guān)信息,但是因?yàn)镺penCV依賴的庫(kù)文件很多,我們指定的時(shí)候需要鏈接很多庫(kù),這就很麻煩了,比如
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è)問(wèn)題,使用很簡(jiǎn)單:
g++ DisplayImage.cpp $(pkg-config --cflags --libs opencv4)
其實(shí)就是用pkg-config --cflags --libs opencv4這個(gè)命令代替了-I -L -lxxx.so等指令。
其實(shí)構(gòu)建OpenCV項(xiàng)目還有一種方法是cmake,關(guān)于cmake的使用方法,我們以后再介紹。
怎么使用pkg-config
這篇文建是就是根據(jù)官方文檔介紹寫(xiě)的,大家以后需要安裝什么模塊,查看使用方法的,盡量都去官網(wǎng)看一看。
pc文件
pkg-config讀后綴為pc的配置文件的,pkg-config怎么知道pc文件在哪呢?有兩個(gè)方法
1 官網(wǎng)中這樣說(shuō)的
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 這兩個(gè)目錄下。
2 第二種就是修改PKG_CONFIG_PATH環(huán)境變量,將pc文件的路徑添加到這個(gè)環(huán)境變量中
我們可以執(zhí)行pkg-config --variable pc_path pkg-config來(lái)查看當(dāng)前pkg-config會(huì)搜索的路徑
示例
接下來(lái)我們以opencv4.pc來(lái)簡(jiǎn)單介紹下。
# 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 參數(shù)說(shuō)明
- Name:模塊的名字OpenCV,注意,pkg-config是按照文件名字來(lái)查找,和這兒的name無(wú)關(guān)
- Description:簡(jiǎn)單的描述
- Version:模塊的版本信息
- Libs:模塊依賴的lib的信息
- Libs.private:木塊需要但是并未公開(kāi)出爐的鏈接庫(kù)
- Cflags:模塊的編譯器標(biāo)志
2 執(zhí)行
$ 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
最后再來(lái)看一下編譯的指令,是不是就簡(jiǎn)單了呢
g++ DisplayImage.cpp $(pkg-config --cflags --libs opencv4)
審核編輯:劉清
-
OpenCV
+關(guān)注
關(guān)注
31文章
635瀏覽量
41418 -
LINUX內(nèi)核
+關(guān)注
關(guān)注
1文章
316瀏覽量
21688
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論