C/C++教程

Windows MinGW cmake 安装编译Opencv 3.4.3 C++开发环境

本文主要是介绍Windows MinGW cmake 安装编译Opencv 3.4.3 C++开发环境,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

win10 _64位系统

VSCode:官网地址

Opencv:3.4.5

Cmake:3.9.0

MinGw:MinGW-W64 GCC-8.1.0(x86_64-posix-seh)

MinGW配置:

MinGW可以在线安装,也可以直接下载文件后离线解压。

本人选择的是后者,文件下载连接如上。

下载完成后直接解压到你的安装磁盘下。

然后将解压后bin文件夹的path添加到系统环境变量。例如:D:\mingw-w64\bin。

Cmake配置:

Cmake同样是下载文件后解压到安装磁盘。

然后,将其bin文件夹的path添加到系统环境变量。例如:D:\cmake\bin。

OpenCV配置:

将下载的Opencv文件解压到安装磁盘后,通过Cmake选取source路径和build路径,如下图。这里ENABLE_CXX11勾选是指支持C11。

Configure后Genrate一下。因为要是使用MinGW中的C/C++编译,所以Configure的选择如下:

C选择 D:\mingw-w64\bin\gcc.exe C ++选择 D:\mingw-w64\bin\g++.exe

管理员运行cmd后cd到D:\opencv\build\x64\MinGW文件夹下。

输入minGW32-make编译,完成编译后minGW32-make install安装。

最后,将D:\opencv\build\x64\MinGW\bin添加到系统环境变量中。

VSCode配置:

下载后,安装C/C++插件,下图中第一个。

然后,配置tasks.json、launch.json、c_cpp_properties.json这三个文件。

tasks.json:

{
  "version": "2.0.0",
  "tasks": [
    {
      "type": "shell",
      "label": "g++.exe build active file",
      "command": "D:\\mingw-w64\\bin\\g++.exe",
      "args": ["-g", 
        "${file}",
        "-o", "${fileDirname}\\${fileBasenameNoExtension}.exe",
        "-I", "D:/opencv/build/x64/MinGW/install/include",
        "-I", "D:/opencv/build/x64/MinGW/install/include/opencv",
        "-I", "D:/opencv/build/x64/MinGW/install/include/opencv2",
        "-L", "D:/opencv/build/x64/MinGW/install/x64/mingw/lib",
        "-lopencv_core345",
        "-lopencv_imgproc345",
        "-lopencv_imgcodecs345",
        "-lopencv_video345",
        "-lopencv_ml345",
        "-lopencv_highgui345",
        "-lopencv_objdetect345",
        "-lopencv_flann345",
        "-lopencv_imgcodecs345",
        "-lopencv_photo345",
        "-lopencv_videoio345"],
      "options": {
        "cwd": "D:\\mingw-w64\\bin"
      },
      "problemMatcher": ["$gcc"],
      "group": {
        "kind": "build",
        "isDefault": true
      }
    }
  ]
}

launch.json:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "g++.exe build and debug active file",
      "type": "cppdbg",
      "request": "launch",
      "program": "${fileDirname}\\${fileBasenameNoExtension}",
      "args": [],
      "stopAtEntry": false,
      "cwd": "${workspaceFolder}",
      "environment": [],
      "externalConsole": true,
      "MIMode": "gdb",
      "miDebuggerPath": "D:\\mingw-w64\\bin\\gdb.exe",
      "setupCommands": [
        {
          "description": "Enable pretty-printing for gdb",
          "text": "-enable-pretty-printing",
          "ignoreFailures": false
        }
      ],
      "preLaunchTask": "g++.exe build active file"
    }
  ]
}

c_cpp_properties.json:

{
    "configurations": [
      {
        "name": "win32",
        "includePath": ["${workspaceFolder}/**",
            "D:/mingw-w64/bin/c++",
            "D:/opencv/build/x64/MinGW/install/include",
            "D:/opencv/build/x64/MinGW/install/opencv",
            "D:/opencv/build/x64/MinGW/install/opencv2",
            "D:/opencv/build/x64/MinGW/install/x64/mingw/lib"],
        "defines": ["_DEBUG", "UNICODE", "_UNICODE"],
        "compilerPath": "D:\\mingw-w64\\bin\\gcc.exe",
        "cStandard": "c11",
        "cppStandard": "c++17",
        "intelliSenseMode": "clang-x64"
      }
    ],
    "version": 4
  }

将以上三个文件放在一个新建的.vscode文件夹下,并且将这个文件夹与你的代码放在同一文件下,就可以F5键来调试你的程序了。

这篇关于Windows MinGW cmake 安装编译Opencv 3.4.3 C++开发环境的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!