Interpretation of the json configuration file of C++ in Vscode

g ++ and gcc can run a c/cpp file in the local terminal

Terminal try
gcc -v
g + + -v

Contains the compiler if there is information about the version.

g++ mian.cpp -o main.exe & & main
-o followed by the file name of the generated exe, exe can be omitted
 & amp; & amp; followed by the generated exe file, exe can be omitted
In fact, these are two commands, because the execution of the exe file can directly enter the file name on the command line. 

Check gcc, g++ version

gcc runs c program

g++ run cpp program

If the terminal can run, but not in vscode, there is a high probability that it is a problem with the compilation command

open settings

Enter the settings.json global file

Modify the run command

If it can run successfully after this step, you don’t need to read the following, the following is the local settings for the current folder! ! !

Set the settings.json file under the .vscode folder

In fact, what is ultimately needed is code-runner.executorMap, because it sets what your command line looks like, in other words, it becomes a general command to generate and run an exe file.

If it can be run in the terminal, it should be possible to change the original command to the one below.

Secondly, if you want to delve deeper, take a look at the notes behind each one.

{
    "files.defaultLanguage": "cpp", // ctrl + N default language after creating a new file
    "editor.formatOnType": true, // format when input, the default trigger character is less, semicolon can trigger
    "editor.snippetSuggestions": "top", // snippets code first display completion
?
    "code-runner.runInTerminal": true, // If set to false, it will be output in "Output" and cannot be input
    "code-runner.executorMap": {
        "c": "cd $dir & amp; & amp; g + + $fileName -o $fileNameWithoutExt.exe & amp; & amp; $dir$fileNameWithoutExt",
        "cpp": "cd $dir & amp; & amp; g + + $fileName -o $fileNameWithoutExt.exe & amp; & amp; $dir$fileNameWithoutExt"
    }, // Set the command line of the code runner
    "code-runner.saveFileBeforeRun": true, // save before running code
    "code-runner.preserveFocus": true, // If false, the cursor will focus on the terminal after running code. If you need to enter data frequently, it can be set to false
    "code-runner.clearPreviousOutput": false, // Clear the terminal messages belonging to the code runner before each run code
?
}

c_cpp_properties.json – Compilation environment related settings

This file is used to specify the general compilation environment, including header file path, compiler path, etc. Open the command line through Ctrl + Shift + p, type the keyword “C ++ “, select “C/C ++ Edit configuration” in the drop-down menu, and the system will automatically create c_cpp_properties.json in the .vscode directory Files for users to configure the environment for compilation.

  • compilerPath should be accurate to: gcc.exe

  • Cmd to get includePath: gcc -v -E -x c + + - (function: prevent include from reporting error: file not found)

  • IncludePath must have: " " before and after each item, and each item must have: ,

  • I really can’t find the path, download everything software, and find out where the path is

{
    "configurations": [
        {
            "name": "Win32", //environment name
            "includePath": [
                "${workspaceFolder}/**", //Specify the path of the header file, the current working directory is specified here, if necessary, add it later, and the way to add it is to add the path in the format of "/to/path"
                "C:\Program Files (x86)\Dev-Cpp\MinGW64\lib\gcc\x86_64-w64-mingw32\4.8. 1\include\c + + ",
                "C:\Program Files (x86)\Dev-Cpp\MinGW64\lib\gcc\x86_64-w64-mingw32\4.8. 1\include",
                "C:\Program Files (x86)\Dev-Cpp\MinGW64\lib\gcc\x86_64-w64-mingw32\4.8. 1\include\c + + \x86_64-w64-mingw32",
                "C:\Program Files (x86)\Dev-Cpp\MinGW64\lib\gcc\x86_64-w64-mingw32\4.8. 1\include\c + + \backward",
                "C:\Program Files (x86)\Dev-Cpp\MinGW64\lib\gcc\x86_64-w64-mingw32\4.8. 1\include-fixed",
                "C:\Program Files (x86)\Dev-Cpp\MinGW64\lib\gcc\x86_64-w64-mingw32\4.8. 1\..\..\..\..\x86_64-w64-mingw32\include"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "compilerPath": "D:\Program Files (x86)\mingw64\bin\gcc.exe", //The path of the compiler can be based on your own setup for installation
            "cStandard": "gnu17",
            "cppStandard": "gnu++ 14", //Set the C/C++ standard used
            "intelliSenseMode": "windows-gcc-x64"
        }
    ],
    "version": 4
}

tasks.json

This file is used to specify the compilation rules of the program, that is, how to compile the source file into an executable program. Open the command line via Ctrl + Shift + p, type the keyword “task”, and select Tasks in the drop-down menu: Configure Default Build Task -> Create tasksk.json file from template -> Others, the system will automatically create tasksk in .vscode Create a task.json file (basic template) in the directory for users to set specific compilation rules.

Note that only a simplified template of task.json is generated at this time, modify the value of the label key in the file, and then open the command line, select the Tasks:Configure Default Build Task option, and the modified label name will appear in the option bar , when the name of the label is selected, the corresponding task.json file will change and be set as the default compilation object (that is, when the command Tasks: Run Build Tasks is executed, the compilation corresponding to the task.json file is executed by default) , roughly formatted as follows. Users need to modify the key values according to the actual situation.

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Compile", // task name, corresponding to preLaunchTask of launch.json
            "command": "clang + + ", // compiler to use
            "args": [
                "${file}",
                "-o", // Specify the output file name, if this parameter is not added, the default output is a.exe, and the default is a.out under Linux
                "${fileDirname}/${fileBasenameNoExtension}.exe",
                "-g", // generate and debug related information
                "-Wall", // enable additional warnings
                "-static-libgcc", // static link
                "-fcolor-diagnostics", // colored error messages? But it seems that clang is enabled by default and gcc does not accept this parameter
                "--target=x86_64-w64-mingw", // clang's default target is msvc, if you don't add this one, you won't find the header file; under Linux, remove this one
                "-std=c + + 17" // The latest standard of C language is c11, or modify it according to your own needs
            ], // compile command parameters
            "type": "shell", // can be shell or process, the former is equivalent to opening the shell first and then entering the command, the latter is to run the command directly
            "group": {
                "kind": "build",
                "isDefault": true // Set to false to configure multiple compilation instructions in one tasks.json, you need to modify this file yourself, I won’t mention it here
            },
            "presentation": {
                "echo": true,
                "reveal": "always", // The strategy for displaying compilation information in the "terminal", which can be always, silent, never. See VSC documentation for details
                "focus": false, // Set to true to focus on the terminal when executing tasks, but for compiling c and c ++, setting it to true is meaningless
                "panel": "shared" // The compilation information of different files shares a terminal panel
            }
            // "problemMatcher":"$gcc" // If you don't use clang, remove the preceding comment and add a comma after the previous one. Do not need to change according to my tutorial (you can also delete this line)
        }
    ]
}

Simplify your own as follows:

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "build",
            "command": "C:\Program Files (x86)\Dev-Cpp\MinGW64\bin\g + + .exe",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": "build",
            "detail": "Compiler: "C:\Program Files (x86)\Dev-Cpp\MinGW64\bin\g++ .exe""
        },
        {
            "type": "cppbuild",
            "label": "C/C++ : g++ .exe generates active files",
            "command": "C:/Program Files (x86)/Dev-Cpp/MinGW64/bin/g++.exe",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "C:/Program Files (x86)/Dev-Cpp/MinGW64/bin"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by the debugger."
        }
    ]
}

launch.json

This file is mainly related to the debugging of the program. The user can open the command line through Ctrl + Shift + p, type the keyword “launch”, select “Debug:Open launch.json” -> “C ++ (GDB/LLDB)” to open Configuration file launch.json for debugging. This file sets the basic content and requirements of VScode when debugging. After configuring the launch.json file, press F5 to enter the debugging mode. The basic options for the launch.json file are as follows.

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch", // configuration name, will be displayed in the drop-down menu of the launch configuration
            "type": "cppdbg", // Configuration type, here can only be cppdbg
            "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 program debugging, generally set to empty
            "stopAtEntry": false, // When set to true, the program will pause at the program entry, I usually set it to true
            "cwd": "${workspaceFolder}", // The working directory when debugging the program
            "environment": [], // (environment variable?)
            "externalConsole": true, // Whether to display the console window during debugging, generally set to true to display the console
            "internalConsoleOptions": "neverOpen", // If it is not set to neverOpen, it will jump to the "Debug Console" tab when debugging. You should not need to manually enter commands to gdb, right?
            "MIMode": "gdb", // Specify the connected debugger, which can be gdb or lldb. But currently lldb does not have a pre-compiled version under windows.
            "miDebuggerPath": "gdb.exe", // debugger path, the suffix cannot be omitted under Windows, and removed under Linux
            "setupCommands": [ // Unknown purpose, so template
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": false
                }
            ],
            "preLaunchTask": "Compile" // The task performed before the debugging session starts, usually compiling the program. Corresponds to the label of tasks.json
        }
    ]
}

Simplify your own as follows:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch", // configuration name, will be displayed in the drop-down menu of the launch configuration
            "type": "cppdbg", // Configuration type, here can only be cppdbg
            "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 program debugging, generally set to empty
            "stopAtEntry": true, // When set to true, the program will pause at the program entry, I usually set it to true
            "cwd": "${workspaceFolder}", // The working directory when debugging the program
            "environment": [], // (environment variable?)
            "externalConsole": true, // Whether to display the console window during debugging, generally set to true to display the console
            "internalConsoleOptions": "neverOpen", // If it is not set to neverOpen, it will jump to the "Debug Console" tab when debugging. You should not need to manually enter commands to gdb, right?
            "MIMode": "gdb", // Specify the connected debugger, which can be gdb or lldb. But currently lldb does not have a pre-compiled version under windows.
            "miDebuggerPath": "C:\Program Files (x86)\Dev-Cpp\MinGW64\bin\gdb.exe", // debugger Path, the suffix cannot be omitted under Windows, and it can be removed under Linux
            "setupCommands": [ // Unknown purpose, so template
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": false
                }
            ],
            "preLaunchTask": "build" // The task performed before the debugging session starts, usually compiling the program. Corresponds to the label of tasks.json
        }
    ]
}

Successful presentation