环境配置:OpenCV(c++)


配置cpp opencv 的 Windows vscode编译环境

🔗 链接:

mingw:https://pan.quark.cn/s/cb6d3cb6d4cf

opencv:https://opencv.org/releases/

cmake:https://pan.quark.cn/s/d40f6922312c

1、下载mingw压缩包,并解压mingw,把X:\xxx\mingw64\bin路径添加到系统环境变量

2、下载opencv源码,并在那个目录旁边新建一个build文件夹

3、下载cmake,打开cmake,第一行选择opencv源码根目录路径,第二行选刚刚创建的build文件夹路径

4、点选Configure,在下拉框选MinGW Makefiles,单选栏选Use default native compilers,开始cmake,注意:

  • 勾选BUILD_EXAMPLES、BUILD_opencv_world,WITH_OPENGL
  • 不勾选WITH_IPP、WITH_MSMF和ENABLE_PRECOMPILED_HEADERS(如果有的话),CPU_DISPATCH选空。

然后,界面会红一下,再点一次Configure即可。

5、在cmake好后,打开cmd,cd进入opencv的build目录,执行MinGW32-make -j8开始编译

参数j是多线程数,可以视情况选j4j8j16

6、编译好之后,minGW32-make install安装,然后添加环境变量,比如我的是D:\code_env\opencv_460_build\bin

7、前往vscode,去应用商店下载微软的c++插件,然后自己找个目录是放代码的,用vscode打开这个文件夹。 在此文件夹下新建一个.vscode文件夹,再继续新建三个.json文件(tasks.jsonlaunch.jsonc_cpp_properties.json)。

//c_cpp_properties
{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**",
                "D:/code_env/mingw64/include/**",
                "D:/code_env/opencv_460_build/install/include",
                "D:/code_env/opencv_460_build/install/include/opencv2"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "compilerPath": "D:/code_env/mingw64/bin/g++.exe",
            "cStandard": "c11",
            "cppStandard": "c++11",
            "intelliSenseMode": "windows-gcc-x64"
        }
    ],
    "version": 4
}
//tasks
{
	"version": "2.0.0",
	"tasks": [
		{
			"type": "cppbuild",
			"label": "opencv460", //任务名称,要和launch.json的"preLaunchTask"字段对应
			"command": "D:/code_env/mingw64/bin/g++.exe", //编译器路径
			"args": [
				"-g", //控制台编译时输入的-g,表示生成调试有关信息,编译生成可执行文件调试
				"-std=c++11",
				// "-static",
        		"-fexec-charset=GBK",// 令mingw按GBK编码⽣成exe⽂件 GBK
        		"-finput-charset=UTF-8",// 令mingw按UTF-8编码处理(此参数可以不设置)//
				"${file}",//要编译的文件,默认只能编译单个文件
				"-o", //指定生成的可执行文件的名字,编译不加 -o 默认生成就是a.exe
				"${fileDirname}\\Release\\${fileBasenameNoExtension}.exe",
                "-I","D:/code_env/opencv_460_build/install/include",
                "-I","D:/code_env/opencv_460_build/install/include/opencv2",
                "-I ${fileDirname}\\..\\include",//添加本地文件的头文件路径,否则提示头文件找不到
				"-L","D:/code_env/opencv_460_build/bin",
				"-l","libopencv_world460",
			],
			"options": {
				"cwd": "${workspaceFolder}" 
			},
			"problemMatcher": [
				"$gcc"
			],
			"group": {
				"kind": "build",
				"isDefault": true // 开启编译快捷键, ctrl + shift + B
			},
			"detail": "编译器: D:/code_env/mingw64/bin/g++.exe"
		}
	]
}
//launch
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "opencv460",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}\\Release\\${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,  //这里如果为 false,则说明调试直接运行。(反之则停止)
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false, //是否调用外部cmd
            "MIMode": "gdb", 
            "miDebuggerPath": "D:/code_env/mingw64/bin/gdb.exe",
            "setupCommands": [
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": false
                }
            ],
            "preLaunchTask": "opencv460"
        }
    ]
}

然后可以写代码了

#include<opencv2/opencv.hpp>
#include<iostream>
int main()
{
    cv::Mat img;
    cv::VideoCapture capture(0);
    int key;
    while(true){
        capture >> img;
        cv::imshow("test", img);
        key=cv::waitKey(30);
        if(key==27){
            break;
        }
    }
    return 0;
}

使用ctrl+shift+b运行。或按F5运行也可以。一般来说,已经配置完毕了。


文章作者: 拓佑豪
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 拓佑豪 !
评论