vscode配置opencv2.4.9踩坑教程
主要用到的工具MinGW-w64和CMake,需注意版本兼容问题,参考网址https://github.com/huihut/OpenCV-MinGW-Build
(1)MinGW-w64下载与安装
MinGW-w64离线包下载地址: https://sourceforge.net/projects/mingw-w64/files/ 下载后解压到自选路径。找到下图的文件夹位置,并添加bin文件到环境变量中。 D:\software\mingw64\bin 检查安装:
(2)安装cmake
CMake下载地址:https://cmake.org/download/ 选择Binary distributions栏目。下载完后解压到自选路径,添加该路径到环境变量,如:D:\software\cmake-3.12.4-win64-x64\bin。检查安装
最后双击打开cmake-gui。
(3)生成opencv的Makefiles文件
主要需要opencv的sources源文件。 打开cmake-gui后选择opencv的源文件路径和makefiles的保存位置。在点击configure时会弹出以下配置: 其中下拉框选择的是MinGW,然后点击Next,随后选择编译工具gcc和g++的路径。 执行过程中消息框会出现一堆红色信息,最后显示Configure done,是正常的。如果执行时中断,则存在其他问题。在执行完后,勾选WITH_OPENGL和BUILD_EXAMPLES,不勾选WITH_IPP、WITH_MSMF和ENABLE_PRECOMPILED_HEADERS(如果有的话),CPU_DISPATCH选空。 最后点击generate。
(4)opencv249编译
cmd到MakeFiles所在文件夹,执行minGW32-make命令(可选-j16等):
期间opencv_contrib可能存在编译出错的问题,主要解决方式: 进入E:\opencv\build\x64\mingw\modules\contrib\CMakeFiles文件下,打开opencv_contrib.dir文件夹下flags.make文件,然后删掉“ -Werror=address”,重新编译既可。 最后将编译完成的opencv库文件目录路径添加到环境变量中,如我的路径为: E:\opencv\build\x64\mingw\bin
(5)Vscode配置
在VScode中安装C/C++扩展,添加以下三个json文件: launch.json的配置为:
{
"version": "0.2.0",
"configurations": [
{
"name": "opencv_test",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "D:/kcs_software/mingw64/bin/gdb.exe",
"setupCommands": [
{
"description": "gdb start align print",
"text": "-enable-pretty-printing",
"ignoreFailures": false
}
],
"preLaunchTask": "g++"
}
]
}
tasks.json的配置为:
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "g++",
"command": "D:/kcs_software/mingw64/bin/g++.exe",
"args": [
"-g",
"${file}",
"-o",
"${workspaceFolder}\\${fileBasenameNoExtension}.exe",
"-I",
"E:/opencv/build/x64/mingw/install/include",
"-I",
"E:/opencv/build/x64/mingw/install/include/opencv",
"-I",
"E:/opencv/build/x64/mingw/install/include/opencv2",
"-L", "E:/opencv/build/x64/mingw/install/x64/mingw/lib",
"-l", "libopencv_core249",
"-l", "libopencv_features2d249",
"-l", "libopencv_flann249",
"-l","libopencv_highgui249",
"-l","libopencv_imgproc249",
"-l","libopencv_legacy249",
"-l","libopencv_ml249",
"-l","libopencv_nonfree249",
"-l","libopencv_objdetect249",
"-l","libopencv_stitching249",
"-l","libopencv_superres249",
"-l","libopencv_photo249",
"-l","libopencv_video249",
],
"options": {
"cwd": "D:/kcs_software/mingw64/bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"presentation": {
"echo":true,
"reveal": "always",
"focus":false,
"panel": "shared"
},
}
]
}
c_cpp_properties.json的配置为:
{
"configurations": [
{
"name": "windows-gcc-x64",
"includePath": [
"${workspaceFolder}/**",
"E:/opencv/build/x64/mingw/install/include",
"E:/opencv/build/x64/mingw/install/include/opencv2",
"E:/opencv/build/x64/mingw/install/include/opencv"
],
"compilerPath": "D:/kcs_software/mingw64/bin/gcc.exe",
"cStandard": "${default}",
"cppStandard": "${default}",
"intelliSenseMode": "windows-gcc-x64",
"compilerArgs": [
"-Wall",
"-Wextra",
"-Wpedantic"
]
}
],
"version": 4
}
settings.json的配置为:
{
"C_Cpp_Runner.cCompilerPath": "D:/kcs_software/mingw64/bin/gcc.exe",
"C_Cpp_Runner.cppCompilerPath": "D:/kcs_software/mingw64/bin/g++.exe",
"C_Cpp_Runner.debuggerPath": "D:/kcs_software/mingw64/bin/gdb.exe",
"C_Cpp_Runner.warnings": [
"-Wall",
"-Wextra",
"-Wpedantic"
],
"C_Cpp_Runner.compilerArgs": [],
"C_Cpp_Runner.includePaths": [],
"C_Cpp_Runner.linkerArgs": [],
"C_Cpp_Runner.cStandard": "",
"C_Cpp_Runner.cppStandard": "",
"C_Cpp_Runner.excludeSearch": [],
"C_Cpp_Runner.enableWarnings": true,
"C_Cpp_Runner.warningsAsError": false,
"cmake.configureOnOpen": true
}
验证程序,Ctrl+Shift+P或F5编译运行即可:
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace std;
using namespace cv;
int main()
{
cout<<"hello world"<<endl;
Mat img = imread("E:/cifar10/dog.png");
imshow("111",img);
waitKey(0);
system("pause");
return 0;
}
参考文档: [1]https://www.cnblogs.com/kensporger/p/12320622.html [2]https://blog.csdn.net/zhaiax672/article/details/88971248
|