In the first semester of my freshman year, I was given vscode by my senior, but I couldn’t configure it after downloading and installing it. After seven or eight hours of searching and asking, I finally configured it. Many students around me don’t know how to configure, so they all come to me for help. In addition, when I was groping for configuration, I felt that there were no detailed and reliable tutorials on the Internet, so I decided to record the configuration process for your reference. I hope it can help To the students who want to use it like me but don't know how to configure vscode.
Table of contents
The first step is to download and install VSCode
The second step is to download and install g++
The third step is to install the VSCode plug-in
The fourth step is to configure the debugging function
1. Chinese display garbled characters
3. When debugging, it shows "cannot find g++"
The first step is to download and install VSCode
This should be the easiest step, I believe you can do it yourself. If you feel that the download is particularly slow on the vscode official website, you can try the Tencent Software Center. I downloaded it from this webpage. After downloading, follow the prompts to install it.
The second step is to download and install g++
MinGW Distro - nuwen.net https://nuwen.net/mingw.html
This is the link I found in the comment area when I was looking for tutorials at station b. You can download it with your own network
After entering, click the link circled in the picture to download
Install after the download is complete, choose the installation path yourself, you need to remember the installation path, it will be used soon
Note that Chinese cannot appear in the path! Note that Chinese cannot appear in the path! Note that Chinese cannot appear in the path!
For convenience, I installed it directly on the C drive.
After the installation is complete, we open the installation path just now, find and open MinGW -> bin, enter the bin folder and click here, right click to copy the path
Then we enter the settings, search for "environment variables", select "edit system environment variables"
Click to enter the environment variable
Double-click Path
Click New and paste the path you just copied
Then click OK three times in a row.
Then let's take a look at whether the operation just now was successful, press Win+R, enter cmd, and enter g++ --version in the console
Even if such an interface appears, it is successful
In this way, our g++ is installed!
The third step is to install the VSCode plug-in
Now we open the VSCode we just installed and click this button
Search C/C++ to install the first plugin (I have configured it before so it is already installed)
Then search and install Code Runner
If you need the Chinese version, you can search and install the Chinese plug-in (I don’t recommend it personally, if you feel difficult to get started, you can install it if you are a novice)
There are also many useful plug-ins, such as automatic completion, changing the color of brackets, etc. If you need it, you can search it yourself. I won’t digress here.
The fourth step is to configure the debugging function
This is the most important part of the configuration process! The previous content is also easy to find on various platforms, and then I was stuck at this step for several hours.
First of all, you can create a folder in a location you want, and you can name it at will (note that Chinese is not allowed!), and the future C/C++ code files must be placed in this folder to be able to debug normally.
Here I built it on the desktop.
Then enter VSCode, click Open Folder or click File -> Open Folder in the upper left corner, then open the folder you just created, and choose to trust the parent folder
Click this icon to create a new folder and name it .vscode (note that it must be this name!)
After the creation is complete, click this icon to create four new files. The file names are
//c_cpp_properties.json
//launch.json
//settings.json
//tasks.json
Next copy and paste the contents of these four files
The first is c_cpp_properties.json
{
"configurations": [
{
"name": "Win64",
"includePath": ["${workspaceFolder}/**"],
"defines": ["_DEBUG", "UNICODE", "_UNICODE"],
"windowsSdkVersion": "10.0.18362.0",
"compilerPath": "C:/MinGW/bin/g++.exe",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "gcc-x64"
}
],
"version": 4
}
Note that the compilerPath item needs to change the path to the installation path of g++ just now: find the installation folder just now->MinGW->bin->g++,exe, then copy or manually type the path of g++.exe, the format should be the same as Same as above code snippet
then launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceRoot}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe",
"preLaunchTask": "g++",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
Note that the path of miDebuggerPath should also be changed to the installation path of g++ just now: find the installation folder just now->MinGW->bin->gdb,exe, then copy or manually type the path of gdb.exe, the format should be Same as above code snippet
Next is settings.json
{
"files.associations": {
"*.py": "python",
"iostream": "cpp",
"*.tcc": "cpp",
"string": "cpp",
"unordered_map": "cpp",
"vector": "cpp",
"ostream": "cpp",
"new": "cpp",
"typeinfo": "cpp",
"deque": "cpp",
"initializer_list": "cpp",
"iosfwd": "cpp",
"fstream": "cpp",
"sstream": "cpp",
"map": "c",
"stdio.h": "c",
"algorithm": "cpp",
"atomic": "cpp",
"bit": "cpp",
"cctype": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"compare": "cpp",
"concepts": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"exception": "cpp",
"ios": "cpp",
"istream": "cpp",
"iterator": "cpp",
"limits": "cpp",
"memory": "cpp",
"random": "cpp",
"set": "cpp",
"stack": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"utility": "cpp",
"xfacet": "cpp",
"xiosbase": "cpp",
"xlocale": "cpp",
"xlocinfo": "cpp",
"xlocnum": "cpp",
"xmemory": "cpp",
"xstddef": "cpp",
"xstring": "cpp",
"xtr1common": "cpp",
"xtree": "cpp",
"xutility": "cpp",
"stdlib.h": "c",
"string.h": "c"
},
"editor.suggest.snippetsPreventQuickSuggestions": false,
"aiXcoder.showTrayIcon": true
}
Finally tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "g++",
"command": "g++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}.exe"
],
"problemMatcher": {
"owner": "cpp",
"fileLocation": ["relative", "${workspaceRoot}"],
"pattern": {
"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
},
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
Save these four files and the configuration is complete!
Emphasis again: future C/C++ code files must be placed in this Code folder, or in a folder with a .vscode folder. If you debug code files placed in other locations, an error will be reported!
You can create multiple folders in the Code file to store codes in categories like me.
If you have completed the above process, now you can create a new .c or .cpp file and write code to test the VSCode you just configured! (Note that the file name cannot be in Chinese!)
something else
1. Chinese display garbled characters
This is a very common problem, so when I help others configure it, I usually help out.
First click the gear button in the lower left corner to open Settings
Enter ecocoding in the search box, and then change Encoding to GBK as shown in the figure (it should be UTF-8)
After setting, editing files with Chinese characters will not display garbled characters!
2. Run in terminal
If you don’t like a small black box popping up every time, you can choose to run it in the terminal, the effect is as shown in the figure
This setting is also very simple. Open the settings, search for run in terminal, find this option and tick it.
After that, you can click the button in the upper right corner to run/debug in the terminal below, or press the shortcut key Ctrl+Alt+N to run (personally prefer)
Note that you must save it before running the terminal, otherwise it will run the code before saving (you can check the automatic save in File)
Of course, you can still choose to press F5 to use the small black box to run and debug
But the two processes are in conflict, there is just one more option, and they cannot be used at the same time! For example, when I am running the code in the terminal, if I press F5 to debug the code, an error will occur
3. When debugging, it shows "cannot find g++"
First check whether there is Chinese in the g++ installation path or file name, if there is Chinese, you need to change the Chinese name or replace it with another path to install
If Chinese does not exist, right-click the VSCode icon and select "Properties"
Then select "Compatibility", check "Run this program as an administrator", and then click "Apply" and click "OK" (some computers need to select this option)