配置opencv的CMakeLists.txt

在Clion中运行程序
#include <opencv2/opencv.hpp>
#include <stdio.h>
#include "opencv/highgui.h"
using namespace std;
int main()
{
    IplImage *img=cvLoadImage("image/first-5.bmp");
    cvNamedWindow("example", CV_WINDOW_AUTOSIZE);
    cvShowImage("example",img);
    cvWaitKey(0);
    cvReleaseImage(&img);
    cvDestroyWindow("example");
}

会报错误:

/home/johnqi/ClionProjects/opencv/main.cpp:7:对‘cvLoadImage’未定义的引用
/home/johnqi/ClionProjects/opencv/main.cpp:8:对‘cvNamedWindow’未定义的引用
/home/johnqi/ClionProjects/opencv/main.cpp:9:对‘cvShowImage’未定义的引用
/home/johnqi/ClionProjects/opencv/main.cpp:10:对‘cvWaitKey’未定义的引用
/home/johnqi/ClionProjects/opencv/main.cpp:11:对‘cvReleaseImage’未定义的引用
/home/johnqi/ClionProjects/opencv/main.cpp:12:对‘cvDestroyWindow’未定义的引用

这主要的问题是配置CMakeLists.txt文件时出错导致的,应该在CMakeLists.txt文件中添加一句

target_link_libraries(opencv  -I/usr/local/include/opencv -I/usr/local/include  /usr/local/lib/libopencv_calib3d.so /usr/local/lib/libopencv_contrib.so /usr/local/lib/libopencv_core.so /usr/local/lib/libopencv_features2d.so /usr/local/lib/libopencv_flann.so /usr/local/lib/libopencv_gpu.so /usr/local/lib/libopencv_highgui.so /usr/local/lib/libopencv_imgproc.so /usr/local/lib/libopencv_legacy.so /usr/local/lib/libopencv_ml.so /usr/local/lib/libopencv_nonfree.so /usr/local/lib/libopencv_objdetect.so /usr/local/lib/libopencv_photo.so /usr/local/lib/libopencv_stitching.so /usr/local/lib/libopencv_ts.so /usr/local/lib/libopencv_video.so /usr/local/lib/libopencv_videostab.so)

注意:不同的系统添加的链接库文件可能不同,具体的库文件可以通过在ubuntu终端下输入命令pkg-config --cflags --libs opencv获取.

其中opencv为当前项目的名称

如果为mac电脑需要先安装brew,然后再

brew install pkg-config --cflags --libs opencv
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

友情链接更多精彩内容