“This is the second day of my participation in the November Gwen Challenge. See details of the event: The last Gwen Challenge 2021”.

VS Code preliminary

VS Code download address

code.visualstudio.com/

VS Code Installation tutorial

This article skips the VS Code installation tutorial. If you need to install a tutorial, please refer to @Chen Xiaosu

Necessary plug-ins for VS Code

  1. First, open the VSC to install the Chinese language package (this step can be ignored if Chinese is not required), as shown in the figure

2. Install the C++ extension package, as shown in the figure

GCC compiler installation

Download address

Provided hereMinGW w64 4.3.5After installation, the default decompression path of GCC isD:/GCC, if the reader’s path is different, please modify the relevant file appropriately.

Configuring environment Variables

You need to configure two environment variables: Path points to the compiler and debugger, and Include points to the header directory.

  • Path: D: \ GCC \ bin
  • Include: D: \ GCC \ Include

Test environment variables

Enter g++ -v on the cli. If the g++ version is displayed, the variable is configured successfully.

C++ file configuration

Assume the project is in a folder named Test, as shown in the figure

Create folder test/.vscode

Create two new files in the.vscode folder: launch.json and tasks.json. As follows:

Json (need to change the value of miDebuggerPath item to the directory of reader GCC)

{
    "version": "0.2.0"."configurations": [{"name": "C/C++"."type": "cppdbg"."request": "launch"."program": "${fileDirname}/${fileBasenameNoExtension}.exe"."args": []."stopAtEntry": false."cwd": "${workspaceFolder}"."environment": []."externalConsole": true."MIMode": "gdb"."miDebuggerPath": "D:/GCC/bin/gdb.exe"."preLaunchTask": "g++"."setupCommands": [{"description": "Enable pretty-printing for gdb"."text": "-enable-pretty-printing"."ignoreFailures": true}],},]}Copy the code

tasks.json

{
    "version": "2.0.0"."command": "g++"."args": [
        "-g"."${file}"."-o"."${fileDirname}/${fileBasenameNoExtension}.exe"]."problemMatcher": {
        "owner": "cpp"."fileLocation": [
            "relative"."${workspaceRoot}"]."pattern": {
            "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$"."file": 1."line": 2."column": 3."severity": 4."message": 5}},"group": {
        "kind": "build"."isDefault": true}}Copy the code

rendering

Debugging part

Create the source file test/test.cpp

  • Test. CPP can be edited directly with VSC, as shown below:

  • Test. CPP, here take the test content 99 times table as an example:
#include <stdio.h>
#include <stdlib.h>
int main(a){
	int a=1,b=1;
	for(a; a<=9; a++){for(b; b<=a; b++)printf("%d*%d=%2d	",b,a,a*b);
		printf("\n");
		b=1; }}Copy the code

Load the project configuration (that is, open the project folder)

Click here to load the project configuration and open the folder as Test

A successful loading flag

As shown, you can see the.vscode folder and source files

Add a breakpoint

Click before the line number to make a red dot appear

The test and operation

  • Click the button as shown or press F5 directly to run debugging

  • After a few moments, the debugging mode will be entered, and the debugging tool shown in the figure will pop up

Viewing debugging Information

As shown in the figure, the running state is on the left and the running interface is on the right.

Afterword.

Modify the debugging interface in Terminal

Json ==externalConsole== =false==

  • The debugging effect is as follows, as can be seen, the running interface is transferred to terminal: