Clion + opencv环境搭建(体验最好的C++ IDE)
前言:
一个好的开发环境,是程序猿梦寐以求的,对于opencv的开发,一直觉得vs虽然牛逼但太庞大,所以后来用了codeblocks,然后又觉得无论是vs还是codeblocks都不够美观,代码提示也不太好,配置也很繁琐。然后,我就在网上搜好用的C++ IDE,这不,逮到了Clion,一直很怀念当年做web开发时用的IntelliJ IDEA,这个Clion也是来自Jetbrains家族的,所以美观方面可谓无可挑剔。下面就开始来搭建吧。
搭建:
1、下载下来最新的2016.3.3版本的Clion,很简单,默认安装。
2、minGW编译器,网上下载个绿色版本的,我这里用的是minGW3.20的版本,解压在某处,打开Clion就在setting的时候选择一下编译器所在目录就行
3、需要用minGW编译opencv,参考我另外一篇文章Codeblocks + opencv + Cmake + minGW 环境搭建(一劳永逸版)
示例项目:
新建一个项目hello,只要配置一下cmake的list文件,然后在main.cpp写个简单的显示lena的代码就行了
cmake_minimum_required(VERSION 3.6) project(hello) set(CMAKE_CXX_STANDARD 11) # 包含目录 include_directories(C:\\\\opencv\\\\include) include_directories(C:\\\\opencv\\\\include\\\\opencv) include_directories(C:\\\\opencv\\\\include\\\\opencv2) # 源文件 set(SOURCE_FILES main.cpp) # minGW编译opencv的dll.a库 set(TARGET_LIBS "C:\\\\opencv\\\\lib\\\\libopencv_core244.dll.a" "C:\\\\opencv\\\\lib\\\\libopencv_highgui244.dll.a" "C:\\\\opencv\\\\lib\\\\libopencv_imgproc244.dll.a" ) add_executable(hello ${SOURCE_FILES}) target_link_libraries(hello ${TARGET_LIBS})
# minGW编译opencv的dll.a库(所有17个) set(TARGET_LIBS "C:\\\\opencv\\\\lib\\\\libopencv_core244.dll.a" "C:\\\\opencv\\\\lib\\\\libopencv_highgui244.dll.a" "C:\\\\opencv\\\\lib\\\\libopencv_imgproc244.dll.a" "C:\\\\opencv\\\\lib\\\\libopencv_calib3d244.dll.a" "C:\\\\opencv\\\\lib\\\\libopencv_contrib244.dll.a" "C:\\\\opencv\\\\lib\\\\libopencv_features2d244.dll.a" "C:\\\\opencv\\\\lib\\\\libopencv_flann244.dll.a" "C:\\\\opencv\\\\lib\\\\libopencv_gpu244.dll.a" "C:\\\\opencv\\\\lib\\\\libopencv_nonfree244.dll.a" "C:\\\\opencv\\\\lib\\\\libopencv_ts244.a" "C:\\\\opencv\\\\lib\\\\libopencv_video244.dll.a" "C:\\\\opencv\\\\lib\\\\libopencv_videostab244.dll.a" "C:\\\\opencv\\\\lib\\\\libopencv_stitching244.dll.a" "C:\\\\opencv\\\\lib\\\\libopencv_photo244.dll.a" "C:\\\\opencv\\\\lib\\\\libopencv_objdetect244.dll.a" "C:\\\\opencv\\\\lib\\\\libopencv_ml244.dll.a" "C:\\\\opencv\\\\lib\\\\libopencv_legacy244.dll.a" )
#include <iostream> # include<opencv2/core/core.hpp> # include<opencv2/highgui/highgui.hpp> # include<opencv2/imgproc/imgproc.hpp> using namespace cv; using namespace std; int main() { Mat img = imread("../lena.png"); resize(img,img,Size(500,500)); imshow("img",img); waitKey(); destroyAllWindows(); return 0; }
其实这里最重要的Cmakelist.txt的配置,此步简化了配置,不用再像vs和codeblocks那样一点一点选择库目录和包含目录,很方便。
这里有个问题需要注意一下,Clion用Cmake来管理程序的编译生成和运行,exe生成在cmake下面,因此代码中的lena文件需要用"../lena.png",才能正常访问。
好了,这样就可以用Clion来写opencv的图像程序了,体验很不错。