Home >Development Tools >VSCode >How to configure cpp debugging environment in vscode

How to configure cpp debugging environment in vscode

王林
王林Original
2020-02-10 14:14:474256browse

How to configure cpp debugging environment in vscode

Install C/C plug-in

Open the plug-in page, search and enter C/C to search for C/C plug-in.

How to configure cpp debugging environment in vscode

After installing the plug-in, when using vscode to open the folder containing the cpp file, vscode will add the .vscode subfolder to the directory.

Add c_cpp_properties.json configuration

Run C/Cpp: Edit configurations through the shortcut key ⇧⌘P and add the missing c_cpp_properties.json file. The default added files are as follows:

{
    "configurations": [
        {
            "name": "Mac",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [],
            "macFrameworkPath": [
                "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/System/Library/Frameworks"
            ],
            "compilerPath": "/usr/bin/clang",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "clang-x64"
        }
    ],
    "version": 4
}

No adjustments have been made to this part. The files added by default are used.

Add tasks.json configuration file

Select the command to be executed through the shortcut key ⇧⌘P, select the Task: Configure Task command, and select Create tasks.json from templates, Select Others to create an external command. Replace the commnd option according to your own compiler.

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "cpp",
            "type": "shell",
            "command": "g++",
            "args": [
                "-g",
                "${file}",
                "-o",
                "a.out"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

It needs to be said here that we hope to compile and execute the code of the current tab page, so ${file} is used in the args parameter. Another point that needs to be mentioned is that if the output compiled file is not specified, debugging will be affected.

>> g++ -g question.cpp -o a.out

Of course, you can also replace a.out in args with ${file} to maintain correspondence with the file name.

Add the launch.json configuration file

Click Run on the debugging interface, and you will be prompted to add the launch.json configuration file. It defines the relevant attributes of the startup debugging file.

{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(lldb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/a.out",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "lldb",
            "preLaunchTask": "cpp"
        }
    ]
}

There are three parameters that need to be explained here:

1. Program parameter. The file name here should correspond to the previous one. If a.out was used earlier, it should also be a. .out.

2. The externalConsole parameter will be displayed when hovering over it. If it is a Linux and other systems, when it is set to false, the printing content will be output within the vscode integration. If true, it will be output to the external terminal. In order to see the output in vscode, set it to false here. Another thing to pay attention to is the parameter externalConsole. If you hover it, it will say. If it is a system such as Linux, when it is set to false, the output will be printed within the vscode integration. content. If true, it will be output to the external terminal. In order to see the output in vscode, set it to false here.

3. preLaunchTask parameter, because each debugging requires pre-compiling the code, here you can specify the compilation task through the preLaunchTask parameter. Here it is designated as the previous task: cpp. That is, the content in the label of the previous task.

Recommended related articles and tutorials: vscode tutorial

The above is the detailed content of How to configure cpp debugging environment in vscode. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn