win10 VS Code配置 C++ opencv环境

VSCode配置 MingW与CMake:一步步教你设置launch.json与c_cpp_properties.json

具体关于mingw,cmake之类的下载配置见:
写的很详细了,注意看完(一)后还有(二)

我这里就写下配置json文件的结果(搞了几个小时才搞定)。

总之就一句话: 一定要使用default生成的json文件来改!!!

不要从各种博客上借鉴改来改去,每个人的vscode版本都不一样,default生成的json文件都不一样,其实需要改的地方很少,只需要在task.json和c_cpp_properties.json上加上路径即可;

首先是launch.json

这个不需要修改,在一个文件夹内open with vscode后会自动生成.vscode文件,然后新建一个.cpp文件,debug就会自动生成launch.json,完全不需要修改,用default的配置即可;

(如果需要显示powershell窗口输出的话,可以设置 “externalConsole”: true,
这里默认是false)

接下来是task.json

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++.exe build active file",
            "command": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe",
                // 从这里开始修改,改成自己的路径
                "-I",
                "D:/Software/open-cv/opencv/build/include",
                "-I",
                "D:/Software/open-cv/opencv/build/include/opencv",
                "-I",
                "D:/Software/open-cv/opencv/build/include/opencv2",
                "-L",
                "D:/Software/open-cv/opencv/build/x64/MinGW/lib",
				// 这里注意要换成自己安装的opencv的版本号,很多种方式查看,百度即可
                "-l", "libopencv_core3413",
                "-l", "libopencv_imgproc3413",
                "-l", "libopencv_video3413",
                "-l", "libopencv_ml3413",
                "-l", "libopencv_highgui3413",
                "-l", "libopencv_objdetect3413",
                "-l", "libopencv_flann3413",
                "-l", "libopencv_imgcodecs3413",
                "-l", "libopencv_photo3413",
                "-l", "libopencv_videoio3413"

            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
}

c_cpp_properties.json

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**",
                // 只需要在这里添加自己的路径即可
                "D:/Software/open-cv/opencv/build/include",
                "D:/Software/open-cv/opencv/build/include/opencv",
                "D:/Software/open-cv/opencv/build/include/opencv2"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "compilerPath": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\gcc.exe",
            "cStandard": "gnu17",
            "cppStandard": "gnu++14",
            "intelliSenseMode": "windows-gcc-x64"
        }
    ],
    "version": 4
}
VS Code配置C++项目以使用OpenCV库,主要涉及三个核心的JSON配置文件:`c_cpp_properties.json`、`tasks.json`和`launch.json`。以下是详细的配置步骤: ### 1. 配置 `c_cpp_properties.json` 该文件用于设置编译器路径、包含路径以及C/C++标准等信息。以下是一个基于MinGW的示例配置: ```json { "configurations": [ { "name": "win", "includePath": [ "${workspaceFolder}/**", "C:/opencv/opencv/build/x64/cv/install/include", "C:/opencv/opencv/build/x64/cv/install/include/opencv2" ], "defines": [], "compilerPath": "C:\\MinGw\\mingw64\\bin\\g++.exe", "cStandard": "c11", "cppStandard": "c++17", "intelliSenseMode": "${default}" } ], "version": 4 } ``` 确保将`includePath`中的路径替换为你本地OpenCV安装目录的实际路径[^2]。 --- ### 2. 配置 `tasks.json` 该文件用于定义编译任务。以下是一个使用`g++`编译OpenCV项目的示例: ```json { "version": "2.0.0", "tasks": [ { "type": "shell", "label": "g++: build file", "command": "g++", "args": [ "-std=c++17", "-I", "C:/opencv/opencv/build/x64/cv/install/include", "-L", "C:/opencv/opencv/build/x64/cv/install/lib", "-lopencv_core454", "-lopencv_highgui454", "-lopencv_imgproc454", "${file}", "-o", "${fileDirName}/${fileBasenameNoExtension}" ], "options": { "cwd": "C:\\MinGw\\mingw64\\bin" }, "problemMatcher": ["$gcc"], "group": { "kind": "build", "isDefault": true } } ] } ``` 请注意,上述配置中的`-lopencv_core454`、`-lopencv_highgui454`和`-lopencv_imgproc454`是根据OpenCV 4.5.4版本的库命名方式指定的链接参数。如果你使用的是不同版本的OpenCV,请相应调整这些参数[^3]。 --- ### 3. 配置 `launch.json` 该文件用于定义调试器的行为。以下是一个使用`gdb`进行调试的示例: ```json { "version": "0.2.0", "configurations": [ { "name": "g++ - Build and debug active file", "type": "cppdbg", "request": "launch", "program": "${fileDirName}/${fileBasenameNoExtension}.exe", "args": [], "stopAtEntry": false, "cwd": "${fileDirName}", "environment": [], "externalConsole": true, "MIMode": "gdb", "miDebuggerPath": "C:\\MinGw\\mingw64\\bin\\gdb.exe", "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ], "preLaunchTask": "g++: build file", "miDebuggerArgs": "" } ] } ``` 确保将`miDebuggerPath`指向你系统上实际安装的`gdb`路径[^1]。 --- ### 4. 测试你的配置 创建一个简单的C++文件来测试OpenCV是否正常工作: ```cpp #include <opencv2/opencv.hpp> #include <iostream> int main() { cv::Mat image = cv::imread("test.jpg"); if (image.empty()) { std::cout << "Could not open or find the image" << std::endl; return -1; } cv::namedWindow("Display Image", cv::WINDOW_AUTOSIZE); cv::imshow("Display Image", image); cv::waitKey(0); return 0; } ``` 保存并运行此代码,确保能够正确加载图像并在窗口中显示[^2]。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值