c + + project configuration of vscode under linux

Posted by asterinex on Mon, 02 Dec 2019 03:01:49 +0100

Get ready

install vscode , you can download the deb package directly for installation, install the C/C++ for Visual Studio Code plug-in after completion, and restart after installation (no need to restart after the latest version 1.3).

Generate directories and files

Create a new folder [test] and a new file helloworld.cpp. The contents of the file are as follows:,

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

Open folder with vscode

Configure c++

Use F1, open the command option, enter C/C + +, select C/C++:Edit configuration, and generate the C ﹣ CPP ﹣ properties.json configuration file.

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

The most important ones are the references of "includePath" and the path of the library, which are configured according to the reference content.

launch

In the debug interface, select Add configuration, then select the option of c++(gdb/lgdb), and generate launch.json. As the name implies, this file is mainly used for loading control during debugging

{
    "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
                }
            ]
        }
    ]
}

The parameter to be noted is "program", which is the target file to be debugged and should be set as the file location for compiling output; secondly, "preLaunchTask" needs to be added, and the name of this item should be consistent with the task name in the tasks.json created below.

tasks.json

Enter task in the command window and select the task: configure task option to generate the tasks.json file

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

Note that "preLaunchTask" in launch.json calls the same task as "label".

Start debugging

Press F5 to start debugging, everything is so simple, start a good journey.

Topics: C++ JSON Linux shell