VSCode在windows下C/C++环境配置
部分内容来源于:
1. launch.json
需要修改的地方:launch中 "miDebuggerPath" 选项需要设置为你的调试器(gdb.exe)所在位置 这里的是我电脑上MinGW -w64的安装位置
无论安装的是MinGW还是mingw-w64,都会有一个gdb.exe在安装目录的bin文件夹下,一定要把对应的路径修正否则无法调试
<pre>
<code>
{
"version": "0.2.0",
"configurations": [
{
"name": "C/C++",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "D:/develop/mingw64/bin/gdb.exe",
"preLaunchTask": "g++",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
},
]
}
</code>
</pre>
2. tasks.json
{
"version": "2.0.0",
"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
}
}
之后打开在当前工作区子目录下的.c/cpp文件就可以添加断点进行调试了
3. c_cpp_properties.json
如果找不到头文件可以添加如下配置文件
其中,要将includePath选项更改为你mingw编译器的安装路径下的lib/gcc/x86_64-w64-mingw32/8.1.0/include文件夹路径
{
"configurations": [
{
"name": "Win32",
"includePath": [
"D:/develop/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"intelliSenseMode": "gcc-x64"
}
],
"version": 4
}
对于非标准库中的头文件,也可以通过列表追加的方式将路径追加到includePath中去
比如:
"includePath": [
"C:/Program Files/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include",
"path1",
"path2",
...
],
4. 程序在终端下运行
依次打开:文件>首选项>设置>用户设置>拓展>Run Code Configuration
找到 Run In Terminal 打上勾 这样运行的程序就会运行在vscode的终端上
在工作区设置也有这个选项,但工作区设置只会对工作区生效
5. 程序运行时在终端无法输入的问题
将输入法切换到英文模式,中文模式下可能只能输入个别数字然后卡死。
6. 常用插件
- C/C++
- C++ Intellisense
- Code Runner
- One Dark Pro
- Bracket Pair Colorizer