pkg-config妙用

1.makefile 中选项

cflags 指定头文件路径 eg: CFLAGS=-I/usr/include

LDFLAGS: 库文件位置 eg: LDFLAGS=-L/usr/lib -L/path/to/your/lib

LIBS:告诉链接器要链接哪些库文件,如LIBS = -lpthread -liconv

gcc 选项: -I -L

2.pkg-config

LInux命令,获得某个库/模块的所有编译相关的信息

pkg-config opencv –libs –cflags 查看opencv头文件和库文件

输出:

-I/usr/local/include/opencv4/opencv -I/usr/local/include/opencv4 
-L/usr/local/lib 
-lopencv_gapi -lopencv_stitching -lopencv_superres -lopencv_videostab -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_dpm -lopencv_face -lopencv_photo -lopencv_fuzzy -lopencv_hdf -lopencv_hfs -lopencv_img_hash -lopencv_line_descriptor -lopencv_optflow -lopencv_reg -lopencv_rgbd -lopencv_saliency -lopencv_sfm -lopencv_stereo -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_surface_matching -lopencv_tracking -lopencv_video -lopencv_datasets -lopencv_plot -lopencv_xfeatures2d -lopencv_shape -lopencv_ml -lopencv_ximgproc -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_flann -lopencv_xobjdetect -lopencv_imgcodecs -lopencv_objdetect -lopencv_xphoto -lopencv_imgproc -lopencv_core

好处:所有用opencv的其他程序,在编译时,只需要写“pkg-config opencv –libs –cflags”,而不需要自己去找opencv的头文件在哪里,要链接的库在哪里!省时省力! 
如果你写了一个库,不管是静态的还是动态的,要提供给第三方使用,那除了给人家库/头文件,最好也写一个pc文件,这样别人使用就方便很多,不用自己再手动写依赖了你哪些库,只需要敲一个”pkg-config [YOUR_LIB] –libs –cflags”。 
pkg信息在PKG_CONFIG_PATH环境变量下,里面有各个pc文件,如opencv的.

使用pkg-config 编译自己程序写法为:gcc main.c `pkg-config --cflags --libs opencv` -o main

你可能感兴趣的:(linux编程相关)