win10+vscode+opencv(C++)环境搭建便捷版(无需cmake编译)

win10+vscode+opencv c++环境搭建——无需CMake编译

  • MinGW-w64安装
  • OpenCV-MinGW-Build
  • vscode配置
  • 常见问题

用vscode来写Opencv的c++代码需要自行编译Opencv,这期间主要用到的工具有MinGW-w64和CMake。由于自行编译的过程耗时繁复,并不会一帆风顺,常出现编译错误的现象,需要有极强的折腾的精神。

本博文使用他人已经编译过的Opencv库来搭建我们需要的开发环境,在解决好版本兼容问题后,能减少许多麻烦事。

MinGW-w64安装

MinGW是C++编译器,用于编译C++文件。

  • MinGW-w64离线包下载地址: link.
    跟据需要的版本对应下载
    win10+vscode+opencv(C++)环境搭建便捷版(无需cmake编译)_第1张图片

  • 下载后直接解压,将文件mingw64放到自选路径下。win10+vscode+opencv(C++)环境搭建便捷版(无需cmake编译)_第2张图片

  • 添加路径到环境变量Path,如:D:\mingw64\bin

  • 检查安装是否成功:
    win10+vscode+opencv(C++)环境搭建便捷版(无需cmake编译)_第3张图片

OpenCV-MinGW-Build

  • 访问网站:https://github.com/huihut/OpenCV-MinGW-Build
    该网站包含了已经编译过的Opencv库,同时还提供了现成的版本兼容信息,点击Configuration可以看到所对应的MinGW和CMake版本,故下载安装时就需要据此选择合适的MinGW版本。
    win10+vscode+opencv(C++)环境搭建便捷版(无需cmake编译)_第4张图片

  • 下载后解压到自选路径。win10+vscode+opencv(C++)环境搭建便捷版(无需cmake编译)_第5张图片

  • 添加路径到环境变量Path,如:D:\OpenCV-4.5.0\x64\mingw\binwin10+vscode+opencv(C++)环境搭建便捷版(无需cmake编译)_第6张图片

vscode配置

  • 验证程序
#include 
#include 
#include 
using namespace cv;

int main()
{
    Mat img=imread("man.jpg");
    imshow("result",img);
    waitKey();
    return 0;
}
 
  • launch.json配置,只需配置miDebuggerPath项
{  
    "version": "0.2.0",  
    "configurations": [  
        {  
            "name": "opencv4.5.0 debug", // 配置名称,将会在启动配置的下拉菜单中显示  
            "type": "cppdbg",       // 配置类型,这里只能为cppdbg  
            "request": "launch",    // 请求配置类型,可以为launch(启动)或attach(附加)  
            "program": "${workspaceFolder}/DeBug/${fileBasenameNoExtension}.exe",// 将要进行调试的程序的路径  
            "args": [],             // 程序调试时传递给程序的命令行参数,一般设为空即可  
            "stopAtEntry": false,   // 设为true时程序将暂停在程序入口处,一般设置为false  
            "cwd": "${workspaceFolder}", // 调试程序时的工作目录,一般为${workspaceFolder}即代码所在目录  
            "environment": [],  
            "externalConsole": true, // 调试时是否显示控制台窗口,一般设置为true显示控制台  
            "MIMode": "gdb",  
            "miDebuggerPath": "D:/mingw64/bin/gdb.exe", // miDebugger的路径,注意这里要与MinGw的路径对应  
            "preLaunchTask": "g++", // 调试会话开始前执行的任务,一般为编译程序,c++为g++, c为gcc  
            "setupCommands": [  
                {   
       				"description": "Enable pretty-printing for gdb",  
                    "text": "-enable-pretty-printing",  
                    "ignoreFailures": true  
                }  
            ]  
        }  
    ]  
  }
  • c_cpp_properties.json配置,includePath项和compilerPath项
{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**",
                "D:/OpenCV-4.5.0/include",
                "D:/OpenCV-4.5.0/include/opencv2"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "compilerPath": "D:/mingw64/bin/gcc.exe",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "clang-x64"
        }
    ],
    "version": 4
}
  • tasks.json配置,args项
{
    "version": "2.0.0",
    "command": "g++",
    "args": [
        "-g",
        "${file}",
        "-o",
        "${workspaceFolder}/DeBug/${fileBasenameNoExtension}.exe",
        "-I", "D:/OpenCV-4.5.0/include",
        "-I", "D:/OpenCV-4.5.0/include/opencv2",
        "-L", "D:/OpenCV-4.5.0/x64/mingw/lib",
        "-l", "libopencv_core411",
        "-l", "libopencv_imgproc411",
        "-l", "libopencv_imgcodecs411",
        "-l", "libopencv_video411",
        "-l", "libopencv_dnn411",
        "-l", "libopencv_ml411",
        "-l", "libopencv_highgui411",
        "-l", "libopencv_objdetect411",
        "-l", "libopencv_flann411",
        "-l", "libopencv_imgcodecs411",
        "-l", "libopencv_photo411",
        "-l", "libopencv_videoio411"
        ],    // 编译命令参数
    "problemMatcher": {
        "owner": "cpp",
        "fileLocation": ["relative", "\\"],
        "pattern": {
            "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
            "file": 1,
            "line": 2,
            "column": 3,
            "severity": 4,
            "message": 5
        }
    }
}
  • 结果验收win10+vscode+opencv(C++)环境搭建便捷版(无需cmake编译)_第7张图片

常见问题

  • 编译OpenCV项目时出现undefined reference to cv::xxxx错误
    在IDE中也可以引入openCV相关的头文件,并没有报异常,但是在编译的时候,就会出现错误
    在这里插入图片描述
    解决方案:在tasks.json的args项中,添加头文件对应的libs文件下的dll文件win10+vscode+opencv(C++)环境搭建便捷版(无需cmake编译)_第8张图片
    如果还有什么问题,欢迎关注公众号与我交流探讨:
    win10+vscode+opencv(C++)环境搭建便捷版(无需cmake编译)_第9张图片

你可能感兴趣的:(win10+vscode+opencv(C++)环境搭建便捷版(无需cmake编译))