Ubuntu 18.04 vscode configure c++ version opencv

written in front: This article mainly introduces how to link opencv to VS Code on the ubuntu system.
Make sure that opencv has been installed on your computer, and then enter the following command pkg-config --libs --cflags opencv4 (PS: Sometimes it may not be opencv4, but opencv2 or opencv). If the result shown in the picture below appears, you can continue to read this article, otherwise please use Baidu for other methods.
Run command result graph
Then modify the tasks.json file as follows to complete the link.

{
    
    
    "tasks": [
        {
    
    
            "type": "cppbuild",
            "label": "C/C++: g++ 生成活动文件",
            "command": "/usr/bin/g++",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}",
                "`pkg-config","--libs","--cflags","opencv`"
            ],
            "options": {
    
    
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
    
    
                "kind": "build",
                "isDefault": true
            },
            "detail": "调试器生成的任务。"
        }
    ],
    "version": "2.0.0"
}

is mainly to add instructions to find the opencv code library under the args parameter list.
The above is all the operations of opencv linking to VS Code. Let’s look at the test code below.

#include <opencv2/opencv.hpp>

int main() {
    
    
    cv::Mat img = cv::imread("./2.png");
    cv::namedWindow("1", cv::WINDOW_AUTOSIZE);
    cv::imshow("1", img);
    cv::waitKey(0);
    return 0;
}

The final picture displayed is as follows:
Result graph

Guess you like

Origin blog.csdn.net/orangeboss/article/details/128952604