VS Code配置C/C++
安装
-
下载并安装MinGW-w64
推荐使用离线方式安装,因网络原因,在线安装大概率失败。
-
下载插件:C/C++ 插件
创建配置文件
在项目工程目录下创建.vscode
文件,分别创建三个文件:
-
c_cpp_properties.json
,将文件中的目录替换成你的MinGW安装目录:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceRoot}",
"C:/Program Files/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++",
"C:/Program Files/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32",
"C:/Program Files/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/backward",
"C:/Program Files/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include",
"C:/Program Files/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/tr1",
"C:/Program Files/mingw64/x86_64-w64-mingw32/include"
],
"defines": [
"_DEBUG",
"UNICODE",
"__GNUC__=6",
"__cdecl=__attribute__((__cdecl__))"
],
"intelliSenseMode": "msvc-x64",
"browse": {
"path": [
"${workspaceRoot}",
"C:/Program Files/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++",
"C:/Program Files/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32",
"C:/Program Files/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/backward",
"C:/Program Files/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include",
"C:/Program Files/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/tr1",
"C:/Program Files/mingw64/x86_64-w64-mingw32/include"
],
"limitSymbolsToIncludedHeaders": true,
"databaseFilename": ""
},
"compilerPath": "C:/Program Files (x86)/mingw64/bin/gcc.exe",
"cStandard": "c11",
"cppStandard": "c++17"
}
],
"version": 4
} -
launch.json
,将miDebuggerPath
替换成你的MinGW安装目录:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18{
"version": "0.2.0",
"configurations": [
{
"name": "C++ Launch (GDB)",
"type": "cppdbg",
"request": "launch",
"targetArchitecture": "x86",
"program": "${workspaceRoot}\\${fileBasename}.exe",
"miDebuggerPath":"C:/Program Files/mingw64/bin/gdb.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceRoot}",
"externalConsole": true,
"preLaunchTask": "g++"
}
]
} -
tasks.json
:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17{
"version": "2.0.0",
"command": "g++",
"args": ["-g","-std=c++11","${file}","-o","${workspaceRoot}\\${fileBasename}.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
}
}
}
使用
按F5
或左侧工具栏第四个Debug按钮中的绿色运行按钮即可运行,若有断点则为debug。
(可选)安装 Code Runner插件
Code Runner 可以免除以上三个配置文件的创建,能很方便地运行代码,但是无法调试。如果无需调试,可以选择安装此插件:
按ctrl+,
进入设置界面,点击右上角切换到Config
文件,添加以下内容:
1 | /* Code Runner Config*/ |
完成后,点击右上角三角按钮或右键Run Code
即可运行代码。
更多
更多VS Code相关配置见环境搭建 - VS Code
参考资料
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 哆啦 C 梦!
评论