vscode installs and configures the opencv+eigen library and uses it

The blogger used to use Visual Studio 2017 for c++ development before. At present, the team development uses vscode for development and maintenance, so they went to step on the pit to build a local vscode development environment. The following is a record of the entire process of building the environment.

1. Software and library selection

2. Software installation

2.1 vscode installation

Just click the downloaded vscode installation package to install:
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here

2.2 cmake installation

2.2.1 Download the zip package corresponding to cmake

insert image description here

2.2.2 Decompress to the corresponding path

It is recommended to put Program Files on the c drive
insert image description here

2.2.3 Configure environment variables

Open My Computer->Properties->Advanced System Settings->Environment Variables, add the variables shown in the red box below
insert image description here

2.2.4 Signs of successful configuration

Enter cmake --version in cmd, and the following situation indicates that the configuration is successful
insert image description here

2.3 MinGW installation

2.3.1 Download x86_64-posix-silj and decompress it

First select the download address to download the repo shown in the figure below
insert image description here
and then unzip the compressed package to your own disk:
insert image description here

2.3.2 Configure environment variables

Open My Computer->Properties->Advanced System Settings->Environment Variables, add the variables shown in the red box below
insert image description here

2.3.3 Signs of successful configuration

Enter gcc -v in cmd, the following situation appears, indicating that the configuration is successful
insert image description here

2.4 opencv installation and compilation

To use opencv in vscode, you need to use cmake to compile the opencv source code first

2.4.1 Download opencv and decompress it

insert image description here

2.4.2 cmake compilation

2.4.2.1 Open cmake-gui.exe

Find the location where cmake is stored, corresponding to the gui location in it
insert image description here

2.4.2.2 Configure cmake basic parameters

Configure the location of the opencv source code that needs to be compiled at where is the source code, and configure the location of the compiled source code at where to build the binaries. Here I built an opencv-4.5.3_x64_build folder for storage.
insert image description here

2.4.2.3 Click configure, select MinGW Makefiles, Specify native compilers

insert image description here

2.4.2.4 Click Next, choose the compiler, choose gcc.exe in the bin directory of MinGW-w64 for C, and choose g++.exe for C++

insert image description here

2.4.2.5 After clicking Finish, the program automatically configures

insert image description here
------------------------------- Some troubleshoot methods ---------------- -------------
This step of configuration may cause some libraries to fail to download due to network speed problems. You can open CMakeDownloadLog.txt in your generated directory to view the specific failed libraries, as follows:
Table of contents
CMakeDownloadLog.txt download The failure example is as follows

#use_cache "D:/chorm-download/opencv-4.5.3/.cache"
#do_copy "opencv_videoio_ffmpeg.dll" "bab661341c30862fa88627130219c0a5" "https://raw.githubusercontent.com/opencv/opencv_3rdparty/213fcd5d4897319a83207406036c4a5957fba010/ffmpeg/opencv_videoio_ffmpeg.dll" "D:/chorm-download/opencv-4.5.3_x64_build/3rdparty/ffmpeg"
#missing "D:/chorm-download/opencv-4.5.3_x64_build/3rdparty/ffmpeg/opencv_videoio_ffmpeg.dll"
# timeout on name lookup is not supported
# Trying 151.101.108.133...
# TCP_NODELAY set
# connect to 151.101.108.133 port 443 failed: Connection refused
# Failed to connect to raw.githubusercontent.com port 443: Connection refused
# Closing connection 0

Solution:
Copy the corresponding dll download path in the above txt, download it with Thunder, and copy it to the .cache/ffmpeg folder of the opencv source code (my path is: D:\chorm-download\opencv-4.5.3 .cache\ffmpeg), and use the command in cmd to obtain the MD5 value of the win-win file, and use the MD5+ file name to rename the original file name eg: original file name: opencv_videoio_ffmpeg_64.dll updated file name:
ac99f9767a83103c31709628af685924
-
opencv_videoio_ffmpeg_64.dll
among
insert image description here
them The download of ffmpeg_version.cmake can consider saving the webpage first and then changing the name suffix

2.4.2.6 Click generate

insert image description here

2.4.2.6 After the completion, enter the folder where the compilation result created by yourself is stored, hold down shift and press the right mouse button, and select to enter the powershell window here. Enter MinGW32-make -j4 to start compiling, it will take about 20 minutes.

insert image description here

2.4.2.7 After compiling, enter MinGW32-make install to install

insert image description here

2.4.2.8 Environment configuration

Open My Computer->Properties->Advanced System Settings->Environment Variables, add the variables shown in the red box below
insert image description here

2.4 Eigen download and decompression

  • Download the tar package of the following Eigen3.4.0 version from the location shown in the first section
    insert image description here
  • Unzip to a file for backup

3. Vscode configuration and use

3.1 Open the vscode software and create a .vscode folder under the code directory you set

insert image description here

3.2 Create three json files under the .vscode folder, respectively: c_cpp_properties.json, launch.json, tasks.json.

The specific configuration is as follows:
c_cpp_properties.json is configured as follows:

{
    
    
    "configurations": [
        {
    
    
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**",
                "D:/personal/vs_project/eigen-3.4.0/**",
                "D:/chorm-download/opencv-4.5.3_x64_build/install/include",
                "D:/chorm-download/opencv-4.5.3_x64_build/install/include/opencv2",
                "D:/chorm-download/opencv-4.5.3_x64_build/install/x64/mingw/lib",
                "D:/personal/vs_project/eigen-3.4.0"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "windowsSdkVersion": "10.0.17763.0",
            "compilerPath": "D:\\chorm-download\\mingw64\\bin\\gcc.exe",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "clang-x64"
        }
    ],
    "version": 4
}

launch.json is configured as follows:

// https://code.visualstudio.com/docs/cpp/launch-json-reference
{
    
    
    "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:\\chorm-download\\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相对应
    }]
}

tasks.json is configured as follows

// {
    
    
//     "version": "2.0.0",
//     "tasks": [
//         {
    
    
//             "type": "shell",
//             "label": "g++.exe build active file",
//             "command": "D:\\chorm-download\\mingw64\\bin\\g++.exe",
//             "args": ["-g", 
//             "${
    
    file}",
//             "-o", "${fileDirname}\\${fileBasenameNoExtension}.exe",
//             "-I", "D:/chorm-download/opencv-4.5.3_x64_build/install/include",
//             "-I", "D:/chorm-download/opencv-4.5.3_x64_build/install/include/opencv2",
//             "-L",  "D:/chorm-download/opencv-4.5.3_x64_build/install/x64/mingw/lib",
//             "-l","opencv_core453",
//             "-l","opencv_imgproc453",
//             "-l","opencv_imgcodecs453",
//             "-l","opencv_video453",
//             "-l","opencv_ml453",
//             "-l","opencv_highgui453",
//             "-l","opencv_objdetect453",
//             "-l","opencv_flann453",
//             "-l","opencv_imgcodecs453",
//             "-l","opencv_photo453",
//             "-l","opencv_videoio453"],
//             "options": {
    
    
//                 "cwd": "D:\\chorm-download\\mingw64\\bin"   
//             },
//             "problemMatcher": [
//                 "$gcc"
//             ],
//             "group": {
    
     
//                 "kind": "build",
//                 "isDefault": true
//             }
//         }
//     ]
// }

// https://code.visualstudio.com/docs/editor/tasks
{
    
    
    "version": "2.0.0",
    "tasks": [
        {
    
    
        "label": "Compile", // 任务名称,与launch.json的preLaunchTask相对应
        "command": "D:\\chorm-download\\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:/chorm-download/opencv-4.5.3_x64_build/install/include",
            "-I", "D:/chorm-download/opencv-4.5.3_x64_build/install/include/opencv2",
            "-I","D:/personal/vs_project/eigen-3.4.0",
            "-L",  "D:/chorm-download/opencv-4.5.3_x64_build/install/x64/mingw/lib",
            "-l", "libopencv_calib3d453",
            "-l", "libopencv_core453",
            "-l", "libopencv_dnn453",
            "-l", "libopencv_features2d453",
            "-l", "libopencv_flann453",
            "-l", "libopencv_gapi453",
            "-l", "libopencv_highgui453",
            "-l", "libopencv_imgcodecs453",
            "-l", "libopencv_imgproc453",
            "-l", "libopencv_ml453",
            "-l", "libopencv_objdetect453",
            "-l", "libopencv_photo453",
            "-l", "libopencv_stitching453",
            "-l", "libopencv_video453",
            "-l", "libopencv_videoio453"
            ], // 编译的命令,其实相当于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可以注释掉
    }]
}

3.3 Specific application examples

A simple application example is used here to use the opencv and eigen libraries:
create a src folder under opencv-demo, create a new opencv_demo.cpp folder, and add the following c++ running code. The
code is as follows:

#include<opencv2/opencv.hpp>
#include<opencv2/highgui.hpp>
#include<Eigen/Dense>


using namespace cv;
using Eigen::MatrixXd;
using Eigen::VectorXd;

int main(){
    
    
    Mat img = cv::imread("D:/personal/model_and_code/img/adc.png");
    cv::namedWindow("demo",cv::WINDOW_NORMAL);
    cv::imshow("demo", img);

    
    MatrixXd m = MatrixXd::Random(3, 3);
    m = (m + MatrixXd::Constant(3, 3, 1.2)) * 50;
    std::cout << "m =" << std::endl << m << std::endl;
    VectorXd v(3);
    v << 1, 2, 3;
    std::cout << "m * v =" << std::endl << m * v << std::endl;
    cv::waitKey(0);
    system("pause");

    return 0;
}

After F5 starts the program, you can see the following sample results:
insert image description here
At this point, you're done, and the opencv and eigen libraries can be used happily in vscode.

Reference link:
When CMake compiles OpenCV4.0, opencv_ffmpeg.dll and other download failures are summarized.
VSCode builds OpenCV's c++ environment (windows10)

Guess you like

Origin blog.csdn.net/caobin_cumt/article/details/129216401