Explain
VSCode is an editor. By default, it does not have a compiler and debugging environment. It needs to install the compiler by itself. Debugging can be done with its plug-in.
The compiler suite is based on TDM-GCC, which is easy to use. (Codeblocks, Dev-Cpp and other default bands are this one). Paths are automatically added to the environment variable PATH when installed.
download
- VS Code : https://code.visualstudio.com/Download // zip is a green version
- TDM-GCC : http://tdm-gcc.tdragon.net/download // Select the corresponding system
To configure
- Open the power shell and enter gcc to check whether the installation of gcc is successful.
- Open VS Code, point extension, and install all the plug-ins and themes you need.
- Build a working directory in pure English (VS Code has poor support for Chinese). Open this directory in the resource manager of VSCode. Press F5, it will automatically create launch.json file in the. vscode directory, edit and save:
{ "version": "0.2.0", "configurations": [{ "name": "C Launch (GDB)", // Configuration name, which will be displayed in the drop-down menu of boot configuration "type": "cppdbg", // Configuration type, cppdbg only "request": "launch", // Request configuration type, which can be launch (start) or attach (attach) "launchOptionType": "Local", // Debugger startup type, Local only "targetArchitecture": "x86", // Generate target architecture, generally x86 or x64, for x86, arm, arm64, mips, x64, amd64, x86_64 "program": "${cwd}\\${fileBasenameNoExtension}.exe", // The path of the program to be debugged "miDebuggerPath": "gdb32.exe", // miDebugger's path, notice that it corresponds to MinGw's path here. gdb64.exe for 64-bit systems "args": [""], // Command-line parameters passed to the program during debugging are usually set to null. "stopAtEntry": false, // When set to true, the program will pause at the program entry. I usually set it to true. "cwd": "${workspaceRoot}", // The working directory for debugging programs is generally ${workspace Root}, which is the directory where the code resides. "externalConsole": true, // Whether the console window is displayed during debugging is generally set to true display console "preLaunchTask": "gcc" // The tasks performed before the debugging session starts are generally compilers, c++ is g++, c is gcc. }] }
- Create a new tasks.json file in the. vscode directory:
{ "version": "0.1.0", "command": "gcc", // Compiling C++ with g++, self-modifying with MinGW and clang. "args": ["${file}", "-o", "${fileBasenameNoExtension}.exe", "-g3", "-Og", "-Wall", "-static-libgcc", "-std=c11", "-fexec-charset=GBK", "-finput-charset=UTF-8"], // Compile command parameters, C++ available - std=c++14 "showOutput": "always", "problemMatcher": { "owner": "c", "fileLocation": ["relative", "${workspaceRoot}"], "pattern": { "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$", "file": 1, "line": 2, "column": 3, "severity": 4, "message": 5 } } }
- Create a new settings.json file in the. vscode directory:
// Place settings in this file to override default values and user settings. { "files.defaultLanguage": "c", "code-runner.runInTerminal": false, "code-runner.executorMap": { "c": "cd $dir && gcc $fileName -o $fileNameWithoutExt -Wall -g3 -Og -static-libgcc -std=c11 && $dir$fileNameWithoutExt", "cpp": "cd $dir && g++ $fileName -o $fileNameWithoutExt -Wall -g3 -Og -static-libgcc -std=c++14 && $dir$fileNameWithoutExt" } }
- Create a new c_cpp_properties.json file in the. vscode directory:
// Catalogue modification according to actual situation { "configurations": [ { "name": "Win32", "includePath": [ "${workspaceRoot}", "C:/TDM-GCC-32/include", "C:/TDM-GCC-32/lib/gcc/mingw32/5.1.0/include" ], "browse": { "limitSymbolsToIncludedHeaders": true, "databaseFilename": "", "path": [ "C:/TDM-GCC-32/include", "C:/TDM-GCC-32/lib/gcc/mingw32/5.1.0/include" ] } } ], "clang_format": { "style": "file", "fallback-style": "LLVM", "sort-includes": false } }
7. Create a new. c file in the current directory, test it in the debug panel, or press F5 directly.
Skill
- Because VS needs to be configured separately for each folder, it is recommended that the. vscode folder be placed at the top of your usual folder, so that there is no need to repeat the configuration. (There are generally four basic files: launch.json, tasks.json, settings.json, c_cpp_properties.json to support compiled output)
- Ctrl += or - (the left side of the backspace key) can be zoomed in and out, and then pull the windows, with the theme of the force is perfect!
- Although the environment variables are configured, the path of the includePath item still needs to be changed to the actual path of the installed TDM-GCC, which allows VSCode to recognize the header file of C/C++, otherwise it will make an error when it cannot be found.
Reference article: