linux下vscode的c++工程配置


准備

安裝vscode,可直接下載deb包進行安裝,完成后安裝C/C++ for Visual Studio Code插件,安裝后重啟(最新1.3版本以后不需要重啟)。

生成目錄和文件

新建文件夾【test】,並新建文件helloworld.cpp文件,文件中內容如下,

#include <iostream>
#include <string>
using namespace std;
int main(int argc, char const *argv[])
{
    cout<< "hello world" << endl;
    return 0;
}

使用vscode打開文件夾

配置c++ IntelliSense

使用F1,打開命令選項,輸入C/C++,選擇C/C++:Edit configuration,生成c_cpp_properties.json配置文件。

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

其中最主要為"includePath"的引用和庫的路徑,根據引用內容進行配置。

launch

在debug界面中選擇添加配置,然后選擇才c++(gdb/lgdb)選項,生成launch.json 顧名思義此文件主要服務於調試時的加載控制

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/helloworld",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "preLaunchTask": "build",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

需要注意的參數為"program",此為需要調試的目標文件,應當設置為編譯輸出的文件位置;其次需要添加"preLaunchTask",此項的名字應與下面所建的tasks.json中的任務名稱一致。

tasks.json

在命令窗口中輸入task,選擇task: configure task選項生成tasks.json文件

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "type": "shell",
            "command": "g++",
            "args":[
                "-g","helloworld.cpp","-o","helloworld"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

注意launch.json中的"preLaunchTask"調用與“label”相同的task。

開始調試

按下F5開始調試吧,一切就是這么簡單,開始美好的旅程。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM