preface

A few days ago, my student asked me how to use VScode to build C language environment, just before I also want to use VScode to build C++ environment (personally feel VC6 and Dev C++ are not very convenient to use, Dev C++ debugging function is not very good, and the interface is not as good-looking as VScode. As for VC6, I can’t make fun of it anymore, but the school uses it and calls it a classic (although VC6 is a classic HHHH), hence this article.

Dev C++ as a C/C++ development environment is also a very good choice (especially for students who have just started to learn C language, pay attention not to install in a Directory with Chinese). Attach the Dev C++ interface:

  • Light-colored examples :(feels refreshing)

  • Dark color examples:

  • Dev’s website: c + + sourceforge.net/projects/or…

After that, let’s start building C and C++ environments.

A brief introduction to VScode

VScode is a popular open source text editor from Microsoft. Compared to other editors, VScode has many advantages, such as cool themes, beautiful appearance, integration of terminals, lightweight size, and so on. The best thing about VScode is that it has a mature plug-in market (which we’ll use in a moment) that meets the various development needs of developers. Thanks to this plug-in market, we can make VScode into a variety of development environments.

VScode is easy to download, go to the official website to download and install.

MinGW briefly introduces and downloads configuration

One thing to note is that VScode is just a text editor, essentially the same thing as Windows notepad, it’s not a compiler, it’s just an editor, and editors don’t compile and run programs, that’s what compilers do. So in order for us to compile and run C/CPP files in VScode, we need to install an additional C and C++ compiler: GCC. The port of GCC for Windows is MinGW. We use MinGW to compile our C/C++ files using GCC.

MinGW download address: sourceforge.net/projects/mi…

Once inside, select Files:

Download MinGW zip:

Download process as a result of downloading from the Internet, will be slower, you can try a few more times or hang agent, in addition according to the author’s experience to see the Internet of things generally downloaded at noon will be faster.

After downloading it, unpack it in a directory without Chinese and Spaces and configure the environment variables.

Win10 标 签 : MinGW 标 签 : MinGW 标 签 : MinGW 标 签 : MinGW 标 签 : MinGW 标 签 : MinGW 标 签 : MinGW 标 签 : MinGW 标 签 : MinGW 标 签 : MinGW 标 签 : MinGW 标 签 : MinGW 标 签 D: development\MinGW\bin), and then make sure along the way.

Now that the environment variable is configured, we can verify this by opening CMD in any path: GCC –version:

Next we configure VScode

VScode configuration

Open VScode, then open the plugins market on the left, and download and install two plugins:

  • Chinese: Chinese Chinese plug-in, VScode is the default English interface, for the convenience of our to change him to Chinese. Search for Chinese in the search bar, select Simplified Chinese and click install in the lower right corner (I have installed it here so there is no install button).
  • C/C++ : a code plug-in for C/C++ syntax highlighting. The installation mode is the same as Chinese.

After that, let’s write a C file to test it. Click on the upper-left corner of the file -> Open Folder -> select an empty folder to open, and right click -> New File:

Give it a name (hello.c) and write some random code, like the classic Hello World:

#include<stdio.h>

int main(void){
    printf("Hello World!");
    return 0;
}
Copy the code

Then we compile it, go to the top terminal -> Configure the default build task (you can also use the shortcut CTRL + Shift + B) and you will see this option:

This lets us choose what to use to edit the file. Since we are writing C files, we should use a compiler that can compile C files (i.e. GCC in MinGW). We click the C/C++ editor here. Then you’ll notice that we’ve generated a Hello.exe file and a.vscode folder.

Open a terminal (terminal –> new terminal), type.\ hello.exe to run the file, and you will find that the terminal successfully prints HelloWorld, which means a successful run.

As for the.vscode folder, these are configuration files in the current folder (task.json here, which contains information about what compiler to use, compiler path, etc.).

(note: the following task. Json configuration steps from zhihu, the original links: zhuanlan.zhihu.com/p/147366852…

{
    "version": "2.0.0"."tasks": [{// Inside the braces is the 'build' task
            "label": "build".// The task name can be changed, but it is not recommended
            "type": "shell".// Task type, process is the VSC to parse the predefined variables and escape directly to command; Shell is equivalent to opening the shell and then typing the command, so arGS is parsed through the shell again
            "command": "gcc".// compile command, in this case GCC, to compile c++ with g++
            "args": [    // In square brackets are a set of arguments passed to the GCC command to implement some function
                "${file}".// Specify the current file to compile
                "-o".// Specify the path and name of the output file
                "${fileDirname}\\bin\\${fileBasenameNoExtension}.exe".// Take the -o from the previous step and make the executable output to the bin folder in the same folder as the source file
                "-g".// Generate and debug related information
                //" -wall ", // enable extra warning
                //"-static-libgcc", // static link libgcc
                "-fexec-charset=GBK".// The generated program uses GBK encoding. If this item is not added, Chinese characters will be output in Win
                "-std=c11".// language standard, can be modified according to their own needs, write c++ to c++ language standard, such as c++11]."group": {  // Group means' group ', we can have many tasks and put them into a 'group'
                "kind": "build".// Indicates that this set of task types is build
                "isDefault": true// Indicates that this task is the default task in the current group of tasks
            },
            "presentation": { // Some additional Settings for performing this task
                "echo": true.// Indicates that there should be output at the terminal during the execution of the task
                "reveal": "always".// Whether to switch to the terminal panel during task execution. The value can be always, silent, or never
                "focus": false.// True enables task execution to focus on the terminal, but it does not make sense for compilation because input is only involved at runtime
                "panel": "new" // Create a new terminal panel each time the task is executed. You can also set it to shared, which will cause the task to be reused by the terminal
            },
            "problemMatcher": "$gcc" // capture the compiler error message displayed in the terminal at compile time and display it in vscode's 'problems' panel
        },
        {// Inside the braces is the 'run' task, with some of the same Settings as the build task above
            "label": "run"."type": "shell"."dependsOn": "build".// Task dependent, since the build task must be executed before executing this task,
            "command": "${fileDirname}\\bin\\${fileBasenameNoExtension}.exe".// Execute the exe file, just specify where the exe file is located
            "group": {
                "kind": "test".// This group is the 'test' group, put the run task in the test group so that we can use the shortcut keys to execute
                "isDefault": true
            },
            "presentation": {
                "echo": true."reveal": "always"."focus": true.// Set this to true to focus on the terminal after running the task for easy input
                "panel": "new"}}}]Copy the code

The debugger

Next we say debug, VScode debug shortcut is F5, press F5, the debugger selection screen will appear:

If you select GDB, then GCC, you’ll notice that the terminal is redirected to the debug console, and there is a launch.json file under the.vscode folder, which contains configuration files from the current working folder. For example, task.json is a configuration file for compiling and executing tasks. The launch.json file is a configuration file for debugging.

(note: the following the launch of the json configuration steps from zhihu, the original links: zhuanlan.zhihu.com/p/147366852…

{
    "version": "0.2.0"."configurations": [{// Inside the braces is our Debug configuration
            "name": "Debug".// Configure the name
            "type": "cppdbg".// Configuration type. CPPDBG corresponds to the debugging function provided by CPPTools. You can assume that this can only be CPPDBG
            "request": "launch".// The request configuration type can be launch or attach.
            "program": "${fileDirname}\\bin\\${fileBasenameNoExtension}.exe".// The path of the program to be debugged
            "args": [].// The command line argument passed to the program during debugging
            "stopAtEntry": false.// If set to true, the program will pause at the program entrance, equivalent to breaking on main
            "cwd": "${fileDirname}".// This is the directory where the source files are located
            "environment": [].// The environment variable is null
            "externalConsole": false.// Use a separate CMD window with a small black box when true; If set to false, vscode's built-in terminal is used
            "internalConsoleOptions": "neverOpen".// If neverOpen is not set, the debug console TAB will be displayed during debugging, which is not used by novice debugging
            "MIMode": "gdb".// Specify the connection debugger. GDB is the debugger in minGW
            "miDebuggerPath": "C:\\Program Files\\mingw64\\bin\\gdb.exe".// Specify the path where the debugger is located. If your minGW is installed elsewhere, change it to your own path. Note the interval is \\
            "preLaunchTask": "build" // Tasks performed before debugging. We compile the build before debugging. This should correspond to the label of tasks.json with the same name}}]Copy the code

To try to debug the program, press F9 to set a breakpoint on the current line. You can also change the “stopAtEntry”: false to true in the launch.json file. This will stop the program at the start of the main function.

Once from left to right:

  • Continue (F5)
  • Single step skip (F10)
  • Single step debugging (F11)
  • Step out (Shift +F11)
  • Reset (Shift + CTRL +F5)
  • Stop (Shift +F5)

You can also monitor variable changes in the variables area on the left. These are basic debugging functions that are not covered here.

conclusion

So far we’ve built VScode as a C and C++ development environment and tried compiling, running and debugging. You can ditch the clunky VC6 and inconvenient Dev C++ debugging and instead write and debug C/C++ programs using VScode. Have fun with it!