ubuntu下clion中配置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为当前项目的名称



你可能感兴趣的:(ubuntu,opencv,clion)