C++ project configuration of vscode under linux

ready

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

Generate directories and files

Create a new folder [test], and create 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 the folder with vscode

Configure c++ IntelliSense

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 reference of "includePath" and the path of the library, which are configured according to the content of the reference.

launch

In the debug interface, choose to add configuration, and then select the c++ (gdb/lgdb) option to generate launch.json. As the name suggests, this file mainly serves 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 that needs attention is "program", which is the target file that needs to be debugged and should be set to the file location of the compiled output; secondly, you need to add "preLaunchTask", the name of this item should be the same as the task name in the tasks.json created below Consistent.

tasks.json

Enter task in the command window, select task: configure task option to generate 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 as simple as that, and start a beautiful journey.

Category: C&&C++ knowledge , linux

Guess you like

Origin blog.csdn.net/qq_27009517/article/details/112176317