pkg-config 与gcc 配合的使用

我们平常都是这样用pkg-config的
$gcc main.c pkg-config --cflags --libs gtk+-2.0 -o main
上面的编译命令中,pkg-config --cflags --libs gtk+-2.0的作用就如前面所说的,把gtk的头文件路径和库文件列出来,让编译去获取。–cflags和–libs分别指定头文件和库文件。

pkg-config 是在 PKG_CONFIG_PATH 目录下寻找pc文件来确定各个包的头文件的目录和库的目录
默认在这里面有
/usr/local/lib/pkgconfig

举例opencv.pc


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.10
Libs: -L${exec_prefix}/lib -lopencv_shape -lopencv_dnn -lopencv_highgui -lopencv_objdetect -lopencv_viz -lopencv_superres -lopencv_videostab -lopencv_video -lopencv_photo -lopencv_ml -lopencv_stitching -lopencv_calib3d -lopencv_features2d -lopencv_flann -lopencv_videoio -lopencv_imgcodecs -lopencv_imgproc -lopencv_core
Libs.private: -ldl -lm -lpthread -lrt
Cflags: -I${includedir_old} -I${includedir_new}


随便写一个opencv 程序如下

#include "highgui.h" 

int main(int argc, char** argv)
{
    const char *imgpath = "opencv.jpg";
    IplImage *img = cvLoadImage(imgpath);//加载图像文件至内存
    cvNamedWindow("Example1", CV_WINDOW_AUTOSIZE);//创建一个名为Example1的窗口
    cvShowImage("Example1", img);//显示图像
    cvWaitKey(0);//等待用户触发按键
    cvReleaseImage(&img);//释放图片占用的内存
    cvDestroyWindow("Example1");//销毁窗口

    return 0;
}

保存然后使用pkg-config 来寻找对的头文件和库,最后使用gcc编译
链接对应的头文件
pkg-config --cflags opencv
这句话对应的就是

pkg-config --cflags opencv

链接对应的库
pkg-config --libs opencv

-L/usr/local/lib -lopencv_shape -lopencv_dnn -lopencv_highgui -lopencv_objdetect -lopencv_viz -lopencv_superres -lopencv_videostab -lopencv_video -lopencv_photo -lopencv_ml -lopencv_stitching -lopencv_calib3d -lopencv_features2d -lopencv_flann -lopencv_videoio -lopencv_imgcodecs -lopencv_imgproc -lopencv_core

完整使用gcc编译代码如下

gcc opcv.cpp `pkg-config --cflags --libs opencv` -o opencvtest

你可能感兴趣的:(编译)