VS Code configures the C++ runtime environment, just read this article

VS Code configures the C++ execution environment

crap written in front

If you want to do a good job, you must first sharpen your tools. For C++ beginners, it is particularly important to choose a code editing and debugging tool that is handy, because beginners often only need to debug a few cpp files separately, rather than developing a large project. Large-scale integrated IDEs like Visual Studio that cost dozens of gigabytes are very bloated. At this time, some people will think of the strongest code editor——Visual Studio Code. VS Code is a lightweight code editing and debugging tool. It will integrate so many functions like a large IDE. Its functions completely depend on the needs of users, and the resource usage is very low. Although some settings need to be manually configured to use, but believe me, this is for you who have the courage to learn C++ Said a piece of cake! let's start.

start of text

1. Install the C++ runtime environment

According to my habit, I will go to the official website to download various software, but this time I stepped on a big hole, the official website address www.mingw.orgwill jump to other pages many times (2022.04.29)
, so this time I choose a third-party download sitehttps://sourceforge.net/projects/mingw/

① Click Install to start the installation
Please add a picture description
② Select an installation location, then click Continue to continue
Please add a picture description
③ Wait
Please add a picture description
④ There are two items in the Mark icon, which support the compilation of C and C++ programs respectively ⑤ Click Installation
Please add a picture description in the upper left menu bar and select Apply Changes ⑥ Click Apply to start downloading the component package ⑦ The speed is slow, wait patiently
Please add a picture description
Please add a picture description

In this step, there may be an error of failing to obtain the component package. This is because the download server is abroad. The solution is as follows: 1.
Hang the ladder, the speed will be much faster
2. After the end, go back to the above step ⑤ and click Apply again . The program will automatically download missing packages

Please add a picture description⑧ After completion, you can close the installer
Please add a picture description⑨ Find from the settings or search for environment variable settings globally
Please add a picture description
⑩ Click Environment Variables ⑪ Find the Path
Please add a picture description
variable in System Variables , select it and click Edit to enter the editor ⑫ Click New and enter the MinGW installation directory folder 's
Please add a picture description
binComplete address, and then click OK to close all windows

Note here that if the first item of the environment variable on your computer adopts the reference format, that is, for example %JAVA-HOME%, it may not display the following editing interface, but display all the environment variables in one line. At this time, you only need Fill in the address at the top, and thenseparated by a semicoloncan

Please add a picture description
⑬ Open the terminal on the computer (or Windows logo key + R to enter cmd to open the console) and enter g++ --versionand gdb --versioncheck whether the configuration is successful
Please add a picture description

2. VS Code installs C++ extension

After VS Code is downloaded and installed, it has no function, and it can only be used as a text editor. Almost all of its functions come from the extension store, that is to say, users can choose the corresponding extension according to their own needs, so as to customize the suitable Own VS Code.
If you want to use the Chinese version, just search for Chinese in the extension store and install the Chinese extension

① Open the extension , search for C++ to find C/C++ Extension Pack and click Install . This is an official extension package, which contains extensions necessary for C++ debugging and some commonly used tools.
Please add a picture description

3. Project debugging configuration

① Open the resource , select and open a folder as your working directory
Please add a picture descriptionPlease add a picture descriptionCreate , name it .vscode, this folder stores the configuration files required for project debugging, for C++ program debugging For , two configuration files are required, named launch.jsonand tasks.jsonRight-click .vscodethe folder and create these two files in it.I will put the detailed description of these two files in the appendix, just follow me now

① Note that the two configuration files here must be created in .vscodethe folder , otherwise they cannot be read.
② Right-click .vscodethe folder instead of right-clicking on a blank space (of course, you can also create it elsewhere and drag it .vscodeinto the folder)
③The .vscodefolder is used to store the project debugging configuration file, and its location is The root directory of the project, every project has this folder and contains at least launch.jsonthe file

Please add a picture description③ Copy the code below to the configuration file created in the previous step

launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "C/C++: Build and Debug Active File",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: Build active file"
        }
    ]
}

tasks.json

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: Build active file",
            "command": "C:\\MinGW\\bin\\g++.exe",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "编译生成可执行程序"
        }
    ],
    "version": "2.0.0"
}

Notice
1. If your MinGW installation location is the same as the default installation location I use, then the above two files do not need to be changed. If not, you need to modify the following two places: ① Change the address of the field in to
your gdb .exe address② Change the address of the field in to your g++.exe address 2. After checking, you can copy these two files to a place for saving, and you can directly paste these two files when creating a new project in the futurelaunch.json"miDebuggerPath"
tasks.json"command"

4. Write & debug C++ programs

① After the previous step, you can write a C++ program. Create test.cppa file , write its content, press F5 to debug, and you can find that the program automatically generates an test.exeexecutable file and runs successfully.
Please add a picture descriptioncode show as below

#include <iostream>
using namespace std;

int main()
{
    
    
    cout << "Hello World" << endl;
    cin.get();
}

5. Conclusion

The previous process seems a bit cumbersome because I wrote it in relative detail, but it is actually very simple. There are only three steps in summary:

  1. Install the C++ runtime environment MinGW
  2. Download the C++ extension
  3. Copy launch.json, tasks.jsontwo configuration files to the project root directory

After completing these three steps, you have obtained a lightweight code debugging tool. I hope that VS Code can accompany you through the learning journey of C++

appendix

1. Detailed configuration file

launch.json

Field name: The name of the startup configuration, that is, the name displayed in the drop-down menu of the debug configuration. If multiple configurations are added, this can be used as a distinction. Field
: programThe full path of the executable file.

① Since the C++ program needs to be compiled into an executable program to run, here is the full path of the file.
${...}Among the path reference symbol of VS Code, ${fileDirname}is the project working directory, ${fileBasenameNoExtension}and is the file name without extension.
Combine them ${fileDirname}\\${fileBasenameNoExtension}.exeto indicate .exethe path of the file named after the current file name, which is the project startup path.

Field cwd: The working directory of the cpp file
Field : The path of the C++ debugger gdb.exemiDebuggerPath you installed Field : The name of the preprocessing process before starting debugging (that is, the process of generating executable program files )
preLaunchTask.exe

① We need to know that the C++ program debugging process is divided into compiling and running, launch.jsonbut configuration at startup (running). Before that, it needs to be compiled to generate an executable file, and tasks.jsonthe configuration file is the configuration at compile time.
②The preLaunchTaskcontent of the field is the task name of the compilation process. VS Code finds the corresponding taskscompilation and runs it, so the content of this field must be consistent with the fieldtasks.json in the file , otherwise it will not be able to find the compilation configuration, that is, labelUnable to generate .exe executable.

tasks.json

Field label: The task name of the compilation process, which launch.jsonneeds preLaunchTaskto be consistent with the field in
Field : The path of commandthe C++ compiler g++.exe installed on your computer Field : The optional parameters of the g++.exe compilation process
args

① The line "-g"below "${file}"indicates that the file to be compiled is the current debugging file.
② The line "-o"below "${fileDirname}\\${fileBasenameNoExtension}.exe"indicates the location of the generated executable file, which generally needs to launch.jsonbe programconsistent with the field of .

If you don't want the generated executable file to be placed together with the source code file, but to be placed in another folder (this folder must exist), you can set it like this: Change the line below the parameter in the field in
to In the field of your custom folder path also modify to your custom folder path so that when you debug the C++ program, the executable file ( .exe ) will be generated under the path you set instead of source files ( .cpp ) mixed uptasks.jsonargs"-o"${fileDirname}
launch.jsonprogram${fileDirname}

2. Other information

VS Code official tutorial link: https://code.visualstudio.com/docs/cpp/config-mingw

Guess you like

Origin blog.csdn.net/NEKOic/article/details/124502138