Visual Studio Code configures the C environment

1. Open the Visual Studio Code interface

First install the C/C++ extension

Note: VS code does not support C language debugging and compilation in the Windows environment, so we also need to download WinGw of Tools such as gcc to provide support.

2. The next step is to download and install MinGw

(1) Official website download address: [https://sourceforge.net/projects/mingw/]

ClickDownLoad

Or click Download x86_64-8.1.0-release-posix-sjlj-rt_v6-rev0.7z (MinGW-w64 – for 32 and 64 bit Windows) (sourceforge.net)

(2) Unzip to the folder, I chose drive E

(Be careful here)

Enter the folder to find the bin file and copy the path: E:\x86_64-8.1.0-release-posix-sjlj-rt_v6-rev0\mingw64\bin

(This is my path)

(3) Configure environment variables

The first step is to open this computer, right-click, click Properties, find Advanced System Properties, click Advanced, find Environment Variables, and find the Path column in System Variables.

Also:

Configuration JSON file

Step 1: Create a folder under the disk and name it CWORKSPACE

Step 2: Create several files under the resource manager in VS Code. Since I have already created them here, I will display them directly.

(1) Create a new folder under the CWORKSPACE file and name it .vscode

(2) Create three json files under the .vscode folder.

Named:

1. c_cpp_properties.json

2. launch.json

3. tasks.json

(3) Modify the content in the Json file

1. c_cpp_properties.json

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceRoot}",
                "E:/x86_64-8.1.0-release-posix-sjlj-rt_v6-rev0/mingw64/include/**",
                "E:/x86_64-8.1.0-release-posix-sjlj-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++ ",
                "E:/x86_64-8.1.0-release-posix-sjlj-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64 -mingw32",
                "E:/x86_64-8.1.0-release-posix-sjlj-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/backward",
                "E:/x86_64-8.1.0-release-posix-sjlj-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include",
                "E:/x86_64-8.1.0-release-posix-sjlj-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include-fixed",
                "E:/x86_64-8.1.0-release-posix-sjlj-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../ ../x86_64-w64-mingw32/include"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "__GNUC__=6",
                "__cdecl=__attribute__((__cdecl__))"
            ],
            "intelliSenseMode": "msvc-x64",
            "browse": {
                "limitSymbolsToIncludedHeaders": true,
                "databaseFilename": "",
                "path": [
                    "${workspaceRoot}",
                    "E:/x86_64-8.1.0-release-posix-sjlj-rt_v6-rev0/mingw64/include/**",
                    "E:/x86_64-8.1.0-release-posix-sjlj-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++ ",
                    "E:/x86_64-8.1.0-release-posix-sjlj-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64 -mingw32",
                    "E:/x86_64-8.1.0-release-posix-sjlj-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/backward",
                    "E:/x86_64-8.1.0-release-posix-sjlj-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include",
                    "E:/x86_64-8.1.0-release-posix-sjlj-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include-fixed",
                    "E:/x86_64-8.1.0-release-posix-sjlj-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../ ../x86_64-w64-mingw32/include"
                ]
            }
        }
    ],
    "version": 4
}

Replace the E:/x86_64-8.1.0-release-posix-sjlj-rt_v6-rev0/mingw64/bin path with yours

2. launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(Windows) Launch",
            "type": "cppvsdbg",
            "request": "launch",
            "program": "cmd",
            "preLaunchTask": "echo",
            "args": [
                "/C",
                "${fileDirname}\${fileBasenameNoExtension}.exe",
                " & amp;",
                "echo.",
                " & amp;",
                "pause"
            ],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "console":"newExternalWindow"
        },
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "E:\x86_64-8.1.0-release-posix-sjlj-rt_v6-rev0\mingw64\bin\gdb.exe", // gdb of your own computer
            "preLaunchTask": "echo",//This corresponds to the label of task.json
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
 
        }
    ]
}

Also modify the E:/x86_64-8.1.0-release-posix-sjlj-rt_v6-rev0/mingw64/bin path here and replace it with your path

3. tasks.json

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "echo",
            "type": "shell",
            "command": "gcc",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileBasenameNoExtension}.exe",
                "-fexec-charset=GBK"//Solve Chinese garbled characters
            ]
        }
    ],
    "presentation": {
        "echo": true,
        "reveal": "always",
        "focus": false,
        "panel": "new",
        "showReuseMessage": true,
        "clear": false
    }
}

4) Hello world program running C program

#include<stdio.h>
int main(){
    printf("hello world");
    return 0;
}

Also:

Expand the next thing inside

Then check the two options below

OK, so far, you have completed configuring the C language environment of VS Code. Hurry up and have fun trying it!

(After the configuration is completed, don’t forget to restart the computer)