Ubuntu16.04下vscode第一個C++程序以及opencv配置文件

用vscode作爲自己用的編譯器,首先需要了解並掌握的就是配置文件。下面記錄的主要是配置文件,以及配置文件參數的意義。

一.C++配置文件

下面配置文件可以運行

main.cpp

#include<iostream>
using namespace std;
int main()
{
    cout << "Hello world!" << endl; 
    return 0;
}

tasks.json

//tasks.json
{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build", //在launch.json文件中有用到
            "type": "shell",
            "command": "g++",
            "args": [
                "-g", "main.cpp"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

launch.json

//launch.json
{
    // 使用 IntelliSense 瞭解相關屬性。 
    // 懸停以查看現有屬性的描述。
    // 欲瞭解更多信息,請訪問: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",                                 //配置名稱,會在啓動配置的下拉菜單中顯示
            "type": "cppdbg",                                       //配置類型,只能爲cppdbg
            "request": "launch",                                    //請求類型,可以爲launch或attach
            "program": "${workspaceFolder}/a.out",                  //將要調試的程序的路徑
            "args": [],                                             //調試時傳遞給程序的命令行參數
            "stopAtEntry": false,                                   //設爲true程序會暫停在入口處
            "cwd": "${workspaceFolder}",                            //調試程序時的工作目錄
            "environment": [],                                      //環境變量
            "externalConsole": true,                                //調試時是否顯示控制檯窗口
            "MIMode": "gdb",                                        //指定連接的調試器,可以爲gdb或lldb
            "miDebuggerPath": "/usr/bin/gdb",                       //gdb路徑
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "build"                                //調試開始前執行的任務,一般爲編譯程序
        }
    ]
}

二.調用opencv程序的配置文件

反正我是沒有調試出來,可以參考一下。這裏的vscode配置opencv庫可以使用。

main.cpp

#include <iostream>
 
#include <opencv2/opencv.hpp>
using namespace cv;
int main(int arg, char** args) {
	std::cout << "aa" << std::endl;
 
    std::string img = "123.png"; // 同級目錄下
    Mat srcImage = imread(img);
    if (!srcImage.data) {
        return 1;
    }
    imshow("srcImage", srcImage);
    std::cout<<"hahh"<< std::endl;
    return 0;
}

launch.json

{
    // 使用 IntelliSense 瞭解相關屬性。 
    // 懸停以查看現有屬性的描述。
    // 欲瞭解更多信息,請訪問: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "gcc - 生成和調試活動文件",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "爲 gdb 啓用整齊打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "gcc build active file",
            "miDebuggerPath": "/usr/bin/gdb"
        }
    ]
}

tasks.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": "msbuild",
            "args": [
                "-g", "-std=c++11", "${file}", "-o", "${fileBasenameNoExtension}.out",// 設置動態鏈接庫
                "-I", "/usr/local/include",
                "-I", "/usr/local/include/opencv4",
                "-I", "/usr/local/include/opencv4/opencv2",
                "-L", "/usr/local/lib",
                "-l","${workspaceFolder}/include",
                "-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"
            ],
            "group": "build",
            "presentation": {
                // Reveal the output only if unrecognized errors occur.
                "reveal": "silent"
            },
            // Use the standard MS compiler pattern to detect errors, warnings and infos
            "problemMatcher": "$msCompile"
        }
    ]
}

c_cpp_properties.json

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

 

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