Compiling C/C with VSCode under Windows++

Posted by kat_jumper_33 on Sat, 22 Jan 2022 10:01:54 +0100

0. Preface

VSCode (Visual Studio Code) is a lightweight and powerful editor. Users can install a variety of plug-ins to make it easier and more personalized. Although VSCode is an editor, it only needs simple configuration to enable VSCode to realize the function of compiler and program breakpoint debugging.

1. Download MinGW-w64

1.1 official website download:

        MinGW-w64 - for 32 and 64 bit Windows - Browse /mingw-w64 at SourceForge.net   

1.2 online disk download: generally, the official website download is not satisfactory for some reasons. Here is the version I downloaded:

        https://pan.baidu.com/s/1V3yl3q6E-ixpUC_ZDy6ZQw (code qxrs)

2. Install MinGW-w64 and add environment variables

2.1 the contents of mingw-w64 after installation are as follows:

     

2.2 add MinGW-w64 to the system environment variable. For the operation of adding system environment variables under Windows10, see the following article, in which you want to add the bin folder under the MinGW-w64 installation directory.

        Method of adding system environment variable under Windos10

Verify whether the addition is successful: press win+R to run cmd, enter gcc -v and press enter. If the information shown in the figure below appears, it indicates that the addition is successful. Otherwise, the addition fails and needs to be added again.

3. Install the necessary plug-ins in vscode

3.1 search and install the following plug-ins in the plug-in center of VSCode:

  •                 C/C++
  •                 Code Runner

3.2 after successful installation, restart the VSCode software, and you can see a small triangle in the upper left corner of the software. Click this small triangle to run the program.

3.3 in case of problems, it is necessary to set the program running result to the integrated console of VSCode.

Click file - > Preferences - > settings to enter the settings, search "Run In Terminal", find the Run In Terminal setting option under code runner, and check it.

4. Realize program debugging function

4.1 first, create a folder dedicated to storing vscode files in the future, open this folder in vscode EXPLORER, and create a directory named ". vscode" under this folder [the dot in front of the name] It must not be missed].

4.2 create two new files in ". vscode" directory JSON files, respectively launch JSON and tasks json. Corresponding to the following codes to two JSON file (be careful not to copy it wrong)

Note:

  • "miDebuggerPat" should be set to the location of MinGW -w64. You need to modify it according to your installation path.
  • "External console" corresponds to the selection for setting the console to run code. Set to true to use the external small window, and false to use the console provided by VSCode. You can set it according to your needs.
  • The configured debugging environment will only be used for the current The file in the path of the vscode folder takes effect. If you want to debug the code of another folder, you need to create a new file in the corresponding file directory Vscode folder and contains launch JSON and tasks json.
//launch.json
{
        "version": "0.2.0",
        "configurations": [
            {
                "name": "C/C++",
                "type": "cppdbg",
                "request": "launch",
                "program": "${fileDirname}/${fileBasenameNoExtension}.exe",
                "args": [],
                "stopAtEntry": false,
                "cwd": "${workspaceFolder}",
                "environment": [],
                "externalConsole": false, //true is run in esternal console; false is run in local console.
                "MIMode": "gdb",
                "miDebuggerPath": "D:/Software/mingw64/bin/gdb.exe",
                "preLaunchTask": "g++",
                "setupCommands": [
                    {
                        "description": "Enable pretty-printing for gdb",
                        "text": "-enable-pretty-printing",
                        "ignoreFailures": true
                    }
                ],
            },
        ]
    }
//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
        }
    }

5. Commissioning procedure

So far, the function of VSCode compiling C/C + + program has been realized. Use the shortcut key F5 or click Run - > start debugging to call up the program debugging function.

Topics: C Visual Studio Code compiler