Configuring VSCode development environment under linux

Configuring VSCode development environment under linux

We must first of its profits

  • System: Manjaro
  • Kernel version: 5.4.24
  • gcc Version: 9.2.1
  • VSCode:1.43.0

VSCode Download

Since my Linux machine is Manjaro (a version of ArchLinux), installation VSCode traditional way and also Debian and RedHat-based system is not the same.

Arch versions can be mounted by normally

$ sudo pacman -S visual-studio-code-bin

But I trust that prompted the signature is unknown, the file is corrupted. So Shangguan net direct download archive.

Visual Studio Code

Download the tar.gzarchive and unpack.

Preparing the environment

  • Create a project directory
$ cd ~
$ mkdir -p workspace/clang/helloworld # 创建helloworld目录
$ cd workspace/clang/helloworld
  • Installation compilation environment

VSCode just an editor, compiler work still needs to provide users with the build environment.

Use versionor whereisto ensure that your computer is installed g++,gdb

$ g++ --version
$ gdb --version
$ whereis g++
$ whereis gdb

Here I start the computer whereis gdbshowed the following path, but can not call versionthis is not enough

$ whereis gdb
gdb: /usr/share/gdb

If not, then use the corresponding command to install linux series, as follows Manjaro,

$ pacman -S g++ gdb

Start VS Code

Switch to the project folder directory, start vscode, or directly to the directory folder into vscode.

The next operation will be automatically established under a project directory .vscodefolder, and built on the folder, which does not require manual

  • c_cpp_properties.json: Compiler path configuration
  • task.json: Command compile time configuration
  • launch.json: Debug Configuration

write the code

  • New helloworld.cppfile, write the first program of each languageHello world
#include <iostream>
#include <string>
using namespace std;

int main(){
    cout << "Hello, world! from VS Code!" << endl;
}
  • Installation C / C ++ plug-ins

On the left side of the plug-in option card, search for C ++, the Microsoft installation C/C++on the line

Back helloworld.cppthen we create tasks.json, tell VS Code how to compile the program.

  • Through the menu Terminal>Configure Default Build Task. You will see a drop-down box, selectC/C++:g++ build active file

At this time, in your catalog will be more of a .vscodefolder, which is automatically generated tasks.json.

Along the following lines

{
    // task.json
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "g++ build active file",
            "command": "/usr/bin/g++",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "/usr/bin"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

commandUsed to set compiler, here is the g++path. argsIs usually in the command line, g ++ command parameters that follow. Here the current file is compiled file $ {file}, the compiler output the same name in the file is the same path without the suffix helloworld.

  • Back helloworld.cppthrough the menu Terminal>Run Build Taskto compile. If successful, the following input terminal

  • Running in the terminal, enter the command./helloworld

#include error

If the total is underlined C program and suggesting检测到#include错误。请更新includePath

  • Through the Ctrl+Shift+Popen command to navigate, run the commandC/C++: Edit Configurations(UI)

In includePathadding, g ++ include position location

Command $ g++ -v -E -x c++ -to view, simply add the two as shown

It will be generated for c_cpp_properties.jsonfiles

debugging

  • Through the menu Run>Add Configuration, choose C++ (GDB/LLDB)>g++ build and debug active fileto create a launch.jsonfile

File looks like

{
    // launch.json
    "version": "0.2.0",
    "configurations": [
        {
            "name": "g++ build and debug active file",
            "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": "g++ build active file",
            "miDebuggerPath": "/usr/bin/gdb"
        }
    ]
}

Which miDebuggerPathis the path of gdb, also it can be stopAtEntryset trueso that a breakpoint is set automatically when entering the main function.

carry out

At this point, create a project, compiling, debugging may have been carried out in the VS Code.

Guess you like

Origin www.cnblogs.com/Axi8/p/12517059.html