VScode installation and C/C++ environment construction (detailed and effective version)

1. Installation of VScode

1.vscode download and installation

https://code.visualstudio.com/
The above is the official website of vscode.
After entering the official website, you will see the following interface.
Please add a picture description
If your system is also windows, follow the following operations. Otherwise, you
can choose according to the computer system.
Please add a picture description
After performing the above operations, you will enter the following interface
Please add a picture description
. At that time, the computer has started to install vscode. Note: If not, please click the "Download" icon above.
After the installation is complete, open the file and the following interface will appear
Please add a picture description
Please add a picture description

Please add a picture description
Please add a picture description
Please add a picture description
Please add a picture description

2. Install the Chinese plug-in

Please add a picture description
Please add a picture description

After the installation is complete, restart vscode

2. VScode configures the C/C++ compilation environment

1. MingGw installation

1. Click the link to enter the MingGw official website
https://sourceforge.net/projects/mingw-w64/files/
After entering the official website, scroll down and
you will see the following screen.
Please add a picture description
After the download is complete, you will get a compressed package.
Please add a picture description
The icon may be different but the name is the same.
After decompression, it will be The mingw64 folder is copied to the root directory of the hard disk
(I moved it to the D drive, so the address of my mingw64 folder is: D:\mingw64)
as shown below
Please add a picture description

2. Edit environment variables

Open the settings to search for environment variables
. Select to edit the system environment variables, and the following interface will pop up.
Please add a picture description
Please add a picture description
Please add a picture description
The address here is the address of the bin directory stored in mingw64, as shown in the figure.
Please add a picture description

3. Check and download the C/C++ plug-in

After the above operations are completed, we can click win+R to enter cmd to open the command panel and enter the following commands respectively

gcc --version
g++ --version

If it appears as shown in the figure below, it is successful.
Please add a picture description
Then open vscode and perform the following operations.
insert image description here
Please add a picture description
After the installation is complete, restart vscode again.

4. Set up compilation and debugging of C/C++ code

First, create a new folder to store the code written in the future.
Please add a picture description
After the selection is completed, the following interface will appear.
Please add a picture description
For example, if I create a new file named class and suffix cpp, it is a C++ type file. If the suffix is ​​C language type file,
Please add a picture description
then click the mouse on the code interface. Then
press Ctrl+shift+p and the following interface will appear.
Please add a picture description
Click to enter and the following interface will appear.
Please add a picture description
If your file is in C language, select gcc; if it is C++, select g++.
Let’s take C++ as an example.
Please add a picture description
After the above operations are completed, return to the code interface
(Note: If you have completed the above operations)
, then click the mouse on the code interface,
click the terminal and select the configuration task, and the following screen will appear.
Please add a picture description
After clicking, the tasks.json file will pop up.
Replace the code in
it with the following code. Must be modified according to the comments

{
    
    
	"version": "2.0.0",
	"tasks": [
		{
    
    
			"type": "cppbuild",
			"label": "C/C++: g++.exe 生成活动文件",
			"command": "D:/mingw64/bin/g++.exe",
			"args": [
				"-fdiagnostics-color=always",
				"-g",
				"${file}",
				"-o",
				"${fileDirname}\\${fileBasenameNoExtension}.exe"
			],
			"options": {
    
    
				"cwd": "D:/mingw64/bin"
			},
			"problemMatcher": [
				"$gcc"
			],
			"group": "build",
			"detail": "编译器: D:/mingw64/bin/g++.exe"
		}
	]
}

Once done you can run the code!
Please add a picture description
Then you click on the terminal below
and you will find that the code runs successfully.
Note: If you still want to run the C language file after completing the above operations, you need to perform the following operations.
First, click the mouse on the code interface and
press Ctrl+shift+p to select UI, then
select Add Configuration and enter the configuration you want name (cannot be Chinese and special symbols)
Please add a picture description

Repeat 4. Set the compilation of C/C++ code (not related to debugging)

5. vscode debugging code

Please add a picture description
Then your interface will pop up the launch.json file and replace the code with the following code
Note: Do not modify some of the code according to the comments

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

    ]
}

After the modification is completed, you can set breakpoints for debugging
Note: F5 is for debugging, F10 is for step-by-step debugging


Summarize

The above is the installation of the entire VScode and the construction of the C/C++ environment. I hope it will be helpful to you, and I hope to give a small praise. Thank you.

おすすめ

転載: blog.csdn.net/m0_73073479/article/details/130722907