VS Code writes C program. Configure vscode

Posted by kovudalion on Mon, 02 Mar 2020 07:41:02 +0100

1. Environmental preparation

Download compiler: x86-8.1.0-release-posix-seh-rt_v6-rev0.7z

Links: Portal Extraction code: e99j

2. Install compiler: the next step is a 7z compression package. If you don't decompress, you can baidu "how to decompress the compressed package". After decompressing, put it in a place where it is not easy to delete. Some of the layers can be removed. Look at the full path of the bin folder,

3. Add environment variable

This computer -- right click Properties -- advanced system settings -- environment variables -- system variables -- Path -- select path, click Edit -- add

D:\Program Files\x86_64-8.1.0-release-posix-seh-rt_v6-rev0\mingw64\bin

2. Open VSCode

1. Install extension

  • C/C + +: also known as cpptools, providing Debug and Format functions
  • 2. Configure several. json files

    Create a folder where you plan to store your code. It's called workspace folder. The path cannot contain Chinese and quotation marks. It's better not to have spaces. I use C. C and C + + need to create different folders, unless you use a virtual workspace. Do not select the folder created by the previous section. The source code and compiler should be opened separately.

    Open VSC and select open folder. Click new folder. The name is. vscode. The reason for not creating a new folder in resource management is that the first character of the folder that Windows Explorer does not allow to create is dot (it is only supported after 1903). Then create (file) launch.json, tasks.json, settings.json and put it in the. vscode folder.

  • launch.json code

    External console can be modified according to your own preference; cwd can be the relative path of the program runtime, and can be changed to ${fileDirname} if necessary

  • Note that the path of miDebuggerPath is bin/gdb.exe under the path you just unzipped

  •  
  • // https://github.com/Microsoft/vscode-cpptools/blob/master/launch.md
    {
        "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: cppdbg corresponds to the debugging function provided by cpptools; it can only be considered as cppdbg here
            "request": "launch", // Request configuration type, which can be launch or attach
            "program": "${fileDirname}/${fileBasenameNoExtension}.exe", // Path to the program to be debugged
            "args": [], // The command line parameter passed to the program during program debugging, which is usually set to null
            "stopAtEntry": false, // When set to true, the program will pause at the program entrance, which is equivalent to breaking points on main
            "cwd": "${workspaceFolder}", // The working directory when debugging the program, which is the workspace folder; change to ${fileDirname} to the directory where the file is located
            "environment": [], // environment variable
            "externalConsole": false, // When it is true, a separate cmd window is used, which is consistent with other ides; when it is set to false after October 18, VSC built-in terminal can be called
            "internalConsoleOptions": "neverOpen", // If it is not set to neverOpen, you will jump to the "debug console" tab when debugging. You don't need to manually input commands to gdb, do you?
            "MIMode": "gdb", // Specifies the debugger for the connection, which can be gdb or lldb. But I haven't tried lldb
            "miDebuggerPath": "D:/Program Files/x86_64-8.1.0-release-posix-seh-rt_v6-rev0/mingw64/bin/gdb.exe", // Debugger path, suffix cannot be omitted under Windows, not under Linux
            "setupCommands": [
                { // The template comes with it. It seems that it can better display the contents of STL container. The specific function is Google
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": false
                }
            ],
            "preLaunchTask": "Compile" // The task to be performed before the start of the debugging session, usually the compiler. Corresponding to the label s of tasks.json
        }]
    }

     

  • tasks.json code

    If you are writing C + +, the compiler needs to be changed to g + +; if you don't want additional warnings, delete the - Wall clause; - std is modified according to your own needs; under Linux, you don't need to add - fexec charset. Anyway, I've added notes to all these, and I can't understand them. Baidu gcc tutorial.

    Review controls whether to jump to the terminal panel at compile time. It can be modified according to your own preference; even if it is set to never, it just doesn't jump automatically, and you can still see the information by clicking in manually.

  • // https://code.visualstudio.com/docs/editor/tasks
    {
        "version": "2.0.0",
        "tasks": [{
            "label": "Compile", // Task name, corresponding to preLaunchTask of launch.json
            "command": "gcc",   // Compiler to use, g for C + +++
            "args": [
                "${file}",
                "-o",    // Specify the output file name. If this parameter is not added, a.exe will be output by default, and a.out will be output by default under Linux
                "${fileDirname}/${fileBasenameNoExtension}.exe",
                "-g",    // Build information about debugging
                "-Wall", // Turn on extra warning
                "-static-libgcc",     // Static link libgcc, usually with
                "-fexec-charset=UTF-8", // The generated program uses GBK code. If this code is not added, Chinese code will be output under Win
                // "- STD = C11", / / the latest C + + standard is c++17, or modify it according to your own needs
            ], // The compiled command is actually equivalent to VSC helping you to input these things in the terminal
            "type": "process", // process is that vsc directly passes all predefined variables and escape parsing to command; shell is equivalent to opening shell first and entering command, so args will be parsed again by shell
            "group": {
                "kind": "build",
                "isDefault": true // When it is not true, ctrl shift B needs to select manually
            },
            "presentation": {
                "echo": true,
                "reveal": "always", // Whether to jump to the terminal panel when executing tasks can be always, silent, never. See VSC documentation for details
                "focus": false,     // Set to true to focus on the terminal when task is executed, but for compiling C/C + +, set to true has no meaning
                "panel": "shared"   // Compile information of different files share a terminal panel
            },
            // "problemMatcher":"$gcc" / / this option can capture the error reporting information in the terminal at compile time. However, because there is Lint, there may be double error reporting when you open this option again
        }]
    }

     

  • settings.json code

    Put the contents of this file in user settings to overwrite the global settings. Otherwise, it is only valid in the current workspace. Each has its own advantages.

    The command line and some options of Code Runner can be modified here according to your own needs. If you want to customize or know what it means, please refer to the extended documentation and Baidu gcc tutorial. If the terminal uses cmd (Win7 default), it needs to be commented out instead, or change terminal.integrated.shell.windows to PowerShell; Win10 default is PS, so it doesn't need to be changed.

  • {
        "files.defaultLanguage": "c", // ctrl+N default language after creating a new file
        "editor.formatOnType": true,  // Automatically format the current line of code after entering a semicolon (statement end identifier of C/C + +)
        "editor.suggest.snippetsPreventQuickSuggestions": false, // There are many jump points in Cland's snippets. Without this, Intellisense must be triggered manually
        "editor.acceptSuggestionOnEnter": "true", // My personal habit is that when I press enter, it must be a real line feed. Only tab will accept Intellisense
        // "editor.snippetSuggestions": "top", / / (optional) snippets are displayed at the top of the completion list. The default is inline
    
        "code-runner.runInTerminal": true, // Set to false to output in output, unable to input
        "code-runner.executorMap": {
            "c": "cd $dir && gcc '$fileName' -o '$fileNameWithoutExt.exe' -Wall -g -O2 -static-libgcc -std=c11 -fexec-charset=GBK && &'$dir$fileNameWithoutExt'",
            "cpp": "cd $dir && g++ '$fileName' -o '$fileNameWithoutExt.exe' -Wall -g -O2 -static-libgcc -std=c++17 -fexec-charset=GBK && &'$dir$fileNameWithoutExt'"
            // "c": "cd $dir && gcc $fileName -o $fileNameWithoutExt.exe -Wall -g -O2 -static-libgcc -std=c11 -fexec-charset=GBK && $dir$fileNameWithoutExt",
            // "cpp": "cd $dir && g++ $fileName -o $fileNameWithoutExt.exe -Wall -g -O2 -static-libgcc -std=c++17 -fexec-charset=GBK && $dir$fileNameWithoutExt"
        }, // The command that runs when you right-click run code; the uncommented command is only applicable to PowerShell (Win10 default), and can be compiled and run if there is a space in the filename; the commented command is applicable to cmd (win7 default), PS and bash can also be used, but cannot be run if there is a space in the filename
        "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 false if frequent data input is required
        "code-runner.clearPreviousOutput": false, // Clear the terminal messages belonging to code runner before each run code. The default is false
        "code-runner.ignoreSelection": true,   // The default value is false. The effect is that you can select a piece of code with the mouse and execute it separately, but C is a compiled language, which is not suitable for this purpose
    
        "C_Cpp.clang_format_sortIncludes": true, // Adjust the order of include when formatting (alphabetical)
    }

    3. test

  • 2.F5 test

For more information, please refer to https://www.zhihu.com/question/30315894/answer/154979413

Published 16 original articles, won praise and 10000 visitors+
Private letter follow

Topics: JSON shell Windows Linux