VScode builds Opencv (C++ development environment)

It took half a day and after reading various blogs, the configuration was finally successful. I will record and share it here, so that more people can avoid detours.

This time the environment is Win 10.

1. Prepare the software

The 3 softwares we used this time, (It is recommended that the reader’s file path and file version should be the same as mine as much as possible, so that it is not easy to make mistakes. If you are different, you can try it yourself first, and don’t worry about changing it.)

MinGw: Version: 8.1.0-release-posix-seh-rt_v6-rev0

Cmake: Version: 3.20.2

Opencv: Version: 4.5.2

2. Download the software

1. MinGw download

mingw64
1 min
insert image description here
downloads the file, unzips it after downloading, and remembers the location. For convenience, I renamed it MinGw. (Here is the MinGw software)

To add environment variables, you can directly search for "environment variables" by win+Q
, and then find the path in the environment variables. This is where you add environment variables.

insert image description here
Add environment variables. The location of MinGw here is: F:/MinGw
, so the environment variable we added is: F:/MinGw/bin
and then ctrl+R, enter cmd, and bring up the cmd command window.
Input: gcc -v
insert image description here
displays this, which means that the MinGw installation and configuration are complete.

2. Cmake download

Index of /files/v3.20 (cmake.org)
 insert image description here
After downloading, unzip and generate a folder just like the previous process.

Also for convenience, we change the name of the folder to cmake, and the same path here is:
F:/cmake
, we continue to add environment variables: F:/cmake/bin
and then, then ctrl+R, enter cmd, call The cmd command window is displayed.
Enter: cmake -version
insert image description here
and then our second software is also installed and configured.

3. Opencv download

** https://opencv.org/releases/**
insert image description here

After downloading, install it. (Specify the directory, which will be used later), I still specify the F drive here
, and then the three softwares are downloaded, installed and configured.

3. Compile

Here I want to explain that Opencv can be directly applied in vs, but vs is too heavy and inconvenient. In contrast, using vscode is more lightweight and convenient, and there are many plug-ins that allow us to customize. So we choose vscode to configure opencv.

But vscode can't use opencv directly, we need to compile the source file of opencv before it can be used. This is why we use cmake. Next we will use cmake to compile the source files of opencv.

1、cmake-gui

Find the cmake-gui file in the bin under the cmake folder and start it.
F:/cmake/bin/cmake-gui
insert image description here
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. The
general tutorials are placed in opencv/build/x64/mingw
(Note: mingw here is an empty folder created by ourselves).

insert image description here
Then click Configure
insert image description here
and don’t choose the wrong one here, we need to use mingw makefile. Continue to next.

insert image description here
Here it means to select the compiling tools for c and cpp files, for c we use gcc.exe.cpp and we use g++.exe.
The path is under MinGw/bin/
Continue to finish, it will start to download what we need. Some files or something.

During the execution process, a bunch of red information will appear in the message box, and finally it will display Configure done, which is normal. If it breaks while executing, there are other problems.
(This is based on the experience of a predecessor.)

If there is no "Science Online", some configurations cannot be downloaded, and we need to download them manually.
For example:
insert image description here
the information in red here means that there is a file download failure, and then we open
insert image description here
the file and check the log to see which file failed.
insert image description here
(You can also look at the experience of the predecessors. If the opencv_ffmpeg.dll and other downloads fail when CMake compiles OpenCV, enter this website: https://www.cnblogs.com/huluwa508/p/10142718.html

GitHub things can be downloaded through this website: https://ghproxy.com

insert image description here
Just like this:
insert image description here
Then go back to cmake-gui and click Configure again

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), CPU_DISPATCH is empty.

Continue to General.
insert image description here

2、make

At this point, the configuration is also completed, and it needs to be compiled and generated.
ctrl+R, enter cmd to enter the command line mode:
enter the newly created mingw folder
f:
cd opencv/build/x64/mingw
execute the command: minGW32-make -j 4
insert image description here

(-j 4 is for faster execution, if your computer configuration is OK, you can even -j 8)
and then wait quietly...
There may be errors in the middle due to some unknown reasons, such as (the software version is different from mine .Then try the same software version as mine.) For other errors, suggest Baidu, or send them to me, and I will help you to see (not guaranteed to be resolved).

3、install

If there is no error, we will continue to enter the command in this directory:
minGW32-make install
This will generate an install directory. So far, the sky has paid off, and we have finally compiled it!
Then we continue to add two environment variables:
the first path is: F:/opencv/build/x64/vc15/bin
and the second path is: F:/opencv/build/x64/mingw/bin
insert image description here
(according to your own directory to make changes.)

4. VScode configuration

Mainly configure 3 json

1、launch.json

(self-configured property: miDebuggerPath)

{
    
    

    "version": "0.2.0",
    "configurations": [
        {
    
    
            "name": "opencv debuge",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}\\Debugger\\${fileBasenameNoExtension}.exe",
            //上面这个Debugger是我自己定义的,为了方便放置生成的exe文件
            "args": [],
            "stopAtEntry": false, //这里如果为 false,则说明调试直接运行。(反之则停止)
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,//是否调用外部cmd
            "MIMode": "gdb",
            "miDebuggerPath": "F:\\MinGw\\bin\\gdb.exe",//自己进行设置
            "setupCommands": [
                {
    
    
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": false
                }
            ],
            "preLaunchTask": "opencv3.5.2"
        }
    ]
}

2、c_cpp_properties.json

The first one of includePath should not be changed, and the latter should be set as your own path.

{
    
    
    "configurations": [
        {
    
    
            "name": "win",
            "includePath": [
                "${workspaceFolder}/**",
                "F:/opencv/build/x64/mingw/install/include",
                "F:/opencv/build/x64/mingw/install/include/opencv2"
            ],
            "defines": [],
            "compilerPath": "F:MinGw/bin/g++.exe",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "${default}"
        }
    ],
    "version": 4
}

3、tasks.json

{
    
    
    "version": "2.0.0",
    "tasks": [
        {
    
    
            "type": "shell",
            "label": "opencv3.5.2",
            "command": "F:/MinGw/bin/g++.exe",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${workspaceFolder}\\Debugger\\${fileBasenameNoExtension}.exe",
                //上面这个Debugger是我自己定义的,为了方便放置生成的exe文件
                "F:/opencv/build/x64/mingw/bin/libopencv_world452.dll",
                "-I",
                "F:/opencv/build/x64/mingw/install/include",
                "-I",
                "F:/opencv/build/x64/mingw/install/include/opencv2",
            ],
            "options": {
    
    
                "cwd": "F:/MinGw/bin"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
    
    
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

5. Test

We have to create the Debugger folder in the file just now. The automatically generated exe file will be placed inside.
The execution of exe files requires dependent files. We also need to put the dependent files in this folder, so that the dependencies can be called without error when generating the exe file. Without this dependency, the exe file cannot be generated.

Find the dependency file: There are two ddl files in
F:\opencv\build\x64\MinGw\install\x64\mingw\bin :

These two DDL files are very important, they can be found in the address above, and then copied
to the Debugger directory in the working directory. Otherwise the program may fail to build.
**
libopencv_world452.dll
opencv_videoio_ffmpeg452_64.dll
**

Readers also go to a similar directory to find, and then copy the file to the Debugger directory.
insert image description here

Then create a test file: (call the camera)
If the reader has an opencv foundation, you can test it yourself. I provide a cpp file for testing the camera.

test.cpp


#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;
}

Press F5 to run it.
If you can call the camera, you're done!

If you don't want to compile it yourself, you can use what I have compiled. See the link:
opencv_vscode_simple version

Guess you like

Origin blog.csdn.net/qq_45022687/article/details/120241068