vscode configures opencv of c/c++

OpenCV is an open source cross-platform computer vision library based on the BSD license. It can be widely used freely under various operating systems such as Windos, Linux, and Mac. It is a highly scalable and lightweight library. OpenCV is based on C language and a small number of C++ class functions, and provides rich interfaces such as Matlab and Python.

Table of contents

1. Install vscode and configure the c/c++ environment (please refer to my article)

2. Install cmake and configure environment variables

        1.cmake installation

        2.Camke environment variable configuration

 3. Opencv download and compile

        1.opencv download

        2.opencv compilation

 4. vscode configuration and testing

        1. Configure three json files in the .vscode directory of vscode

                1、launch.json

                2、c_cpp_properties.json

                3、tasks.json

         2. Test


1. Install vscode and configure the c/c++ environment (please refer to my article)

(1 message) Installation and configuration of vscode C/C++_Put on straw sandals to travel blog-CSDN blog

2. Install cmake and configure environment variables

        1.cmake installation

            cmake download official website Index of /files/v3.20

            

 

        2.Camke environment variable configuration

            Open the downloaded folder, find the bin directory, and copy the file address of the bin directory.

            

           There is an article at the beginning of the article on how to open environment variables, and then paste the bin directory file address into the user variables and system measurement path.

              To test cmake, open the terminal and enter:

cmake --version

 3. Opencv download and compile

        1.opencv download

                opencv download official website: Releases - OpenCV

                 After downloading, extract it to the directory you specify. You will use it later.

 

        2.opencv compilation

                Open the bin directory of cmake downloaded before and start cmake-gui

 

                 Where is the source code: The source directory under the opencv directory is used here. Where to build the binaries: This is the directory where the compiled files are placed. Generally, tutorials are placed in opencv/build/x64/mingw, and then click Configure (Note: the mingw here is an empty folder created by ourselves).

                Don't choose the wrong one

                 This means selecting the compilation tool for c and cpp files. For c, we use gcc.exe.cpp and for cpp, we use g++.exe. The path is MinGw/bin/. Continue to finish and it will start downloading what we need.

                During the execution process, a bunch of red information will appear in the message box, and finally Configure done will be displayed, which is normal.

 

                

                After execution, uncheck everything about python. Check BUILD_opencv_world, WITH_OPENGL and BUILD_EXAMPLES, uncheck WITH_IPP, WITH_MSMF and ENABLE_PRECOMPILED_HEADERS (if any), and leave CPU_DISPATCH empty.

                Continue to General.    

    

                At this point, the configuration is complete and needs to be compiled and generated.
                ctrl+R, enter cmd to enter command line mode:
                enter the mingw folder you just created

minGW32-make -j 4

 

                 An error may be reported in the middle, so don't worry as long as [100%] is displayed at the end.

                 An error may be reported in the middle, so don't worry as long as [100%] is displayed at the end. Last input:

mimGW32-make istall

                This will generate an install directory in the mingw directory. Then we continue to add two environment variables (pasted under system and user paths):

 4. vscode configuration and testing

        1. Configure three json files in the .vscode directory of vscode

               Open vscode and press Ctrl+Shift+p, click in the red box to generate the .vscode folder

 

                1、launch.json

{
	// Use IntelliSense to learn about possible attributes.
	// Hover to view descriptions of existing attributes.
	// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
	"version": "0.2.0",
	"configurations": [
		{
			"name": "(gdb) Launch", // 配置名称,将会在启动配置的下拉菜单中显示
        	"type": "cppdbg", // 配置类型,cppdbg对应cpptools提供的调试功能;可以认为此处只能是cppdbg
        	"request": "launch", // 请求配置类型,可以为launch(启动)或attach(附加)
        	"program": "${fileDirname}/${fileBasenameNoExtension}.exe", // 将要进行调试的程序的路径
        	"args": [], // 程序调试时传递给程序的命令行参数,一般设为空即可
        	"stopAtEntry": false, // 设为true时程序将暂停在程序入口处,相当于在main上打断点
        	"cwd": "${workspaceFolder}", // 调试程序时的工作目录,此为工作区文件夹;改成${fileDirname}可变为文件所在目录
        	"environment": [], // 环境变量
        	"externalConsole": true, // 使用单独的cmd窗口,与其它IDE一致;为false时使用内置终端
        	"internalConsoleOptions": "neverOpen", // 如果不设为neverOpen,调试时会跳到“调试控制台”选项卡,你应该不需要对gdb手动输命令吧?
        	"MIMode": "gdb", // 指定连接的调试器,可以为gdb或lldb。
        	"miDebuggerPath": "D:\\VScode\\gcc\\x86_64-8.1.0-release-posix-sjlj-rt_v6-rev0\\mingw64\\bin\\gdb.exe", // 调试器路径,Windows下后缀不能省略,Linux下则不要
        	"setupCommands": [
            	{ // 模板自带,好像可以更好地显示STL容器的内容,具体作用自行Google
                	"description": "Enable pretty-printing for gdb",
                	"text": "-enable-pretty-printing",
                	"ignoreFailures": false
            	}
        					],
        	"preLaunchTask": "Compile" // 调试会话开始前执行的任务,一般为编译程序。与tasks.json的label相对应
		}
	]
}

                2、c_cpp_properties.json

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**",
                "D:\\VScode\\OpenCV\\opencv\\build\\x64\\mingw\\install\\include\\",//改成你自己的
                "D:\\VScode\\OpenCV\\opencv\\build\\x64\\mingw\\install\\include\\opencv2\\"//改成你自己的
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "windowsSdkVersion": "10.0.22000.0",
            "compilerPath": "cl.exe",
            "cStandard": "c17",
            "cppStandard": "c++17",
            "intelliSenseMode": "windows-msvc-x64"
        }
    ],
    "version": 4
}

                3、tasks.json

// https://code.visualstudio.com/docs/editor/tasks
{
    "version": "2.0.0",
    "tasks": [
        {
        "label": "Compile", // 任务名称,与launch.json的preLaunchTask相对应
        "command": "D:\\VScode\\gcc\\x86_64-8.1.0-release-posix-sjlj-rt_v6-rev0\\mingw64\\bin\\g++.exe",   // 要使用的编译器,C++用g++,改成你自己的
        "args": [
            "${file}",//当前文件名
            "-o",    // 指定输出文件名,不加该参数则默认输出a.exe,Linux下默认a.out
            "${fileDirname}/${fileBasenameNoExtension}.exe",
            "-g",    // 生成和调试有关的信息
            "-m64", // 不知为何有时会生成16位应用而无法运行,加上此条可强制生成64位的
            "-Wall", // 开启额外警告
            "-static-libgcc",     // 静态链接libgcc,一般都会加上
            "-finput-charset=UTF-8",
            "-fexec-charset=GBK", // 生成的程序使用GBK编码,不加这条会导致Win下输出中文乱码;繁体系统改成BIG5
            "-std=c++17", // 要用的语言标准,根据自己的需要修改。c++可用c++14
            // 扩展参数
            // -I 头文件目录
            // -L 库文件目录
            // -l 库文件
            "-I", "D:\\VScode\\OpenCV\\opencv\\build\\x64\\mingw\\install\\include",//改成你自己的
            "-I", "D:\\VScode\\OpenCV\\opencv\\build\\x64\\mingw\\install\\include\\opencv2",//改成你自己的
            "-L", "D:/VScode/OpenCV/opencv/build/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"
            ], // 编译的命令,其实相当于VSC帮你在终端中输了这些东西
        "type": "process", // process是把预定义变量和转义解析后直接全部传给command;shell相当于先打开shell再输入命令,所以args还会经过shell再解析一遍
        "group": {
            "kind": "build",
            "isDefault": true // 不为true时ctrl shift B就要手动选择了
        },
        "presentation": {
            "echo": true,
            "reveal": "always", // 执行任务时是否跳转到终端面板,可以为always,silent,never。具体参见VSC的文档
            "focus": false,     // 设为true后可以使执行task时焦点聚集在终端,但对编译C/C++来说,设为true没有意义
            "panel": "shared"   // 不同的文件的编译信息共享一个终端面板
        },
        "problemMatcher":"$gcc" // 捕捉编译时终端里的报错信息到问题面板中,修改代码后需要重新编译才会再次触发
        // 本来有Lint,再开problemMatcher就有双重报错,但MinGW的Lint效果实在太差了;用Clang可以注释掉
    }]
}

         2. Test

                Create a test.cpp in the project and press F5

 


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

using namespace cv;
using namespace std;

int main()
{
    VideoCapture cap(0);
    Mat img;

    while (1)
    {
        cap >> img;
        if (img.empty())
            break;
        namedWindow("img", WINDOW_NORMAL);
        imshow("img", img);
        if (27 == waitKey(20))
            break;
    }

    return 0;
}

Guess you like

Origin blog.csdn.net/qq_63032911/article/details/130500125