[Wins+VSCode] Configure C++ development environment

1. Install vscode

Just download it from the official website, free of charge: https://code.visualstudio.com/

2. Install the Chinese language package and the C++ extension package

Find the extension button directly on the left, and install the c++ extension package:
insert image description here

3. Install the c++ compilation tool: g++

Install directly https://sourceforge.net/projects/mingw-w64/files/

Then add the installed mingw to the system environment variable:
insert image description here
verify whether the installation is successful:
insert image description here

4. Run code tests

Create a new test file:

#include<iostream>
using namespace std;
int main()
{
    
    
    cout << "hello world" << endl;
    return 0;
}

Click to run in debug/non-debug mode:

insert image description here
Choose GDB:
insert image description here
Choose g++:
insert image description here

At this time, it can run normally and display the results, the program can be executed normally, debugging and running can be done.
insert image description here
But careful students can find that there are two more files in the left file: lauch.json and tasks.json. These are two very important files, which are the key configurations of the entire development, and it is necessary to understand what they are for.

5、leek.json

Function: Used to add gdb debugging tasks and run executable files.

Initialization generates:

{
    
    
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
    
    
            "name": "g++.exe - 生成和调试活动文件",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",  // 调用的exe名,要与tasks生成的exe名一致
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,  // // 决定输出是单独外部黑窗口显示,还是在IDE里终端黑窗口显示
            "MIMode": "gdb",
            "miDebuggerPath": "D:\\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 生成活动文件"   
            // 此项命名需要与tasks.json中的label项命名一致,作用是每次调用launch时会先调用tasks.json,从而不用自己每次都ctrl+shift+b手动生成最新任务后,才能调用launch
        }
    ]
}

【parameter】

  1. program: the path of the program to be debugged;
  2. args: parameters added at runtime;
  3. stopAtEntry: False by default, True will stop at the function entry;
  4. externalConsole: whether to run outside the external control, setting it to true will pop up the windows running window;
    5. miDebuggerPath: the path of the local gbd debugger;
  5. setupCommands: Set the corresponding commands for the GDB debugger before starting debugging;
    7. preLaunchTask: Fill in the task name (label item) in the task.json file for the task to be executed before running the debugging;

6、tasks.json

Function: It is equivalent to the summary of gcc compilation steps, and can also call makefile to finally generate an executable file.

Describes what command needs to be called to execute the task, which is equivalent to executing the command directly in the command line cmd. Corresponding to c++ development, it is the process of program construction.

Initialization generates:

{
    
    
    "tasks": [
        {
    
    
            "type": "cppbuild",
            "label": "C/C++: g++.exe 生成活动文件",  // 需与lauch.json中"preLaunchTask"项命名一致
            "command": "D:\\mingw64\\bin\\g++.exe",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"  // 输出exe名,要与launch中调用的program项命名一致
            ],
            "options": {
    
    
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
    
    
                "kind": "build",
                "isDefault": true
            },
            "detail": "调试器生成的任务。"
        }
    ],
    "version": "2.0.0"
}

【Parameter configuration】

  1. label: the name of this task, which will be used lanch.jsonin ;preLaunchTask
  2. command: The command to execute, if it is developed in C++, that is g++, gccthese build commands, half of which write the g++ address in the system environment variable;
  3. args: The input parameters when executing the command, g++ generally uses the default;

7. Problem

Pay attention to the above blackened parameters, and generally there will be no problems after configuration.

7.1. The gcc task cannot be found

The label name in tasks.json does not correspond to the preLaunchTask name in launch.json, resulting in the launch not being able to correspond to the exe after compilation.

Analysis: tasks.json is compiling, launch.json is running exe, tasks.json is before lanuch. The label in tasks must correspond to the preLaunchTask name in launch.json. Otherwise, an error that the gcc task cannot be found will be reported.

Reference

Station b: [Tutorial] Configure C language/C++ operating environment in VScode

Zhihu: How does vscode configure launch.json and tasks.json in the c/c++ locale in the latest version?

Guess you like

Origin blog.csdn.net/qq_38253797/article/details/127873693