How does vscode configure launch.json and tasks.json in the c/c + + language environment in the latest version?

Author: come to know
Link: https://www.zhihu.com/question/336266287/answer/2144611720
Source: Zhihu
The copyright belongs to the author. For commercial reprint, please contact the author for authorization, and for non-commercial reprint, please indicate the source.

 

 

 

 ===============================================================

 

 

 

 

catalogue

 

General description

 

General features:

  • JSON(JavaScript Object Notation) is a javascript based method Data exchange language
  • Core file: tasks.json/launch.json
  • Other documents: c_cpp_propertis.json / settings.json / compile_commands.json
  • Auxiliary plug-in: C/C++



 

Main functions:

 

 

 
 
 
 
 

Reuse reference profile key points:

  • Get the demo configuration file, refer to tasks.json and launch.json, and modify the corresponding gdb/gcc path, source file, header file, etc
  • Ensure that the EXE name called by lauch is consistent with the EXE name generated by tasks. After modification, ctrl + shift + b or click Terminal - > run generation task, that is, call tasks.json to generate exe
  • Press the shortcut key F5 to run, or click the tortoise plus play button in the left column to find the green play button in the upper left corner to start debugging, that is, call lauch.json and run exe

 

Examples

Take compiling and running a single main.c as an example, the configuration is as follows:
 

main.c

#include <stdio.h>
int main()
{
    printf("Hi World!\n");
    return 0;
}

 

 
 
 
 

directory structure

  • .vscode
    • tasks.json
    • launch.json
  • main.c
 
 
 
 
 
 
 
 
 
 
 
Core document
 

tasks.json

The template is as follows:


{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Build", // Need and lauch.json in"preLaunchTask"Consistent item naming
            "type": "shell",
            "command": "D:\\xx\\mingw\\bin\\gcc.exe",
            "args": [
              "main.c",
              "-g",
              "-o",
              "main.exe" // output exe Name, with launch Invoked in program Consistent item naming
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            // Use the standard MS compiler pattern to detect errors, warnings and infos
            "problemMatcher": [
                "$gcc"
            ]
        }
    ]
}

 

 
 
 
 
 
 

launch.json

The template is as follows:


{
    // use IntelliSense Understand related properties. 
    // Hover to view the description of an existing property.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "gdbdebug",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}\\main.exe", // Invoked exe Name, with tasks Generated exe Consistent name
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true, // Determines whether the output is displayed in a separate external black window or in IDE Black window display in terminal
            "MIMode": "gdb",
            "miDebuggerPath": "D:\\xx\\mingw\\bin\\gdb.exe",
            "preLaunchTask": "Build", // This item needs to be named with tasks.json Medium label The item names are consistent, and the function is to call each time launch Will be called first tasks.json,So you don't have to do it every time ctrl+shift+b Only after the latest task is generated manually can it be called launch
            "setupCommands": [
                {
                    "description": "by gdb Enable neat printing",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

 

 
 
 
 

Description of important parameters:

  • The program option is the path of the program to be debugged, here is main.exe
  • args is a parameter added at runtime
  • The stopAtEntry option is false by default. If it is set to true, it will pause in the function entry
  • externalConsole is whether to run outside the external control. If it is set to true, the windows operation window will pop up
  • miDebuggerPath is the path of the gbd debugger
  • setupCommands sets the corresponding commands for the GDB debugger before starting debugging
  • The preLaunchTask option is to run the task executed before debugging

 

 

Other json

c c_cpp_propertis.json / settings.json / compile_commands.json It can be generated automatically through the plug-in and adjusted accordingly. It will not be repeated.

 
 
 
 
 
Common error reporting

Compilation prompt: gcc task not found

  • Root cause: the label name in tasks.json does not correspond to the preLaunchTask name in launch.json, so the corresponding exe cannot be found in the compiled launch
  • Analysis: tasks.json is compiled, launch.json is run exe, and tasks.json is before lanuch. The label in tasks must correspond to the name of preLaunchTask in launch.json. After that, the error that gcc task cannot be found will not be reported.

Multiple main function entries, resulting in compilation abort. Tip: multiple definition of 'main'

  • Delete the relevant. c files, so that these with main do not participate in the compilation.
  • Or exclude the c files related to other main functions in tasks.json and let them not participate in compilation.

 

 
 
 
 
 


 
 
 
 
 
===========================================================
 
 
 
 

 

Tags: C++

Posted on Wed, 01 Dec 2021 19:48:49 -0500 by AlexMason