Home >Development Tools >VSCode >One article explains in detail vscode configuration C/C++ running environment [nanny-level teaching]
How to develop C/C in VScode? How to configure the C/C environment? The following article will share with you the VScode configuration C/C operating environment tutorial (nanny-level teaching). I hope it will be helpful to everyone!
Configure the C/C running environment in Vscode. First, you need to download C /C development environment, and then add the C/C development environment to the system variables.
The first step is to download vscode
Everyone should be able to download VsCode, but VsCode is not provided here. Download and installation tutorial
The second step is to download mingw
Here mingw is used as C/C Development environment, the official website link is as follows
Official website link:MinGW official website
After the download is completed, we will get such an installation Program [Recommended learning: vscode tutorial, Programming teaching, vuejs video tutorial]
Double-click to open
##Then we Find the installation folder of mingw
in sequence mingw-get install gcc
- mingw-get install g
- mingw-get install gdb
NoteIf you want to use the gdb debugger, please refer to the msys2 installation method provided by the boss in the comment area Install the latest version: Because the server that provides the download service is abroad, the download will be slow, and some content may not even be downloaded (such as gdb.exe). If you do not want to use gdb for debugging, gdb.exe does not have to be downloaded. Yes, (the run code is compiled and run directly, no gdb debugger is required)
or download it from Baidu Netdisk (don’t ask why you use Baidu Netdisk, because other netdisks are full of T_T)The version provided here is version 8.1.0 (the latest version 11.2, as of 2022 /11/14)
Link:
MinGW 8.1.0 version
Extraction code: Suif Compressed package size 129.41MB, use Baidu Netdisk for a limited time experience of 30s (if you still have If yes), it will be downloaded soon
Personal test, version 8.1.0 fully supports C 17 and below standards.
##Then mingw download is completed
The third step is to add mingw to the system variables#This step is to tell the computer in which folder the C/C running environment can be found
#In this way, our system environment is configuredWe can check whether the configuration is correct
Press and hold win r to open In the following interface
enter cmd, then click OK
and enter
gcc -v
g -v
gdb -v
See if normal results are displayed
If the results are displayed normally, it means that the system environment is configured correctly. At this time, it is recommended to restart the computer (of course, it is not necessary Restart)
The fourth step is to open VsCode and install the necessary plug-ins
Our vscode configuration C/C preparation work is completed
The first step is to create a new folder to store C/C files and create a new cpp file
We open that folder in vscode
Note: The configured environment is only applicable In this large folder
Configuration method 1 Configuration method with the help of run code plug-in-simple
This is the simplest configuration method, and it is also the most friendly method for beginners or people with little contact with computers.This method requires the use of a plug-in, run code. Let’s first Open the extension store and install the run code plug-in
. After the installation is completed, we will find that there is an additional run code on the right click.
# Just click run code to compile and run the program.
Due to the settings of run code Problem, some students may get output results when using run code to run the program, but cannot directly input content into the program in vscode, so we need some settings to adjust the run code.
We press ctrl to enter settings (ctrl comma) or click File-> Preferences-> Settings
##Restart vscode so that we can enter content into the program in vscode
"c": "cd $dir && gcc $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt", "cpp": "cd $dir && g++ $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
Configuration method 2 Configuration method using gdb debugging - complex
If you want to use the gdb debugger to debug the program, you need to use this method
We hold down ctrl shift p to open the command panel or click View-> command Panel
We configure the compiler path and c/c standard
Then close this interface, we will find that there is an additional .vscode folder in the CppProject folder
Similarly, we hold down ctrl shift p to enter the command Panel or click View-> Command Panel
At this time we will find that there are more tasks.json## in the .vscode folder# Briefly introduce what you need to pay attention to in tasks.json:
label
- : Compile the task name and configure launch.json afterwards We can call the compilation task by the compilation task name.
command- : Compiler path, the compilation task will call the compiler, please make sure the path is correct. args
: Command transmitted to the compiler. By setting this content, you can set the path to generate the exe program
Because the current C/C plug-in will not automatically generate launch.json, so launch .json needs to be written by ourselves. Create a new launch.json file in the .vscode folder
We will find that there is an added configuration
We click Add Configuration and select gdb to start
We Three places need to be modified
The first place: program - executable program path
- This content indicates the called program path , its value should be the same as the path of the exe program generated in task.json, otherwise an error will be reported because the executable program cannot be found
Second place: miDebuggerPath - Custom debugger path
- This value represents the path of the gdb debugger. The value should be consistent with the path of the gdb debugger you want to use, otherwise it will prompt that the gdb debugger cannot be found
##The third place:preLaunchTask - Compilation task executed before debugging
This value indicates the compilation task used before starting the gdb debugger, and its value Equal to label in task.json.
- This content is not automatically generated, so we need to write it manually.
Save and restart the editor, we can press F5 to debug the program
Note: Any changes made to the file need to be saved before the new content will take effect. We can press ctrl s to quickly save the file
Q: Why does this error message appear after pressing F5 and there is no output result
Answer: This is not an error message, but a compilation and debugging command, and the output results are displayed in the debugging console.
Some friends may find that the debugging console is not easy to use. If you want vscode to compile and run a c/c program, a terminal will pop up and run the program in the terminal. This Clicking is actually very simple. We only need to change the contents of program and args in launch.json, and then set the black window to pop up.
Q: Why not set it to run the program in the vscode built-in terminal?Answer: Using gdb debugging with vscode does not provide configuration items for running programs in the built-in terminal of vscode.
If you want to set up the program to run in the vscode built-in terminal, please refer to configuration method one or configuration method three
Note : Using the system terminal (cmd) to run the program cannot use breakpoint debugging, so please configure it as appropriate
It is not necessary for people like me who use vscode to write algorithm questions. breakpoint, so it is better to configure it to pop up a black window.
我的program 和 args 中的内容:
"program": "C:\\Windows\\System32\\cmd.exe", "args": [ "/c", "${fileDirname}\\build\\${fileBasenameNoExtension}.exe", // 更改这项内容为task.json中生成的exe程序路径就好 "&", "pause" ],
完成后的效果
有的小伙伴不想用run code,但是又想达到run code那样能在vscode内置终端中执行程序的效果,可以尝试下使用windows调试。(实际上就是只编译运行程序,没有任何辅助调试程序的功能)
注意:该配置方法无法使用断点调试。
同配置方法二中的第一步
同配置方法二中的第二步
先自建一个lanuch.json文件(参考配置方法二中的第三步)
- 如果lanuch.json中有内容的话,先把configurations中的内容注释掉或者删除掉
- 全选configurations中的内容,按下快捷键ctrl + / 即可快速注释选中内容。
然后我们点击添加配置,选择windows启动
我们只需要修改两项内容即可
第一处:program - 可执行程序路径
- 该项内容表示调用的程序路径,其值应和task.json中生成的exe程序路径相同,否则会由于无法找到可执行的程序而报错
第二处:preLaunchTask - 调试之前执行的编译任务
- 该项值表示启动gdb调试器之前使用的编译任务,其值等于task.json中的label。
- 该项内容并不会自动生成,因此需我们手动编写。
注意配置项: console - 启动调试目标的位置
- 通过更改该项的值,我们可以自由地切换在系统终端中运行程序还是在vscode终端中运行程序
- 该项值为 externalTerminal 则是在系统终端中运行程序
- 该项值为 integratedTerminal 则是在vscode终端中运行程序
我们把该项值改为 integratedTerminal 即可在vscode终端中运行程序
最终效果:
全篇结束,感谢阅读!如果有任何疑问可以评论区留言(因为水平有限,有些问题不一定能解答哈)!
编辑器插件推荐:
- GitHub Theme: Theme plug-in, the editor theme shown in this article, Github Light pure white looks very comfortable.
- vscode-icons: Icon plug-in, the icons shown in this article, with the icons, the sidebar looks more vivid.
- codeSnap: A tool plug-in that generates code screenshots and is used to analyze the code. It is very nice.
For more knowledge about VSCode, please visit: vscode Basic Tutorial!
The above is the detailed content of One article explains in detail vscode configuration C/C++ running environment [nanny-level teaching]. For more information, please follow other related articles on the PHP Chinese website!