How does VS code configure python and C++ environments at the same time (including configuring C++ environments separately)

How does VS code configure python and C++ at the same time {\color{Red}How does VS code configure python and C++ at the same time}How does V S c o d e configure p y t h o n and C at the same time++

1. Install mingw-w64


MinGW is a compilation environment, the full name is Minimalist GNU For Windows, which is equivalent to the GCC download address under linux : https://sourceforge.net/projects/mingw-w64/files/
After opening, enter the following web page:

Don’t click download directly, look down, find the latest MinGW-W64 installation package, the latest is MinGW-W64 GCC-8.1.0, as follows: click to download, after downloading is insert image description here
a 7Z compressed package, decompress it directly That's it. It is recommended to decompress it to the C drive, for example: C:\Program Files\mingw-w64
Whether it is Win10 or Win11, directly enter the advanced system settings in the search box to open the advanced system settings, add the following mingw-w64 in the environment variable Absolute path of bin (it is recommended to decompress to C drive)

Add environment variables:
Click Environment Variables (N) –> Click Path in System Variables (S) –> Click Edit below –> New –> Enter the path of the bin file obtained after installing MinGW above in the space (for example : C:\Program Files\mingw-w64\x86_64-8.1.0-release-posix-seh-rt_v6-rev0\mingw64\bin) –> OK –> OK –> OK. As shown below.
insert image description here
 
 

2. Check whether gcc and gdb are installed successfully on the cmd side

2.1 View gcc

Win+R, enter cmd

在终端中输入
gcc --version
get:

insert image description here

2.2 view gdb

在终端中输入
gdb --version

Get:
insert image description here
This indicates that the installation is successful.
 
 
 

3. Install the necessary plug-ins for VSCode configuration C++

3.1 Install C/C++

insert image description here

 

3.2 Install Code Runner, C/C++ Compile Run

insert image description here

 

3.3 Install CodeLLDB (install python3.x at the same time)

insert image description here
If anaconda has been installed and comes with python3, there is no need to install python3.
 

4. Configure task.json

Take compiling and running a single 1.cpp as an example, the configuration is as follows:
1.cpp:

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

 

Choose Terminal > Configure Default Build Task.
insert image description here
Then select C/C++ as shown in the figure below
insert image description here
: g++.exe build active file enters task.json:
insert image description here
In order to facilitate everyone to copy and modify, post the source code:

{
    
    
	"version": "2.0.0",
	"tasks": [
		{
    
    
			"type": "shell",
			"label": "C/C++: g++.exe build active file",  // 需与lauch.json中"preLaunchTask"项命名一致
			"command": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-release-posix-seh-rt_v6-rev0\\mingw64\\bin\\gcc.exe",    //需要修改成自己的gcc.exe所在路径,这个可执行文件在mingw-w64下面的bin里面,就是刚才添加到系统环境变量里的bin
			"args": [
				"-g",
				"${file}",
				"-o",
				"${fileDirname}\\${fileBasenameNoExtension}.exe"  // 输出exe名,要与launch中调用的program项命名一致
			],
			"options": {
    
    
				"cwd": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-release-posix-seh-rt_v6-rev0\\mingw64\\bin"
			},
			"problemMatcher": [
				"$gcc"
			],
			"group": {
    
    
				"kind": "build",
				"isDefault": true
			}
		}
	]
}

This basically does not need to be modified, what we get directly is the task.json configured for us by vscode
 
 

5. Configure launch.json

First give a template and the author's own launch.json:


{
    
    
    // 这是模板
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    // 转载自https://www.zhihu.com/question/456362523/answer/2144616793
    "version": "0.2.0",
    "configurations": [
        {
    
    
            "name": "gdbdebug",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}\\main.exe", // 调用的exe名,要与tasks生成的exe名一致
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true, // 决定输出是单独外部黑窗口显示,还是在IDE里终端黑窗口显示
            "MIMode": "gdb",
            "miDebuggerPath": "D:\\xx\\mingw\\bin\\gdb.exe",
            "preLaunchTask": "Build", // 此项命名需要与tasks.json中的label项命名一致,作用是每次调用launch时会先调用tasks.json,从而不用自己每次都ctrl+shift+b手动生成最新任务后,才能调用launch
            "setupCommands": [
                {
    
    
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

 
This is the author's modified launch.json:

{
    
    
	// 这是我的launch.json,读者不需要看python.主要看C++(gdb启动)的部分
    "version": "0.2.0",
    "configurations": [

        {
    
    
            "name": "Python: Terminal (external)",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "externalTerminal"
        },
        {
    
    
            "name": "(gdb) 启动",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "miDebuggerPath": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-release-posix-seh-rt_v6-rev0\\mingw64\bin\\gdb.exe",
            "preLaunchTask": "C/C++: g++.exe build active file",
            "setupCommands": [
                {
    
    
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

 
As you can see: the focus is on specifying the location of gdb.
Modify the miDebuggerPath section. Change to the absolute path of gdb.exe. Readers please refer to the path to install MinGW, mine is C:\Program Files\mingw-w64\x86_64-8.1.0-release-posix-seh-rt_v6-rev0\mingw64\bin\gdb.exe.

Note that slashes in launch.json should be written as double slashes or backslashes.
 
 
 
Compile and run 1.cpp will be introduced in the next blog.

Guess you like

Origin blog.csdn.net/weixin_49457347/article/details/123601239