本文使用MinGW编译器, 详情配置请看该链接
为了编译opencv库, 需要安装cmake程序。
我的opencv build完成后文件部署情况:
在C/C++项目目录下创建.vscode
文件,需要创建c_cpp_properties.json
、launch.json
和tasks.json
这3个文件。
配置过程中一定要注意原biild文件
和cmake程序build文件。
c_cpp_properties.json 配置
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
// 引入opencv库
"E:\\codeLib\\opencv\\opencv\\build\\include", // 原build文件夹下
"E:\\codeLib\\opencv\\opencv\\build\\include\\opencv2", // 原build文件下
"E:\\codeLib\\opencv\\opencv\\mingw_build\\lib" // mingw_build为cmake build后的文件夹下
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.19041.0",
"compilerPath": "D:/software/Tools/gcc/mingw64/bin/g++.exe",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "gcc-x64"
}
],
"version": 4
}
launch.json
文件不需要修改{
"configurations": [
{
"name": "C/C++: cpp.exe 生成和调试活动文件",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "D:\\software\\Tools\\gcc\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "将反汇编风格设置为 Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: cpp.exe 生成活动文件"
}
],
"version": "2.0.0"
}
tasks.json
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe 生成活动文件",
"command": "D:\\software\\Tools\\gcc\\mingw64\\bin\\g++.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
// opencv cmake build文件下
"-I", "E:\\codeLib\\opencv\\opencv\\mingw_build\\install\\include",
"-I", "E:\\codeLib\\opencv\\opencv\\mingw_build\\install\\include\\opencv2",
"-L", "E:\\codeLib\\opencv\\opencv\\mingw_build\\lib",
"-l", "opencv_core455",
"-l", "libopencv_imgproc455",
"-l", "libopencv_video455",
"-l", "libopencv_ml455",
"-l", "libopencv_highgui455",
"-l", "libopencv_objdetect455",
"-l", "libopencv_flann455",
"-l", "libopencv_imgcodecs455",
"-l", "libopencv_photo455",
"-l", "libopencv_videoio455"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "调试器生成的任务。"
},
{
"type": "cppbuild",
"label": "C/C++: cpp.exe 生成活动文件",
"command": "D:\\software\\Tools\\gcc\\mingw64\\bin\\cpp.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
// opencv cmake build文件下
"-I", "E:\\codeLib\\opencv\\opencv\\mingw_build\\install\\include",
"-I", "E:\\codeLib\\opencv\\opencv\\mingw_build\\install\\include\\opencv2",
"-L", "E:\\codeLib\\opencv\\opencv\\mingw_build\\lib",
"-l", "opencv_core455",
"-l", "libopencv_imgproc455",
"-l", "libopencv_video455",
"-l", "libopencv_ml455",
"-l", "libopencv_highgui455",
"-l", "libopencv_objdetect455",
"-l", "libopencv_flann455",
"-l", "libopencv_imgcodecs455",
"-l", "libopencv_photo455",
"-l", "libopencv_videoio455"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": "build",
"detail": "调试器生成的任务。"
}
],
"version": "2.0.0"
}
#include
#include
using namespace cv;
int main()
{
std::string imagePath = R"(E:\picture\background\1.jpg)";
Mat img = imread(imagePath); //读取图片
imshow("img", img); //显示图片
waitKey(-1); //等待按键 不然会一闪而过
return 0;
}
运行成功,Nice !