Ubuntu18.04 vscode(c++)配置opencv successful

splendid.rain生發表於2020-11-28

需要準備的東西:vscode vscode-c++編譯環境 opencv,主要是修改三個文件,launch.json , c_cpp_properties.json , task,json
vscode配置c++網上的教程較多,在這裡就不再贅述。
1,新建一個cpp檔案,儲存。
2,vscode左側方的debug按鈕(小蟲子)–Debug -> Open Configurations -> 開啟備選框 -> C++(GDB/LLDB) -> g++ build and debug active file
3,回到資源管理器(左側欄的第一個圖示)在這裡插入圖片描述

4上述操作後開啟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",
            "request": "launch",
            "program": "${workspaceFolder}/${fileBasenameNoExtension}.o",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "preLaunchTask": "build",//也就是新增一個launch之間的任務,任務名為build,這個build就是我們在tasks.json中設定的任務名。
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

5,按鍵ctrl+shift+P,後輸入選擇Configure Task,在備選框中選擇C/C++:cpp build active file選項,則新建一個task.json檔案。

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "type": "shell",
            "command": "g++",
            "args": [
                "-g", "-std=c++11", "${file}", "-o", "${fileBasenameNoExtension}.o",// 設定動態連結庫
                "-I", "/usr/local/include",
                "-I", "/usr/local/include/opencv2",
                "-I", "/usr/local/include/opencv4",
                "-L", "/usr/local/lib",
                "-l", "opencv_core",
                "-l", "opencv_imgproc",
                "-l", "opencv_imgcodecs",
                "-l", "opencv_video",
                "-l", "opencv_ml",
                "-l", "opencv_highgui",
                "-l", "opencv_objdetect",
                "-l", "opencv_flann",
                "-l", "opencv_imgcodecs",
                "-l", "opencv_photo",
                "-l", "opencv_videoio"
            ],// 編譯命令引數        }
     ]
}

6,Ctrl + Shift + P 開啟搜尋框,鍵入c++,會出現備選專案,選擇圖示Edit configurations (JSON)
並且將c_cpp_properties.json修改為:

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**",
                "/usr/local/include", //請確保你的opencv opencv2標頭檔案夾安裝在這個目錄
                "/usr/include"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "clang-x64"
        }
    ],
    "version": 4
}

opencv安裝的目錄要在/usr/local/include裡面。
可以輸入命令 :

cd /usr/local/include
ls

結果如下:
在這裡插入圖片描述

7,回到測試檔案,F5執行如下
測試程式:

#include <iostream>
#include<opencv2/opencv.hpp>
#include<opencv2/highgui/highgui.hpp>
using namespace std;
using namespace cv;
int main() 
{
    cout<<"hello world"<<endl;
    VideoCapture capture(0);
    while (1)
    {
        Mat frame;
        capture>>frame;
        imshow("src",frame);
        waitKey(27);
    }
    
}

相關文章