The picture and text course of configuring C language environment on vscode

Time:2020-11-17

Cheng Cheng recently used C language. Before, he preferred to use Python debugging on vscode. Today, I refer to some articles and try to configure C language debugging on vscode, which contains some basic knowledge.

1. Download the official C / C + + plug-ins in vscode

插件 

2. To install the C / C + + compiler:
Install with the help of MinGW tool on windows, download address:[ https://sourceforge.net/projects/mingw/ ]

MinGW

Just install it in D: MinGW (create a new folder by yourself), and then open it as follows:

这里写图片描述

Find mingw32 in it- gcc.bin , mingw32-gcc-g + +. Bin, and mingw32- gdb.bin The first is a compiler for C language files, the second is for C + +, and the third is for debugging compiled files.
Then click installation > apply all changes

Then install the compiler, set the environment variables, so that the system can automatically find the GCC command. On my computer, right-click, properties, advanced system settings, environment variables, and find path in the system variable. This path is the same as that in Linux. In the command-line interface, the system searches for commands according to the path stored in the path variable. We added the folder of the binary executable file of MinGW.

环境变量

At this time, when we open the command line and enter GCC, we should see that the command has been recognized, and there will be errors such as insufficient parameters
If you have a. C file, you can enter the code at the command line and compile the code:
For example, I have a C file:


#include <stdio.h>

int main()
{
 int n;
 printf("This is a test file!\r\nInput a number: ");
 scanf("%d",&n);
 printf("The number is %d",n);
 return 0;
}

Then enter the command line in the directory where the file is located


gcc -g test.c -o test.exe

GCC is the compile command, which we just installed. The – g parameter indicates that the compiled object can only be run and cannot be debugged. The – O parameter indicates that the output file is specified, followed by the name of the output file. This instruction can compile the C file into an executable binary file.
You can then run it from the command line:


./test.exe

As shown in the figure below:

这里写图片描述

We use vscode to compile automatically. In fact, all ides are based on this principle.
Question: what happens if the system environment variables are not configured?

3. Configure vscode
Open vscode and load the C file just now. Netizens on the Internet say that compiling C file needs to open the folder where the file is located. I tried it, and it is. As shown in the figure below:

这里写图片描述

Then click debug, and he’ll jump out launch.json A configuration file for:
It’s good to revise these places

这里写图片描述 

Program indicates the file to be run. The format of $() indicates that this is a variable, and vscode adds it by itself. According to English, we can know the meaning of this variable. Midebuggerpath is the path to install GDB. As mentioned above, it is a binary file used for debugging (executable binary file is also called command). Pre lauchtask is a key point (knocking on the blackboard). Our configuration means debugging with GDB The binary file specified by the program parameter, but what if there is no such file? Combined with the previous examples, we know that what we can debug and run is the binary generated by GCC compilation test.exe File, but if there is no binary file, should we compile one with GCC first? This is what prelaunchtask does!

generate tasks.json You can use the task in the menu, then configure the task, and then select other. Several necessary configuration items are as follows:

这里写图片描述

Version doesn’t matter, skip. Command and args are actually spelled into the command in front of us:


gcc -g xxx.c -o xxx.exe

This will automatically generate the EXE file we want. Then let’s take a look at the previous configuration file

这里写图片描述 

If we compile the test. C file, then ${file} is test. C. if we execute prelaunchtask first, it is equivalent to executing


gcc -g test.c -o test.exe

Thus, the test.exe file
And then you have to use GDB to debug ${filebasenamenoextension}. Exe test.exe The configuration here should be consistent with the output file name just now, which is equivalent to running:


gdb test.exe

This starts debugging.
Finally, the debugging in vscode is as follows:

这里写图片描述

summary

This article on the configuration of vscode C language environment of the text tutorial on this, more related configuration of vscode C language environment content, please search the previous articles of developeppaer or continue to browse the related articles below, I hope you can support developeppaer more in the future!

Recommended Today

Kafka Study Notes (2): a preliminary study of Kafka

After reading the last article, I believe you have a preliminary understanding of the message system and the overall composition of Kafka. The best way to learn something is to use it. Today, let’s take a look at Kafka and finish our first work. The history of message in Kafka Although we need to master […]