VSCode configuration and writing C/C++ program environment

One, install the VSCode editing environment

  There are two types of software on the official website, one is user-level and the other is system-level . (Install the software corresponding to the number of bits in the system)
Insert picture description here

  If you use a company’s computer, it’s best to install the user level , because the system level requires administrator authority, and the company’s computer administrator authority is usually in the hands of the IT department. In order to regulate the installation of software on computers, the company usually requires an administrator to install software. Permissions.

If I install a WeChat, I have to install a green version without installation.

After the installation is complete, you need to install the C/C++ plug-in (locale) in VSCode

Insert picture description here

2. Install the compiler mingw-w64-v8.0.0

  VSCode itself is just an editor, similar to a text editor, but its comparative advantage lies in the friendliness of the interface, such as good code hints, and it is lightweight, compared to VS.

  So if you need to compile the code, you also need to install a compiler, the mingw compiler ,
(choose according to the options below)

  Mingw-w64 download address
Insert picture description here

VS is an IDE that integrates VSCode and minggw,

The installation path cannot contain Chinese characters and spaces .

The last thing is to add system environment variables . (Change my environment variables, user environment variables, and then edit them) After the
Insert picture description here
Insert picture description here
Insert picture description here
final addition is complete, you can check, gcc -v , you can see the gcc version and other information.

Insert picture description here

Three, VSCode configures the C/C++ compilation environment

  First configure the compilation path, the version of the compiler, system type, etc., and create a c_cpp_properties.json file.

First, the shortcut key Ctrl+Shitf+P, and then enter C/C++: Edit Configuration (UI) .
Insert picture description here
Then follow the options below to fill in the system type ( win32 ) and compiler path ,

The compiler parameter can be empty, select gcc-x64 ,

Insert picture description here

Insert picture description here
The configured json file code is as follows:

{
    
    
    "configurations": [
        {
    
    
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "cStandard": "c11",
            "cppStandard": "c++11",
            "intelliSenseMode": "gcc-x64",
            "compilerPath": "D:\\SoftWare\\mingw-w64\\System\\mingw64\\bin\\g++.exe"     //编译器路径
        }
    ],
    "version": 4
}

  Secondly, create a task.json file to tell the compiler how to build the compiled program, such as compilation options. (Just copy and paste directly, pay attention to the path, the others are the same )

{
    
    
    // See https://go.microsoft.com/fwlink/?LinkId=733558 
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
    
    
            "type": "shell",
            "label": "g++.exe build active file",//任务的名字,就是刚才在命令面板中选择的时候所看到的,可以自己设置
            "command": "D:/SoftWare/mingw-w64/System/mingw64/bin/g++.exe",
            "args": [//编译时候的参数
                "-g",//添加gdb调试选项
                "${file}",
                "-o",//指定生成可执行文件的名称
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
    
    
                "cwd": "D:/SoftWare/mingw-w64/System/mingw64/bin"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
    
    
                "kind": "build",
                "isDefault": true//表示快捷键Ctrl+Shift+B可以运行该任务
            }
        }
    ]
}

  Finally, create a launch.json file to configure debugging information. (Just copy and paste directly, pay attention to the path, the others are the same )

{
    
    
        // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "2.0.0",
    "configurations": [

        {
    
    
            "name": "(gdb) Launch",
            "preLaunchTask": "g++.exe build active file",//调试前执行的任务,就是之前配置的tasks.json中的label字段
            "type": "cppdbg",//配置类型,只能为cppdbg
            "request": "launch",//请求配置类型,可以为launch(启动)或attach(附加)
            "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",//调试程序的路径名称
            "args": [],//调试传递参数
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,//true显示外置的控制台窗口,false显示内置终端
            "MIMode": "gdb",
            "miDebuggerPath": "D:\\SoftWare\\mingw-w64\\System\\mingw64\\bin\\gdb.exe",
            "setupCommands": [
                {
    
    
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

After configuring the above three files, create a new CPP file and write a main function, you can debug/run .

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_34430371/article/details/110189564