vscode+msys+opencv环境搭建记录

最近由于vscode中采用msvc编译cpp还需从vs的developer command prompt中打开才能使用,而且vcpkg安装的包用minGW编译不过,为了同时使用minGW和opencv,且避免复杂的opencv编译,遂采用msys的方式来跑opencv程序

采用一下流程:

1. msys2安装

傻瓜式安装即可

配置msys环境变量:安装路径/usr/bin

运行命令更新:pacman -Syu + pacman -Su

安装minGW工具链:pacman -S --needed base-devel mingw-w64-x86_64-toolchain

配置minGW环境变量:安装路径/mingw64\bin

可以用gcc --version验证下

安装opencv和qt6相应包(qt6因为msys运行opencv基于qt6的界面,否则报错没有qt6的dll)

pacman -S mingw-w64-x86_64-opencv

pacman -S mingw-w64-x86_64-qt6-base

配置vscode(自行安装c/c++插件)

配置运行路径:

c_cpp_properties.json文件:注意包含头文件("D:/msys/mingw64/include",
                "D:/msys/mingw64/include/opencv4")

{
    "configurations": [
        {
            "name": "Win32-g++",
            "includePath": [
                "${workspaceFolder}/**",
                "D:/msys/mingw64/include",
                "D:/msys/mingw64/include/opencv4"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "intelliSenseMode": "gcc-x64",
            "compilerPath": "D:/msys/mingw64/bin/g++.exe"
        },
        {
            "name": "Win32-gcc",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "intelliSenseMode": "gcc-x64",
            "compilerPath": "D:/msys/mingw64/bin/gcc.exe"
        }
    ],
    "version": 4
}

tasks.json:注意引入相应的库("-I",
        "D:/msys/mingw64/include/opencv4",
        "-L",
        "D:/msys/mingw64/lib",
        "-l",
        "opencv_core",
        "-l",
        "opencv_imgcodecs",
        "-l",
        "opencv_highgui")可根据不同需要引入不同的库

{
  "tasks": [
    {
      "type": "cppbuild",
      "label": "C/C++: g++.exe 生成活动文件",
      // "command": "D:/MinGW/bin/gcc.exe", 这是运行c语言程序的配置
      "command": "D:/msys/mingw64/bin/g++.exe",
      "args": [
        "-fdiagnostics-color=always",
        "-g",
        "${file}",
        "-o",
        "${fileDirname}\\${fileBasenameNoExtension}.exe",
        "-I",
        "D:/msys/mingw64/include/opencv4",
        "-L",
        "D:/msys/mingw64/lib",
        "-l",
        "opencv_core",
        "-l",
        "opencv_imgcodecs",
        "-l",
        "opencv_highgui"
      ],
      "options": {
        "cwd": "D:/MinGW/bin"
      },
      "problemMatcher": ["$gcc"],
      "group": {
        "kind": "build",
        "isDefault": true
      },
      "detail": "调试器生成的任务。"
    }
  ],
  "version": "2.0.0"
}

编写opencv测试程序,右上角运行即可,Run code的话会报错找不到opencv的头文件

#include <iostream>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

int main(int argc, char **argv)
{
  Mat src; // 初始化一个操作对象
  src = imread("图片路径");
  if (!src.data) // 判断图片是否加载进来
  {
    cout << "不能加载图片" << endl;
    return -1;
  }
  namedWindow("加载的图片", WINDOW_AUTOSIZE);
  imshow("加载的图片", src); //""内命名一致,才能显示在一个窗口
  waitKey(0);
  return 0;
}

接下来找到opencv.exe在当前目录,命令行运行./opencv.exe就可以看到加载图片的窗口了 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值