1.添加环境变量
D:\mingw64\bin
2.安装vscode
3.下载opencv 4.8.0
4.程序引用第三方库(opencv为例)
打开CMakeLists.txt,引入头文件,使用include_directories 加入头文件所在目录。静态链接库link_directories
# 头文件
include_directories(D:/opencv4.8.0/opencv/build/include)
# 静态链接库
link_directories(D:/opencv4.8.0/opencv/build/x64/vc16/lib)
在main 函数所在文件 添加 #pragma comment(lib,“xxx.lib”),如果当前程序是Debug
加 opencv_world480d.lib;否则加opencv_world480.lib
#pragma comment(lib, "opencv_world480d.lib")
5.测试代码
#include <opencv2/opencv.hpp>
#include <iostream>
#pragma comment(lib, "opencv_world480d.lib")
using namespace cv;
using namespace std;int main(int argc, char** argv) {Mat src = imread("D:/Work/opencv_test/images/1.jpeg");if (src.empty()) {cout << "could not load image..." << endl;return -1;}imshow("input", src);waitKey(0);return 0;
}
CMakeLists.txt
cmake_minimum_required(VERSION 3.28.0)
project(cpp_test)set(CMAKE_GENERATOR "MinGW Makefiles")
# 头文件
include_directories(D:/opencv4.8.0/opencv/build/include)
# 静态链接库
link_directories(D:/opencv4.8.0/opencv/build/x64/vc16/lib)
add_executable(app test01.cpp)
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
tasks.json
{"version": "2.0.0","options": {"cwd": "${workspaceFolder}/build"},"tasks": [{"type": "shell","label": "cmake","command": "cmake","args": [".."]},{"label": "make","group": "build","command": "make","args": [],"problemMatcher": []},{"label": "Build","dependsOrder": "sequence","dependsOn": ["cmake","make"]},{"type": "cppbuild",//"label": "C/C++: g++ 生成活动文件","label": "Build",// "command": "/usr/bin/g++","command": "D://mingw64//bin//g++.exe","args": ["-fdiagnostics-color=always","-g","-o","${workspaceFolder}/bin/Debug/app.exe","-fexec-charset=GBK"],"options": {"cwd": "${workspaceFolder}"},"problemMatcher": ["$gcc"],"group": {"kind": "build","isDefault": true},"detail": "D://mingw64//bin//g++.exe"}]
}
launch.json
{// 使用 IntelliSense 了解相关属性。 // 悬停以查看现有属性的描述。// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387"version": "0.2.0","configurations": [{"name": "(gdb) 启动","type": "cppdbg","request": "launch","program": "${workspaceFolder}/bin/Debug/app.exe","args": ["-I", "D:\\opencv4.8.0\\opencv\\build\\include",//改成你自己的"-I", "D:\\opencv4.8.0\\opencv\\build\\include\\opencv2",//改成你自己的],"stopAtEntry": false,"cwd": "${workspaceFolder}","environment": [],"externalConsole": false,"MIMode": "gdb","setupCommands": [{"description": "为 gdb 启用整齐打印","text": "-enable-pretty-printing","ignoreFailures": true},],"preLaunchTask": "Build","miDebuggerPath": "D://mingw64//bin//gdb.exe", // 修改为你的 gdb 路径},]
}
参考文章:
VScode编写C++概述(VScode 使用opencv与Eigen为例)_vscode c++使用opencv msvc-CSDN博客https://blog.csdn.net/euphorias/article/details/120783669
windows环境下cmake创建MinGW类型makefile报错_cmake error: error: generator : mingw makefiles-CSDN博客https://blog.csdn.net/weixin_48876595/article/details/129414407