Talking about VScode configuration C++ compilation environment


I have been using VS to complete the code editing and debugging work under the recommendation of the senior in the laboratory before, but learned that for the function of document compilation, VS only supports multi-file compilation under Windows, and cannot complete the work under the Urbanu system. Switch to learning the use of VScode.
In this article, the author will share some experience in the process of installing and debugging VScode and configuring the C++ compilation environment in the past few days. On the one hand, I hope to provide readers with some help, and on the other hand, I will help myself in the future configuration process and review the previous Trampled pit.

VScode download

Regarding the download of VScode, the author downloaded it directly from its official website, and then followed the normal download process to install it step by step.
VScode official website

MinGW installation

What is MinGW?
MinGW provides a simple and convenient GCC-based program development environment under Windows. MinGW collects a series of free Windows header files and library files; it also integrates GNU (http://www.gnu.org/) toolsets, especially GNU program development tools, such as classic gcc, g++, make Wait. MinGW is a completely free and free software. It simulates the development environment of GCC under Linux on the Windows platform and provides good basic support for cross-platform development of C++. It is provided for programmers working under Windows to be familiar with the C++ engineering organization under Linux. Condition.
Simple understanding, MinGW provides a C/C++ compilation environment for windows system.
Here is the difference between VS and VScode. VScode is actually a "text document". The VScode that has just been downloaded only has text editing functions, while VS has many functions such as text editing, code compilation, running and debugging, but corresponding , VS is also very large, so VScode is significantly better than VS in terms of convenience.
But this also means that if you want VScode to have a variety of functions, you must complete more configuration work.
The specific MinGW installation also involves configuring environment variables, etc. For specific operations, please refer to this article:

https://blog.csdn.net/qq_43041976/article/details/100542557

Note that the installation path of MinGW must be clear, whether it is configuring environment variables or configuring the compilation environment, you must use its address.
The author's installation location

What are environment variables

Environment variables generally refer to some parameters used in the operating system to specify the operating environment of the operating system, such as the location of the temporary folder and the location of the system folder.
An environment variable is an object with a specific name in the operating system, and it contains information that will be used by one or more applications. For example, the path environment variable in Windows and DOS operating systems. When the system is required to run a program without telling it the full path where the program is located, the system should search for the program in the current directory, and also go to the path specified in the path. . Users can better run the process by setting environment variables.
Here I thought of the need to add the called .h file to a specific location in the process of debugging STM32 and MK66 microcontrollers, so that the compiler can find the file, which may be similar to the configuration environment variables here.
Environment variable configuration location
The location of the environment variable is: this computer-properties-advanced system settings-advanced-environment variables-system variables-Path

C++ small project composition

Combined with the author's actual configuration experience, if you want to run a C++ program in VScode, you must at least have the following components:

  1. Project folder
  2. A folder named .vscode
  3. A cpp suffix file
  4. c_cpp_properties.json file
  5. launch.json file
  6. tasks.json file

The author completely used the new method to complete the creation of these files. After completion, the project structure shown in the following figure will appear in the explorer on the left side of the VScode interface:
C++ minimal project in VScode
In the establishment of the project, I first created a folder named VSCODE_TEST , A file named helloWorld.cpp is created in this folder, a folder named .vscode is created at the same time, and three json files are created in this .vscode folder.
The .exe file shown in the figure is a running file automatically generated by the project after successful compilation.

Code in cpp and json files

  1. cpp file The
    cpp file is the file where we write the main function. In this file, we only need to complete the most basic C++ code. This time I used to print "I love SLAM" to test whether the project is running normally.
#include <iostream>
using namespace std;

int main()
{
    
    

    cout<<"我爱SLAM"<<endl;
    
    system("pause");
    return 0;
}
  1. c_cpp_properties.json
    This file is one of the important files for compilation and operation. It involves the writing of multiple MinGW file addresses. The acquisition of these addresses is mentioned in the previous article. The first address needs to be modified according to the actual address, and the rest It can be directly generated by inputting instructions through the "Run" window.
{
    
    
    "configurations": [
        {
    
    
            "name": "Win32",
            "includePath": [
                "${workspaceRoot}",
                "E:/MinGW/mingw64/include/**",
                "E:/MinGW/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++",
                "E:/MinGW/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32",
                "E:/MinGW/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/backward",
                "E:/MinGW/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include",
                "E:/MinGW/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include-fixed",
                "E:/MinGW/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:/MinGW/mingw64/include/**",
                    "E:/MinGW/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++",
                    "E:/MinGW/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32",
                    "E:/MinGW/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/backward",
                    "E:/MinGW/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include",
                    "E:/MinGW/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include-fixed",
                    "E:/MinGW/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/include"
                ]
            }
        }
    ],
    "version": 4
}
  1. launch.json
{
    
    
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
    
    
            "name": "g++.exe - 生成和调试活动文件",
            "type": "cppdbg",                       // 只能是cppdbg
            "request": "launch",                    // launch:启动,attach:附加
            "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",    // 需要调试的程序
            "args": [],                             // 调试时传递给程序的参数
            "stopAtEntry": false,                   // 调试时是否停在程序入口:{true:是,false:否}
            "cwd": "${workspaceFolder}",            // 工作目录
            "environment": [],                      // 额外的环境变量
            "externalConsole": true,                // true:输出到外部终端;false:只输出到软件终端(有显示不全的可能)
            "MIMode": "gdb",
            "miDebuggerPath": "E:\\MinGW\\mingw64\\bin\\gdb.exe", // 调试gdb路径
            "setupCommands": [                      // 暂时不知道作用
                {
    
    
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: g++.exe build active file" // 预编译任务名称,和tasks.json中的label必须相同
        }
    ]
}

The functions of some statements in this document have been marked in the comments. Note that you must also modify the address of gdb.exe in conjunction with your own MinGW.

  1. tasks.json
{
    
    
    "tasks": [
        {
    
    
            "type": "shell",
            "label": "C/C++: g++.exe build active file",
            "command": "E:\\MinGW\\mingw64\\bin\\g++.exe",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
    
    
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
    
    
                "kind": "build",
                "isDefault": true
            }
        }
    ],
    "version": "2.0.0"
}

Also note that you need to modify the address of g++.exe yourself.

Run the program

After the above work is completed, in the cpp file interface (I don't know why it is in this file, or an error will be reported), click on the run interface (a small bug) on ​​the left, and click Run Program to run the program. The result of the operation is shown in the figure:
operation result
Here, because the launch.json file is set to "externalConsole": true, a black program running box can appear. If it is set to false, it will be output and printed in the "terminal" below.
In addition, when outputting Chinese characters, garbled characters may appear. The author refers to the setting method obtained by Baidu to realize the normal display of Chinese characters. The specific method is to reset the region and tick a place.
See the article for the specific process:

https://blog.amahv.cn/2020/06/24/vscode-shu-chu-chuang-kou-zhong-wen-luan-ma/

postscript

There is no mention of the use of VScode plug-ins, and some specific installation procedures, and the use of cmake has not been mentioned. Readers are advised to check the sharing of other netizens, and they will definitely gain a lot.
In the process of configuring VScode this time, I initially felt its complexity and elegance. Later, when configuring it under the Urbanu system, there should be another summary article.

Thank you for reading!
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_41883714/article/details/109137703