VScode plug-in recommendation and C/C++ configuration

Posted by Beauford on Thu, 29 Aug 2019 16:45:42 +0200

The following are the VScode plug-ins I often use. Because the plug-in itself has detailed configuration and introduction, the installation configuration of the plug-in itself is not explained, and only the main functions of these plug-ins are expended. It is strongly recommended to see the instructions after installing the plug-in. Most of the problems and settings can be found. Don't search the Internet directly because it is English.

Class C++

 

 

These two plug-ins can make writing C++ easier. It has powerful automatic completion function. The specific configuration of C++ will be explained below.

Beautify

I recommend the following two plug-ins for themes and icons. The first Material Theme plug-in was to change background color, code highlighting, and font. The second icon plug-in can make the file structure clearer. Chinese plug-in must be selected for Chinese configuration.

 

 

 

 

 

 

git

I recommend gitlens. Gitlens is extremely powerful. The graphics are just the tip of the iceberg of its functionality. They can even show who made changes in the code section when.

 

 

tab function expansion

The tabout plug-in allows you to press the tab key to jump out of parentheses or quotes without having to press the direction key or the end key.

 

 

cmake

The cmake function makes it easy to write CMakeLists.txt, with automatic completion and highlighting functions.

 

 

 

C/C++ Configuration

Whether it's Linux or Windows, user configurations are placed under. vscode. Here is a description of user configuration and global configuration. User configurations are specific to a project or folder. All configuration files are placed in the. vscode hidden folder under this folder.

 

 

The entry to the global configuration is shown in the following figure. All configuration changes here will be visible in any folder.

 

 

Windows

First paste out the configuration, then explain the various files.

c_cpp_properties.json is configured as follows:

{
    "configurations": [
        {
            "name": "MinGW",
            "compilerPath": "C:\\MinGW\\bin\\g++.exe",
            "includePath": [
                "${workspaceFolder}"
            ],
            "defines": [],
            "browse": {
                "path": [
                    "${workspaceFolder}"
                ],
                "limitSymbolsToIncludedHeaders": true,
                "databaseFilename": ""
            },
            "cStandard": "c11",
            "cppStandard": "c++17"
        }
    ],
    "version": 4
}

  

launch.json:

// https://github.com/Microsoft/vscode-cpptools/blob/master/launch.md
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch", // Configuration name, which will be displayed in the drop-down menu of boot configuration
            "type": "cppdbg", // Configuration type, cppdbg only
            "request": "launch", // Request configuration type, which can be launch (start) or attach (attach)
            "program": "${fileDirname}/${fileBasenameNoExtension}.exe", // The path of the program to be debugged
            "args": [], // Command-line parameters passed to the program during debugging are usually set to null.
            "stopAtEntry": false, // When set to true, the program will pause at the program entry. I usually set it to true.
            "cwd": "${workspaceFolder}", // Working Directory for Debugging Programs
            "environment": [], // (Environmental variables?)
            "externalConsole": true, // Whether the console window is displayed during debugging is generally set to true display console
            "internalConsoleOptions": "neverOpen", // If it is not set to neverOpen, debugging will jump to the "Debugging Console" tab, you should not need to manually input commands to gdb, right?
            "MIMode": "gdb", // Specify the debugger for the connection, which can be gdb or lldb. However, there is no pre-compiled version of lldb under windows.
            "miDebuggerPath": "gdb.exe", // Debugger path, Windows suffix cannot be omitted, Linux suffix can be removed
            "preLaunchTask": "build" // The task performed before the debugging session starts is generally a compiler. Corresponding to tasks.json's label
        }
    ]
}

  

setting.json:

{
    "files.associations": {
        "iostream": "cpp"
    }
}

  

tasks.json:

// https://code.visualstudio.com/docs/editor/tasks
{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build", // Task name, corresponding to launch.json's preLaunch Task
            "command": "g++", // Compiler to use
            "args": [
                "-g",// Generating and debugging information
                "${file}",
                "-o", // Specify the output file name, default output a.exe without this parameter, default output a.out under Linux
                "${fileDirname}\\${fileBasenameNoExtension}.exe",
                //"${fileDirname}/${fileBasenameNoExtension}.exe",
            ], // Compile command parameters
            "type": "shell", // For shell or process, the former is equivalent to opening the shell before entering the command, and the latter is to run the command directly.
            "group": {
                "kind": "build",
                "isDefault": true // Setting it as false can accomplish one task. JSON to configure multiple compilation instructions and need to modify this file by itself. I won't mention much here.
            },
            "problemMatcher":{
                "owner": "$gcc",
                "fileLocation":"absolute",
                "pattern":[
                    {
                        "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
                        "file": 1,
                        "line": 2,
                        "column": 3,
                        //"location": 2,
                        "message": 5
                    }
                ]
             }
        }
    ]
}

  

Linux

launch.json

{
    // Use IntelliSense to understand the relevant properties. 
    // Hover to see the description of existing properties.
    // For more information, visit https://go.microsoft.com/fwlink/?linkid=830387.
    "version": "0.2.0",
    "configurations": [
        {
            "name": "gcc build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "gcc build active file",
            "miDebuggerPath": "/usr/bin/gdb"
        }
    ]
}

  

tasks.json

{
// For documents in tasks.json format, see
    // https://go.microsoft.com/fwlink/?LinkId=733558
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "gcc build active file",
            "command": "/usr/bin/gcc",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}",
                "-l",
                "pthread"
​
            ],
            "options": {
                "cwd": "/usr/bin"
            },
            "problemMatcher": [
                "$gcc"
            ]
        }
    ]
}

  

Configuration instructions

Generally speaking, the debug process of c++ is like this. First, vscode calls launch.json. launch.json calls task. JSON named "gcc build active file" according to the name of "preLaunchTask", "gcc build active file". There are many tasks in task. json, and the corresponding task can be called according to label name in tasks. Task is mainly responsible for compiling into executable files. If only executable files need to be generated, press crtl+shift+b directly.

c_cpp_properties are specially configured for c/c++ compilation, such as include paths.

It is strongly recommended to look at the English in json, whose description is very easy to understand. The compilation process of c++ and the people who have a little basic knowledge of Linux are able to quickly master and customize them personally.

Detailed vscode configuration see Microsoft . I strongly recommend looking at Microsoft's docs notes. Although it seems time-consuming to read, it's much faster than searching for all kinds of information on the Internet. Because even if you follow my configuration settings successfully, when applied to different projects or languages, it may be difficult to configure.

Topics: C++ JSON Linux Windows shell