Ubuntu16.04下vscode配置調用opencv的C++環境

把vscode吹上天,用起來差點吐血。不管是用在Python上還是c++程序上,體驗一個比一個差。說是說vscode輕量級,但是配置運行程序卻是重量級。也許只適合大神,菜雞的我表示用的很難受!!!

廢話不多說,下面把配置vscode可以調用opencv的c++程序貼出來,重要信息寫在配置文件裏面了。說明一下,需要安裝opencv4.1.0版本,其他版本不保證能運行成功,尤其是4版本以下的。(如果沒有安裝opencv,可以參考這裏

工程目錄如下:

main.cpp

#include<iostream>
#include<opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int main()
{
    
    Mat srcImage=imread("123.jpeg");
    imshow("Origin",srcImage);
    waitKey(0);
    return 0;
}

tasks.json

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    // 在當前文件是C++的情況下,tasks可以被用來做編譯,而launch用來執行編譯好的文件
    // 查看很多的博客調用opencv庫都是通過在“args”中添加相應的依賴項和動態庫,反正
    // 我在Ubuntu16.04中沒有一次成功。都是出現未找到“opencv2/opencv.hpp”庫文件。
    // 不過需要注意的是如果想採用如下方式調用opencv庫(這裏opencv版本是4.1.0,4版本之前和這可能會有細微差別),
    // 在自己編譯opencv時,cmake參數最好有這一項:-DOPENCV_GENERATE_PKGCONFIG=ON。沒有的話極大可能會失敗
    "version": "2.0.0",
    "tasks": [
        {
            "label": "C++_build",
            "type": "shell",
            "command": "g++ -std=c++11 ${workspaceRoot}/main.cpp -o ${workspaceRoot}/main `pkg-config --cflags --libs opencv4`",
            "args": [
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

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
    // 在當前文件是C++的情況下,tasks可以被用來做編譯,而launch用來執行編譯好的文件
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",                //配置名稱,會在啓動配置的下拉菜單中顯示
            "type": "cppdbg",                      //配置類型,只能爲cppdbg
            "request": "launch",                   //請求類型,可以爲launch或attach
            "program": "${workspaceFolder}/main",  //運行編譯的文件所在路徑
            "args": [],                            //調試時傳遞給程序的命令行參數
            "stopAtEntry": false,                  //設爲true程序會暫停在入口處
            "cwd": "${workspaceFolder}",           //任務開始運行時的當前工作目錄
            "environment": [],                     //用戶自己可以設定的環境變量
            "externalConsole": false,              //是否打開終端
            "MIMode": "gdb",                       //指定連接的調試器,可以爲gdb或lldb
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            //"preLaunchTask": "build"              //調試開始前執行的任務,一般爲編譯程序(注意需要和tasks.json文件的“label”值一樣)
        }
    ]
}

c_cpp_properties.json

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "/usr/local/include/",
                "/usr/local/include/opencv4"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "clang-x64"
        }
    ],
    "version": 4
}

搭建好工程目錄和把圖片放到相應位置後,按住shift+Ctrl+b編譯文件,然後按F5運行程序。我的運行結果如下:

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章