Sublime+cmake+ming64搭建opencv c++语言编译环境

时间:2024-05-23 08:59:23

一、系统和软件环境:
(1) windows10
(2) mingw-w64-x86_64
(3) cmake-3.16.3-win64-x64
(4) opencv-4.2.0-vc14_vc15.exe
(5) Sublime Text Build 3211 x64 Setup

二、安装mingw-w64-x86_64
百度搜索mingw,下载并双击 ,然后点击install:
Sublime+cmake+ming64搭建opencv c++语言编译环境
这是一个在线安装包,下载安装会获取 sf上的GNU编译套件的资源。
Sublime+cmake+ming64搭建opencv c++语言编译环境
Sublime+cmake+ming64搭建opencv c++语言编译环境

勾选这些包
Sublime+cmake+ming64搭建opencv c++语言编译环境
Sublime+cmake+ming64搭建opencv c++语言编译环境

如果勾选有改变,Apply Changes这个选项就可以按下,就选择这个 请求改变(换句话就是 下载,或者卸载看具体勾选)
Sublime+cmake+ming64搭建opencv c++语言编译环境
Sublime+cmake+ming64搭建opencv c++语言编译环境
下载完成后需要添加环境变量:
Sublime+cmake+ming64搭建opencv c++语言编译环境
然后在运行中查看对应的gcc版本:
Sublime+cmake+ming64搭建opencv c++语言编译环境
安装完成。

三、安装Cmake
Cmake只需要在其官网上下载相应的压缩包解压到自己选择的目录即可:
Sublime+cmake+ming64搭建opencv c++语言编译环境
Sublime+cmake+ming64搭建opencv c++语言编译环境

四、安装和编译opencv
运行opencv-4.2.0-vc14_vc15.exe,讲opencv的文件提取到自己指定的目录:
Sublime+cmake+ming64搭建opencv c++语言编译环境
打开cmake-gui:
Sublime+cmake+ming64搭建opencv c++语言编译环境
设置源码路径和编译文件路径:
Sublime+cmake+ming64搭建opencv c++语言编译环境
点击configure,设置编译器:
Sublime+cmake+ming64搭建opencv c++语言编译环境
Sublime+cmake+ming64搭建opencv c++语言编译环境

配置好后,再次点击Configure,然后点击Generate生成Makefile。
开始编译opencv,打开运行终端,进入你设置的编译路径,输入以下命令进行编译:(-j是使用多少个进程进行编译,可以根据自身计算机配置设置合理线程数):
Sublime+cmake+ming64搭建opencv c++语言编译环境

编译完成后会在相应目录下看到opencv的库文件,这些库文件将会用于各个编辑器(VS、sublime等)的设置:
Sublime+cmake+ming64搭建opencv c++语言编译环境
环境变量里也需要进行设置:
Sublime+cmake+ming64搭建opencv c++语言编译环境

五、安装和配置sublime
在sublime官网下载并双击安装:
安装完成后首先需要安装Package control的查件,这是sublime的管理查件,方便浏览、安装和卸载插件。安装package control的步骤在其官网有介绍:
https://packagecontrol.io/installation
Sublime+cmake+ming64搭建opencv c++语言编译环境
安装完后使用可能会遇到以下问题:
Sublime+cmake+ming64搭建opencv c++语言编译环境

Sublime Text3解决There are no packages available for installation问题:
打开Sublime Text,然后点击Preferences->Browse Packages…,进入一个Sublime Text的Packages目录,然后在网上搜搜下载可以找到的channel_v3.json文件拷贝到该目录下面:
Sublime+cmake+ming64搭建opencv c++语言编译环境

接着修改Package Control的用户配置,点击Preferences->Package Settings->Package Control->Settings – User,新增或者修改”channels”的配置信息,其路径为channel_v3.json文件的路径:
Sublime+cmake+ming64搭建opencv c++语言编译环境

再重启软件即可。

解决之后按下Ctrl+Shift+P调出命令版,输入Install,选择package Control:install package,如下图,等待出现相应的界面,然后需要需要安装的插件名称:
Sublime+cmake+ming64搭建opencv c++语言编译环境
Sublime+cmake+ming64搭建opencv c++语言编译环境

常用的一些插件包括sublimeCodeintel、anaconda、sublimePERL等,可以自行查找和安装,这里不再介绍。

下面再介绍sublime载入opencv库的步骤:
打开sublime,依次点击tools-build system-new build system,新创建opencv_gcc.sublime-build,可以参考如下代码并根据实际情况做修改:
Sublime+cmake+ming64搭建opencv c++语言编译环境
{
“cmd”: [“g++”, “file","o","{file}", "-o", "{file_path}/filebasename","I","D:/ProgramFiles/opencv/opencv/build/include/opencv2","I","D:/ProgramFiles/opencv/mingwbuild/install/include","L","D:/ProgramFiles/opencv/mingwbuild/bin","l","libopencvcalib3d420","l","libopencvcore420","l","libopencvdnn420","l","libopencvfeatures2d420","l","libopencvflann420","l","libopencvgapi420","l","libopencvhighgui420","l","libopencvimgcodecs420","l","libopencvimgproc420","l","libopencvml420","l","libopencvobjdetect420","l","libopencvphoto420","l","libopencvstitching420","l","libopencvvideo420","l","libopencvvideoio420"],"fileregex":"(..[:]):([09]+):?([09]+)?:?(.){file_base_name}", "-I", "D:/Program Files/opencv/opencv/build/include/opencv2", "-I", "D:/Program Files/opencv/mingw_build/install/include", "-L", "D:/Program Files/opencv/mingw_build/bin", "-l", "libopencv_calib3d420","-l", "libopencv_core420", "-l", "libopencv_dnn420", "-l", "libopencv_features2d420", "-l", "libopencv_flann420","-l", "libopencv_gapi420", "-l", "libopencv_highgui420", "-l", "libopencv_imgcodecs420","-l", "libopencv_imgproc420", "-l", "libopencv_ml420", "-l","libopencv_objdetect420", "-l", "libopencv_photo420", "-l", "libopencv_stitching420","-l", "libopencv_video420", "-l", "libopencv_videoio420"], "file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)”,
“working_dir”: “KaTeX parse error: Expected '}', got 'EOF' at end of input: …un", "cmd" : ["{file_path}/${file_base_name}”]
}
]
}

上述代码需要修改为自己对应的路径。第2行是使用gcc++编译器的命令,因为设置了mingw’的环境变量,因此默认使用的g++工具为mingw64下的工具;3到4行,-I代表我们c程序里include的头文件所在的路径;第5行,-L表示我们编译生成的库文件所在的路径;6到10行就是编译链接需要用到的dll文件,将它们按照文件夹中的实际名称更改到代码中,名称中的数字是对应的版本号,完成后在tools-build system选择刚新建的opencv_gcc,就可以进行编译和运行了。

六、测试程序
利用opencv的库函数读一幅图片:

使用Ctrl+Shift+B可以选择编译还是运行,出现的选项中opencv_gcc为编译,opencv_gcc-Run为运行。Ctrl+B可以重复上次的操作,如果上次选择的是编译,则再进行编译;如果上次选择的是运行,则再进行运行。Sublime+cmake+ming64搭建opencv c++语言编译环境
Sublime+cmake+ming64搭建opencv c++语言编译环境

运行结果如下:

Sublime+cmake+ming64搭建opencv c++语言编译环境
在对应的目录下还有生成.exe的可执行文件。
至此,利用sublime+ cmake+ming64搭建opencv c++语言编译环境就介绍到这里了。

参考资料:
[1] https://blog.****.net/u012997311/article/details/51423137
[2] https://blog.****.net/u010798503/article/details/88065863
[3] https://blog.****.net/xjw13896534797/article/details/87717520