Configure C/C++ environment in VScode, super detailed, comprehensive problem analysis, absolutely easy to use

Configure C/C++ environment in VScode, super detailed, comprehensive problem analysis, absolutely easy to use

Configure C/C++ environment in VScode

Hello! This is the welcome page displayed for the first time you use the Markdown editor . If you want to learn how to use the Markdown editor, you can read this article carefully to understand the basic syntax of Markdown.

1. Download the editor VScode

1. Official website: https://code.visualstudio.com/
insert image description here
2. Install VScode (it is recommended to check all additional tasks;
insert image description here

2. Download the compiler MinGW and decompress it

Official website page: https://www.mingw-w64.org/

Download page: https://sourceforge.net/projects/mingw-w64/files/

You can go to the official website to find it yourself

You can also directly click on the download page found for you

Select x86_64-win32-seh to download on the download page
insert image description here
. If you cannot download
https://wwn.lanzouh.com/iLOip031ku6b password: 1234 due to network environment restrictions
insert image description here, you can unzip it anywhere in theory, but note that the path cannot contain Chinese, as for special Please test the characters yourself

3. Add MinGW to environment variables

Enter the bin folder under mingw64, copy the current path, Win + i evokes the system settings, enter the advanced system settings and enter, click the environment variable, select path, edit, create, paste the path, press three OK
insert image description here

4. Configure the VScode plugin

Open VScode to install plug-ins Chinese and C/C++, wait for the installation to complete and restart VScode

picture:
insert image description here

Switch C/C++ plugin to version 1.8.4

insert image description hereBecause the latest version does not automatically generate a launch.json file, which brings inconvenience to subsequent optimization, the old version is returned.

5. Run the code

Create a new folder, modify it to an English name and enter it, right-click to open it through Code If the relevant option is not checked during installation, this option may not be available, please open the folder by yourself in VScode

Create a new file with an English name and an extension of .c

Write relevant code


```c
#include <stdio.h>
#include <stdlib.h>
int main()
{
    
    
    printf("Hello World!\n");
    printf("你好世界!\n");
    system("pause");    // 防止运行后自动退出,需头文件stdlib.h
    return 0;
    }

On the VScode menu bar, click Run to start debugging, wait for the program to run, the output results will be in the terminal below, and the debug panel above, click the orange box on the far right to stop the program running

insert image description here

6. Tuning and optimization

Please optimize according to your own needs.
After the code runs, the .vscode folder will be automatically generated
. Task.json and launch.json under the .vscode folder in your source file directory are used to control the running and debugging of the program

Run the program on an external console [recommended]

.Open launch.jsonthe file , find "externalConsole": false, change false to trueand save
insert image description here

Solve the problem of Chinese garbled characters [recommended]

.Open the task.json file under the .vscode folder, find " ${fileDirname}\\${fileBasenameNoExtension}.exe", add a comma after it and press Enter to the next line, paste the text below "-fexec-charset=GBK"and save
insert image description here

Store the generated exe executable file [optional]

Open the task.json file in the .vscode folder and find"${fileDirname}\\${fileBasenameNoExtension}.exe"

Change it to "${fileDirname}\\coin\\${fileBasenameNoExtension}.exe"and save. Similarly, there are the same fields under launch.json, you need to modify

Create a new coin folder in the same directory as the source file. After the program runs, the executable file will be generated in it (the coin can be changed to your favorite English name)

In this way, when there are more .c files, there will be no .exe and .c interspersed in the directory
insert image description here

7. Solve VSCode debugging C/CPP, CMD flashback

Open the launch.json file
and modify "program" to "C:\Windows\system32\cmd.exe"

Copy the following code directly

{
    
    
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
    
    
            "name": "g++.exe - 生成和调试活动文件",
            "type": "cppdbg",
            "request": "launch",
            "program": "C:\\Windows\\System32\\cmd.exe",
            "args": ["/c","${fileDirname}\\${fileBasenameNoExtension}.exe","&","pause"],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "miDebuggerPath": "D:\\VScode\\mingw64\\bin\\gdb.exe",
            "setupCommands": [
                {
    
    
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                },
                {
    
    
                    "description": "将反汇编风格设置为 Intel",
                    "text": "-gdb-set disassembly-flavor intel",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: g++.exe 生成活动文件"
        }
    ]

}
If you configure it correctly, the terminal will now pause after the program is executed (that is, it will not flash back)

Guess you like

Origin blog.csdn.net/m0_46147064/article/details/124424305