VSCode configures C/C++ language environment (version 2023)

The basic steps:

Download and install VSCode

vscode download address https://code.visualstudio.com/

If you don't know how to install vscode, see the blog below:

Super detailed VSCode installation tutorial (Windows) - Suoerya's Blog - CSDN Blog

Install the C++ plugin

Install the compiler (MinGW-W64 GCC)

C compiler (MinGW-W64 GCC) download address: https://sourceforge.net/projects/mingw-w64/files/mingw-w64/

The exe files downloaded online generally have network problems, resulting in download failure. It is recommended to directly download the 8.1.0 version of x86_64-win32-seh or x86_64-posix-seh for Windows 64 bits. There are some differences between the two in terms of multi-threading. Generally, this function will not be used, so both can be used. After downloading, use decompression software to decompress it.

Configure environment variables

Find a folder called bin in this folder, and then copy its address:

 

After opening, add the address you just copied:


Then click OK, and click OK on all the pages that popped up before. Then test whether the environment configuration is successful:
Win+R shortcut key to open the running window, enter cmd in it, and press Enter to open cmd.exe

 

Enter the following command in cmd.exe:
gcc -v -E -x c++ -
If the running result is as shown in the picture below, the configuration is successful.

configuration

Finally, perform relevant configuration in VSCode:
first create a new folder as the C language project file, then click File—>Open Folder in the menu bar, find the newly created folder, and then click Select Folder to open the project file.
Then create a hello.c file in it (name it casually, just end with .c)

Then build a
.vscode folder (note that there is a dot in front), and create three files in it, c_cpp_properties.json, launch.json, tasks.json

  • c_cpp_properties.json: Copy this code into it
{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceRoot}",
                "C:/MinGW-W64 GCC/mingw64/include/**",
                "C:/MinGW-W64 GCC/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++",
                "C:/MinGW-W64 GCC/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32",
                "C:/MinGW-W64 GCC/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/backward",
                "C:/MinGW-W64 GCC/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include",
                "C:/MinGW-W64 GCC/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include-fixed",
                "C:/MinGW-W64 GCC/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}",
                    "C:/MinGW-W64 GCC/mingw64/include/**",
                    "C:/MinGW-W64 GCC/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++",
                    "C:/MinGW-W64 GCC/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32",
                    "C:/MinGW-W64 GCC/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/backward",
                    "C:/MinGW-W64 GCC/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include",
                    "C:/MinGW-W64 GCC/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include-fixed",
                    "C:/MinGW-W64 GCC/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/include"
                ]
            }
        }
    ],
    "version": 4
}

Then, the content in the red box below needs to be modified, changing all to your own installation path:

  • launch.json: copy and paste, and then change the content in the miDebuggerPath attribute to your own path
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(Windows) Launch",
            "type": "cppvsdbg",
            "request": "launch",
            "program": "cmd",
            "preLaunchTask": "echo",
            "args": [
                "/C",
                "${fileDirname}\\${fileBasenameNoExtension}.exe",
                "&",
                "echo.",
                "&",
                "pause"
            ],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "console":"externalTerminal"
        },
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "miDebuggerPath": "C:\\MinGW-W64 GCC\\mingw64\\bin\\gdb.exe",// 自己电脑的gdb
            "preLaunchTask": "echo",//这里和task.json的label相对应
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
 
        }
    ]
}
  • tasks.json: copy and paste
{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "echo",
            "type": "shell",
            "command": "gcc",
            "args": [
                "-g", 
                "${file}", 
                "-o", 
                "${fileBasenameNoExtension}.exe",
                "-fexec-charset=GBK",//解决中文乱码
                "-lstdc++"//解决只能运行c不能运行c++
            ]
        }
    ],
    "presentation": {
        "echo": true,
        "reveal": "always",
        "focus": false,
        "panel": "shared", 
        "showReuseMessage": true,
        "clear": false
    }
}


Then you can write the program in the previously created hello.c file, such as the hello world we are familiar with:

#include<stdio.h>
main()
{
    printf("hello world\n");
   
    //system("pause");
}

Program crash problem

As long as the above three files are copied correctly and the path is changed to your own flashback problem, the problem has been solved

If it doesn't work, just enter

system("pause");

f5 running results:


Vscode configures the c/c++ environment and the configuration is complete.

 The above reference blog:

VSCode configures C language environment (full version)_vscode c language_SchizophreniA6's Blog-CSDN Blog

vscode can debug c but can't debug c++ program_vscode can't debug c++ code_Pher12's Blog-CSDN Blog

VS code "The key "externalConsole" is deprecated, please use the solution of console" instead_The key "externalconsole" is deprecated. Please use "console" instead. _snowayoung's Blog - CSDN Blog

Super detailed VSCode installation tutorial (Windows) - Suoerya's Blog - CSDN Blog

Guess you like

Origin blog.csdn.net/weixin_44406127/article/details/129898330
Recommended