VS Code:OpenCV环境配置及运行(g++.exe)


1. 准备

  1. VS Code已安装:VS Code:搭建C/C++编译调试运行环境
  2. OepnCV已编译:学习OpenCV3:Cmake+MinGW编译OpenCV

OepnCV目录

2. 环境配置

  在电脑的环境变量path中添加OpenCV目录。

OpenCV目录

D:\opencv3410\build_mingw\x64\mingw\bin

编译环境变量

3. VS Code配置

  在VS Code中配置如下4个文件。其中c_cpp_properties.json用来添加OpenCV包含目录,settings.json用来配置Run Code的执行命令,launch.json和tasks.json来配置程序调试命令。

4个配置文件

  1. c_cpp_properties.json:在"includePath"中添加OpenCV的include目录。
{
    "configurations": [{
        "name": "Win32",
        "includePath": [
            "${workspaceFolder}/**",
            "D:/opencv3410/build_mingw/include"
        ],
        "defines": [
            "_DEBUG",
            "UNICODE",
            "_UNICODE"
        ],
        "windowsSdkVersion": "10.0.18362.0",
        "compilerPath": "C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.24.28314/bin/Hostx64/x64/cl.exe",
        "cStandard": "c11",
        "cppStandard": "c++17",
        "intelliSenseMode": "msvc-x64"
    }],
    "version": 4
}
  1. settings.json:在"code-runner.executorMap"添加"cpp"的编译命令。注意:库文件是从D:/opencv3410/build_mingw/x64/mingw/lib得到,且不能带有后缀名;不同编译方式,其生成的库文件可能不同。

库目录

// -I 后跟包含目录 D:/opencv3410/build_mingw/include
// -L 后跟库目录 D:/opencv3410/build_mingw/x64/mingw/lib
// -l 后跟库文件 libopencv_world3410 libopencv_img_hash3410
{
    "code-runner.executorMap": {
        "cpp": "g++ $fileName -I D:/opencv3410/build_mingw/include -L D:/opencv3410/build_mingw/x64/mingw/lib -l libopencv_world3410 -l libopencv_img_hash3410 -o $fileNameWithoutExt.exe && ./$fileNameWithoutExt.exe",
    }
}
  1. launch.json和tasks.json: launch.json的"preLaunchTask"需与tasks.json的"label"保持一致
// launch.json
{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "g++.exe",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "C:\\mingw64\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "g++.exe build active file"
        }
    ]
}
// tasks.json
{
    "tasks": [
        {
            "type": "shell",
            "label": "g++.exe build active file",
            "command": "C:\\mingw64\\bin\\g++.exe",
            "args": [
                "-g",
                "${file}",
                "-I",
                "D:/opencv3410/build_mingw/include",
                "-L",
                "D:/opencv3410/build_mingw/x64/mingw/lib",
                "-l",
                "libopencv_world3410",
                "-l",
                "libopencv_img_hash3410",
                "-o",
                "${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "C:\\mingw64\\bin"
            }
        }
    ],
    "version": "2.0.0"
}

4. 运行程序

测试程序:

#include <opencv2/opencv.hpp>
#include <opencv2/highgui.hpp>
using namespace cv;
int main()
{
    Mat img = imread("2.png");
    cv::imshow("image", img);
    cv::waitKey();
    return 0;
}
  1. Run Code运行(快捷键:Ctrl+Alt+N)

Run Code

运行结果

  1. 调试运行(快捷键:F5)

调试运行

终端

调试控制台

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值