VScode配置opencv

2021-10-25  本文已影响0人  一个摸鱼AI喵

前言

由于自己编译opencv, 无法解决vs_version.rc.obj生成的问题,故直接下载huihut编译好的Opencv-4.5.2

image-20210725201114309.png

VScode设置

设置文件可以在一个大文件夹下进行配置,这样vscode打开这个文件夹,那所有都适配.

也可以在单个项目文件夹下,这样只针对此单个项目有效.

lauch.json设置

运行-添加配置-C++(GDB/LLDB)


image-20210725201606779.png

这样生成一个.vscode文件夹,有launch.json

并把内容改为:

{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch", // 配置名称,将会在启动配置的下拉菜单中显示
            "type": "cppdbg", // 配置类型,对于C/C++可认为此处只能是cppdbg,由cpptools提供;不同编程语言不同
            "request": "launch", // 可以为launch(启动)或attach(附加)
            "program": "${fileDirname}/${fileBasenameNoExtension}.exe", // 将要进行调试的程序的路径
            // "program": "${workspaceFolder}/${fileBaenameNoExtension}.exe",
            "args": [], // 程序调试时传递给程序的命令行参数,一般设为空
            "stopAtEntry": false, // 设为true时程序将暂停在程序入口处,相当于在main上打断点
            "cwd": "${workspaceFolder}", //调试程序时的工作目录,此为工作区文件夹;改成${fileDirname}可变为文件所在目录
            "environment": [], // 环境变量
            "externalConsole": true, // 使用单独的cmd窗口,与其它IDE一致;为false时使用内置终端
            "internalConsoleOptions": "neverOpen", // 如果不设为neverOpen,调试时会跳到“调试控制台”选项卡,你应该不需要对gdb手动输命令吧?
            "MIMode": "gdb", // 指定连接的调试器,可以为gdb或lldb。但我没试过lldb
            "miDebuggerPath": "D:/mingw64/bin/gdb.exe", // 调试器路径,Windows下后缀不能省略,Linux下则不要
            "setupCommands": [
                { // 模板自带,好像可以更好地显示STL容器的内容,具体作用自行Google
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "Build" // 调试前执行的任务,一般为编译程序。与tasks.json的label相对应
            // "preLaunchTask": "Compile" // 调试前执行的任务,一般为编译程序。与tasks.json的label相对应
        }
    ]
}

tasks.json

添加task.json文件,内容如下:

{
    "version": "2.0.0",
    "command": "g++",
    "label": "Build",
    "options": {
        "cwd": "${workspaceFolder} ",
    },
    "args": [
        "-g",
        "-std=c++11",
        "${file}",
        "-o",
        "${fileBasenameNoExtension}.o",
        "-I",
        "D:/opencv/opencv-4.5.2/include",
        "-I",
        "D:/opencv/opencv-4.5.2/include/opencv2",
        "-L",
        "D:/opencv/opencv-4.5.2/x64/mingw/bin",
        "-L",
        "D:/opencv/opencv-4.5.2/x64/mingw/lib",
        "-l",
        "libopencv_calib3d452",
        "-l",
        "libopencv_core452",
        "-l",
        "libopencv_dnn452",
        "-l",
        "libopencv_features2d452",
        "-l",
        "libopencv_flann452",
        "-l",
        "libopencv_gapi452",
        "-l",
        "libopencv_highgui452",
        "-l",
        "libopencv_imgcodecs452",
        "-l",
        "libopencv_imgproc452",
        "-l",
        "libopencv_ml452",
        "-l",
        "libopencv_objdetect452",
        "-l",
        "libopencv_photo452",
        "-l",
        "libopencv_stitching452",
        "-l",
        "libopencv_video452",
        "-l",
        "libopencv_videoio452"
    ], // 编译命令参数
    "tasks": [
        {
            "type": "shell",
            "label": "cmake", // 对应 cmake ..
            "command": "",
            "args": [
                // ".."        // 如果放在build文件下,需要这句
            ]
        },
        {
            "label": "make", // 对应 make
            "group": {
                "kind": "build",
                "isDefault": true
            },
            /* Linux下面改成"cmake" */
            "command": "mingw32-make.exe",
            "args": []
        },
        {
            "label": "Build", // Build里面依赖于"cmake" 和 "make"
            "dependsOrder": "sequence",
            "dependsOn": [
                "cmake",
                "make"
            ]
        }
    ],
    "problemMatcher": {
        "owner": "cpp",
        "fileLocation": [
            "relative",
            "${workspaceFolder}"
        ],
        "pattern": [
            {
                "regexp": "^([^\\\\s].*)\\\\((\\\\d+,\\\\d+)\\\\):\\\\s*(.*)$",
                "file": 1,
                "location": 2,
                "message": 3
            }
        ]
    },
    "group": {
        "kind": "build",
        "isDefault": true
    }
}

c_pp_properties.json

文件内容如下:

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**",
                "D:/mingw64/include/*",
                "D:/opencv/opencv-4.5.2/include",
                "D:/opencv/opencv-4.5.2/include/opencv2"

            ],
            "defines": [],
            "compilerPath": "D:/mingw64/bin/gcc.exe",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "clang-x64"
        },
        {
            "name": "Win64",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "windowsSdkVersion": "10.0.19041.0",
            "compilerPath": "D:/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.29.30037/bin/Hostx64/x64/cl.exe",
            "intelliSenseMode": "windows-msvc-x64"
        }

    ],
    "version": 4
}

settings.json

如果采用Run code插件,则必须增加settings.json设置

{
    "files.defaultLanguage": "cpp", // ctrl+N新建文件后默认的语言
    "editor.formatOnType": true, // 输入分号(C/C++的语句结束标识)后自动格式化当前这一行的代码
    "editor.suggest.snippetsPreventQuickSuggestions": false, // clangd的snippets有很多的跳转点,不用这个就必须手动触发Intellisense了
    "editor.acceptSuggestionOnEnter": "off", // 我个人的习惯,按回车时一定是真正的换行,只有tab才会接受Intellisense
    // "editor.snippetSuggestions": "top", // (可选)snippets显示在补全列表顶端,默认是inline
    "code-runner.runInTerminal": true, // 设置成false会在“输出”中输出,无法输入
    "code-runner.executorMap": {
        "c": "gcc '$fileName' -o '$fileNameWithoutExt.exe' -Wall -O2 -m64 -lm -static-libgcc -fexec-charset=GBK -D__USE_MINGW_ANSI_STDIO && &'./$fileNameWithoutExt.exe'",
        // "cpp": "g++ '$fileName' -o '$fileNameWithoutExt.exe' -Wall -O2 -m64 -static-libgcc -fexec-charset=GBK && &'./$fileNameWithoutExt.exe'"
        "cpp": "g++ $fileName -I D:/opencv/opencv-4.5.2/include -I D:/opencv/opencv-4.5.2/include/opencv2 -L D:/opencv/opencv-4.5.2/x64/mingw/bin -L D:/opencv/opencv-4.5.2/x64/mingw/lib -l libopencv_calib3d452 -l opencv_core452 -l libopencv_dnn452 -l libopencv_features2d452 -l libopencv_flann452 -l opencv_highgui452 -l opencv_imgcodecs452 -l opencv_imgproc452 -l opencv_ml452 -l opencv_objdetect452 -l opencv_photo452 -l libopencv_stitching452 -l opencv_videoio452 -l opencv_video452  -o $fileNameWithoutExt -fexec-charset=GBK && $dir$fileNameWithoutExt",
        // "c": "gcc $fileName -o $fileNameWithoutExt.exe -Wall -O2 -m64 -lm -static-libgcc -fexec-charset=GBK -D__USE_MINGW_ANSI_STDIO && $dir$fileNameWithoutExt.exe",
        // "cpp": "g++ $fileName -o $fileNameWithoutExt.exe -Wall -O2 -m64 -static-libgcc -fexec-charset=GBK && $dir$fileNameWithoutExt.exe"
        "python": "python -u",
        "perl": "perl",
        "perl6": "perl6",
    }, // 右键run code时运行的命令;未注释的仅适用于PowerShell(Win10默认)和pwsh,文件名中有空格也可以编译运行;注释掉的适用于cmd(win7默认)、PS和bash,但文件名中有空格时无法运行
    "code-runner.saveFileBeforeRun": true, // run code前保存
    "code-runner.preserveFocus": true, // 若为false,run code后光标会聚焦到终端上。如果需要频繁输入数据可设为false
    "code-runner.clearPreviousOutput": false, // 每次run code前清空属于code runner的终端消息,默认false
    "code-runner.ignoreSelection": true, // 默认为false,效果是鼠标选中一块代码后可以单独执行,但C是编译型语言,不适合这样用
    "code-runner.fileDirectoryAsCwd": true, // 将code runner终端的工作目录切换到文件目录再运行,对依赖cwd的程序产生影响;如果为false,executorMap要加cd $dir
    "C_Cpp.clang_format_sortIncludes": true, // 格式化时调整include的顺序(按字母排序)
}

说明:-fexec-charset=GBK 为输出GBK格式,不然中文会乱码

测试

#include <iostream>
#include <opencv2/highgui.hpp>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;

int main(int argc, char **argv)
{
    cout << "你好678" << endl;
    Mat img;
    img=cv::imread("C:\\Users\\jerry\\Pictures\\ch1.jpg");
    cout << " 图像宽为" << img.cols << endl;
    imshow("img", img);
    waitKey(0);
    system("pause");

    return 0;
}
image-20210725202907447.png
上一篇下一篇

猜你喜欢

热点阅读