0. Say it first

First, remember that vscode itself is a plain text editor, not an IDE, with no compiler or other features. Then, VSC official website, download, install, start for the convenience of simplified Chinese plug-in, needless to say.

1. Configure the environment

Download and install the compilerMingw – w64

Find the latest version of X86_64-POSIX-seh. If you cannot download it due to network problems, you can directly find the compressed package. ~~(search engine is the ladder of human progress)~~ after the decompression to their own convenient place, I am in C:\mingw64, find the bin folder, copy its path C:\mingw64\bin. Right-click this computer: Properties – Advanced system Settings – Advanced – Environment traversal – above column PATH- Double click – New – paste the PATH just copied into – ok, verify: win+R- enter CMD – enter GCC -v

Install plugins in VSC:

The rest of the plugins that beautify the interface can search for what they like (search engines are the ladder of human progress)

2. Configure the JSON file

You can create a folder to store C/C++ and then create the following files in it

(CPPSPACE is a folder I created myself, I’ll call it whatever I want.) Then set the json files separately: launch.json:

// https://code.visualstudio.com/docs/cpp/launch-json-reference
{
    "version": "0.2.0"."configurations": [{
        "name": "(gdb) Launch".// The configuration name will be displayed in the drop-down menu of the startup configuration
        "type": "cppdbg".// Configuration type. For C/C++, this can only be CPPDBG, provided by CPPTools; Different programming languages differ
        "request": "launch".// Launch or attach
        "program": "${fileDirname}/${fileBasenameNoExtension}.exe".// The path of the program to be debugged
        "args": [].// Command line arguments passed to the program during debugging
        "stopAtEntry": true.// If set to true, the program will pause at the program entrance, equivalent to breaking on main
        "cwd": "${workspaceFolder}".// This is the workspace folder; ${fileDirname} changes to the file directory
        "environment": [].// Environment variables
        "externalConsole": false.// Use a separate CMD window, consistent with other ides; Use the built-in terminal when false
        "internalConsoleOptions": "neverOpen".// If neverOpen is not set, debugging will jump to the "debug console" TAB. You don't need to manually type commands into GDB, do you?
        "MIMode": "gdb".// Specify the debugger to connect to, either GDB or LLDB. But I haven't tried LLDB
        "miDebuggerPath": "gdb.exe".// Debugger path. The suffix cannot be omitted under Windows, but not under Linux
        "setupCommands": [{// The template comes with it, which seems to better display the contents of the STL container
                "description": "Enable pretty-printing for gdb"."text": "-enable-pretty-printing"."ignoreFailures": false}]."preLaunchTask": "Compile" // The task performed before debugging, usually the compiler. Corresponds to the label of tasks.json}}]Copy the code

settings.json:


{
    "files.defaultLanguage": "c".// CTRL +N Specifies the default language for creating a new file
    "editor.formatOnType": true.// Automatically formats the current line of code after entering a semicolon (end-of-statement identifier in C/C++)
    "editor.suggest.snippetsPreventQuickSuggestions": false.// Clangd's snippets have so many jump points that without this Intellisense must be triggered manually
    "editor.acceptSuggestionOnEnter": "off".// Off Only TAB accepts Intellisense, and on is carriage return
    / / "editor. SnippetSuggestions" : "top", / / (optional) snippets shown at the top of the completion list, the default is the inline

    "code-runner.runInTerminal": true.// Set to false will output in "Output" and cannot be entered
    "code-runner.executorMap": {
        "c": "gcc '$fileName' -o '$fileNameWithoutExt.exe' -Wall -O2 -m64 -lm -static-libgcc -fexec-charset=GBK -D__USE_MINGW_ANSI_STDIO && &'./$fileNameWithoutExt.exe'"."cpp": "g++ '$fileName' -o '$fileNameWithoutExt.exe' -Wall -O2 -m64 -static-libgcc -fexec-charset=GBK && &'./$fileNameWithoutExt.exe'"
        // "c": "gcc $fileName -o $fileNameWithoutExt.exe -Wall -O2 -m64 -lm -static-libgcc -fexec-charset=GBK -D__USE_MINGW_ANSI_STDIO && $dir$fileNameWithoutExt.exe",
        // "cpp": "g++ $fileName -o $fileNameWithoutExt.exe -Wall -O2 -m64 -static-libgcc -fexec-charset=GBK && $dir$fileNameWithoutExt.exe"
    }, // Right-click the command to run with run code; Uncommented only works with PowerShell (default in Win10) and PWSH. Commented out works with CMD (default in Windows 7), PS, and bash, but will not run with Spaces in the file name
    "code-runner.saveFileBeforeRun": true.// Save before run code
    "code-runner.preserveFocus": true.// If false, the cursor will focus on the terminal after run code. Set this parameter to false if you need to enter data frequently
    "code-runner.clearPreviousOutput": false.// Clear terminal messages belonging to Code Runner each time before run code. Default is false
    "code-runner.ignoreSelection": true.// The default value is false. The effect is that a block of code selected by the mouse can be executed separately, but C is a compiled language and is not suitable for this
    "code-runner.fileDirectoryAsCwd": true.// Switch the code Runner terminal's working directory to the file directory and run it again, which affects programs that depend on CWD; If it is false, executorMap adds CD $dir

    "C_Cpp.clang_format_sortIncludes": true.// Adjust the order of the include when formatting (alphabetically). This should not matter
}
Copy the code

tasks.json:

// https://code.visualstudio.com/docs/editor/tasks
{
    "version": "2.0.0"."tasks": [{
        "label": "Compile".// The task name corresponds to the preLaunchTask in launch.json
        "command": "g++".// the compiler to use is g++
        "args": [
            "${file}"."-o".// Specify the output file name. If this parameter is not specified, the default output is A.exe. on Linux, the default output is A.out
            "${fileDirname}/${fileBasenameNoExtension}.exe"."-g".// Generate and debug related information
            "-m64".// This clause can be forced to generate 64-bit
            "-Wall".// Enable additional warnings
            "-static-libgcc".// Static link libgcc is usually added
            "-fexec-charset=GBK".// The generated program uses GBK encoding, not adding this will result in Chinese garbled output in Win; Traditional system changed to BIG5
            "-D__USE_MINGW_ANSI_STDIO".// use MinGW to write C, otherwise do not need, for printf support %zd, %Lf, etc].// Compile the command, in fact, the VSC for you to enter these things in the terminal
        "type": "process".// Process parses predefined variables and escapes directly to command; Shell is equivalent to opening the shell and then typing the command, so arGS is parsed through the shell again
        "group": {
            "kind": "build"."isDefault": true // If not true, CTRL + Shift + B should be selected manually
        },
        "presentation": {
            "echo": true."reveal": "always".// Whether to switch to the terminal panel during task execution. The value can be always, silent, or never. For details, see the VSC documentation. Even if set to never, you can still see it manually
            "focus": false.// True enables task execution to focus on the terminal, but does not make sense for C/C++ compilation
            "panel": "shared"   // Compile information for different files shares a terminal panel
        },
        "problemMatcher":"$gcc" // Capture compile-time error messages in the terminal to the problems panel, modify the code will need to be recompiled again
        // With MinGW, Lint has a bad effect in problemMatcher. You can comment it out with Clangd}}]Copy the code

Most of the above comments can be ignored, pay attention to the launch. The json: the “externalConsole” : false, and Settings. Json: the code – runner. ClearPreviousOutput “: False may need to be customized to your liking.

4. The last

Now you are ready to create c/ C ++ files, taking care not to have Chinese characters on the suffix and path. The first advantage of compiling and running √ VSC is probably pretty