How to write and run C language program tutorial in VS Code

Catalog of this article

foreword

1. Download and install VS Code

2. Install 2 plugins in VS code

3. Download minGW64

4. Configure system environment variables 

5. C language configuration

6. Write a test program

7. Possible problems

Summarize

foreword

After tossing for a long time, I finally succeeded in writing C language programs in VS Code, so I posted an article to share my experience.

To run C language programs in VScode, you first need to install VS Code, and install 2 C language plug-ins in VSCode; secondly (win10 system) download winGW6, and configure environment variables, and finally set C language configuration files.


1. Download and install VS Code

For this step, please refer to  "Download, Installation and Configuration of Assembly Language VS Code Editor" .

2. Install 2 plugins in VS code

(1) "C/C++ Extension Pack" plug-in

Search in the search box of VS Code, and install this plugin. This plugin is responsible for supporting C language programs.

2. "code runner" plugin 

After this is installed, there is a triangle symbol in the upper right corner of VS Code, which can run the code. 

3. Download minGW64

I checked this minGW64 for a long time and still didn't fully understand what it is. I only know that it is an open source plug-in that is necessary for running C language programs and is suitable for windows systems. This plug-in is transplanted from the GCC compiler in the LINUX system to The product in the windows system.

 The interface of the minGW64 download address  is shown in the figure.

Wrong way : click the big green Download, the downloaded compressed package is about 16MB. It takes several minutes to unzip, and then I get a bunch of files I don't know how to use, and there is no exe file.

The correct way : pull down the page, in the "Files" tab, select the file package according to the parameters, and download it (after downloading, it will be in 7z format).

The parameter selection method is shown in the figure.

4. Configure system environment variables 

The purpose of this step is to store the address of minGW64 in the specified directory of the system, so that the system can know where to find the tool in this minGW64 and run it.

(1) Copy address

Copy the address of the bin directory in minGW64, like "c:\***\bin".

(2) Find the environment variable

Search for "environment variables" in the windows search box to find the window for configuring environment variables.

 (3) Modify environment variables

Find Path in the system variable, edit, create new, paste the address you just copied, and confirm.

(4) Check whether the configuration is successful

Enter gcc -v on the command line and press Enter. If it is prompted that this is an illegal command, it means that the configuration has failed, and if there is a long string of characters (as shown in the figure below), it means that the configuration is successful.

5. C language configuration

Two more configuration files are required to run the program in C language.

(1) Specify a working folder

Open a folder in VSCode's explorer as the working folder.

Create a folder named .vscode under this folder, pay attention to a point! If it already exists, there is no need to create it.

(2) Create a new launch.json file

Create 2 new files under this .vscode folder as configuration files.

The first is the launch.json file, enter the following content.

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch", 
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/${fileBasenameNoExtension}.exe",
            "args": [], 
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true, 
            "MIMode": "gdb",
            "miDebuggerPath": "C:\\mingw64\\bin\\gdb.exe",

    //**********上面这行,要修改为你的编译器所在的路径,形如 c:\\***\\bin\\gdb.exe

            "preLaunchTask": "gc", //这里注意一下这个名字一会儿还要用到
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": false
                }
            ]
        }
    ]
}

(3) Create a new tasks.json file

Still in the .vscode folder, create a new tasks.json file and enter the following content.

{
    "version": "2.0.0",
    "command": "gc",  //这里的名字 注意一下
    "args": [
        "-g",
        "${file}",
        "-o",
        "${fileBasenameNoExtension}.exe"
    ], // 编译命令参数
    "problemMatcher": {
        "owner": "cpp",
        "fileLocation": [
            "relative",
            "${workspaceFolder}"
        ],
        "pattern": {
            "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
            "file": 1,
            "line": 2,
            "column": 3,
            "severity": 4,
            "message": 5
        }
    }
}

Note that the parameters in it should be consistent "command"with those in "launch.json" "preLaunchTask".

Note that the system will automatically generate a c_cpp_properties.json file under .vscode.

6. Write a test program

Write a simple Hello World program.

#include<stdio.h>
int main(void){
    printf("Hello World!");
    return 0;
}

Right-click in the code area-run code, and then you can see the result in the terminal at the bottom.

7. Possible problems

One problem I encountered was that when I finished writing the first line #include<stdio.h>, I was prompted that include Path was missing.

My solution is: uninstall the "code runner" plugin of VS code, then restart VS code, install "code runner" again, and it's done.


Summarize

This article introduces how to use VS Code to write C language programs in 64-bit win10 system, including minGW64 download, VS Code plug-in installation, setting C language configuration files and other operations.

Guess you like

Origin blog.csdn.net/Dr_Cheeze/article/details/127964797