配置VsCode在Windows下使用C/C++

好久不写了,正好重做生产环境,在这里记录一笔。

装MinGw啥的我就不废话了,

这个实在不会就百度就完事了。

我用的是TDM-GCC的安装包,省事。

TIPS:想用C++17之后的看这里

在这里下载MinGW-w64
MinGW-w64-x86_64-8.1.0-release-posix-sjlj
下载好后解压到个地方,注意不要有中文路径,
例如”D:\Software\GCC\”,
然后,将gcc下的bin路径加到环境变量里
就像这样
20190503230443.png
打开命令提示符看看。
20190503230626.png
这样就可以了。

现在进入正题。

重点还是配置这几个文件

1
2
3
4
c_cpp_properties.json
launch.json
tasks.json
settings.json

这几个文件位于工作目录的.vscode文件夹里

没有的话运行一下就有了

首先是c_cpp_properties.json,

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
//将'D:/Software/GCC/'替换为安装目录即可
{
"configurations": [
{
"name": "Win32",
"intelliSenseMode": "clang-x64",
"includePath": [
"${workspaceFolder}",
"D:/Software/GCC/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++",
"D:/Software/GCC/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/x86_64-w64-mingw32",
"D:/Software/GCC/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/backward",
"D:/Software/GCC/lib/gcc/x86_64-w64-mingw32/5.1.0/include",
"D:/Software/GCC/x86_64-w64-mingw32/include",
"D:/Software/GCC/lib/gcc/x86_64-w64-mingw32/5.1.0/include-fixed"
],
"defines": [
"_DEBUG",
"UNICODE",
"__GNUC__=7",
"__cdecl=__attribute__((__cdecl__))"
],
"browse": {
"path": [
"${workspaceFolder}",
"D:/Software/GCC/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++",
"D:/Software/GCC/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/x86_64-w64-mingw32",
"D:/Software/GCC/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/backward",
"D:/Software/GCC/lib/gcc/x86_64-w64-mingw32/5.1.0/include",
"D:/Software/GCC/x86_64-w64-mingw32/include",
"D:/Software/GCC/lib/gcc/x86_64-w64-mingw32/5.1.0/include-fixed"
]
},
"limitSymbolsToIncludedHeaders": true,
"databaseFilename": ""
}
],
"version": 4
}

settings.json

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
41
42
43
44
45
46
47
48
49
50
51
{
"window.zoomLevel": 0,
"files.autoGuessEncoding": true,

"editor.snippetSuggestions": "top",
"editor.minimap.enabled": true,
"editor.minimap.renderCharacters": false,
"editor.formatOnPaste": true,
"editor.formatOnType": true,
"editor.wordWrap": "on",

"C_Cpp.clang_format_sortIncludes": true,
"[cpp]": {
"editor.quickSuggestions": true
},
"[c]": {
"editor.quickSuggestions": true
},
"problems.decorations.enabled": true,
"C_Cpp.intelliSenseEngineFallback": "Enabled",
"files.associations": {
"*.cfg": "ini",
"*.fsh": "glsl",
"stack": "cpp",
"iostream": "cpp",
"ostream": "cpp",
"*.tcc": "cpp",
"cctype": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"exception": "cpp",
"initializer_list": "cpp",
"iosfwd": "cpp",
"istream": "cpp",
"limits": "cpp",
"new": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"system_error": "cpp",
"type_traits": "cpp",
"typeinfo": "cpp",
"utility": "cpp"
},
"terminal.integrated.fontSize": 18, //终端字体大小
"editor.fontSize": 20,//编辑器字体大小
}

task.json

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
41
42
43
44
45
46
47
48
49
{
"version": "2.0.0",
"tasks": [
{
"label": "Compile",
"command": "g++",
"args": [
"-g",
"-pthread",
"--std=c++17",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}.exe",
"-ggdb3", // 生成和调试有关的信息
"-Wall", // 开启额外警告
"-static-libgcc", // 静态链接
"-Wno-format",
"-fexec-charset=GBK", //Console窗体输出字符编码 保证能正常显示中文
"-finput-charset=UTF-8" //输入编译器文本编码 默认为UTF-8
],
"type": "shell",
"group": {
"kind": "build",
"isDefault": true
},
"presentation": {
"echo": true,
"reveal": "always", // 在“终端”中显示编译信息的策略,可以为always,silent,never
"focus": false,
"panel": "shared" // 不同的文件的编译信息共享一个终端面板
},
"problemMatcher": {
"owner": "cpp",
"fileLocation": [
"relative",
"\\"
],
"pattern": {
"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
}
}
]
}

最后是启动脚本launch.json,说实话这个最头疼,因为按官方配置的话是一运行闪退,

我又不想加system(“PAUSE”),最后和Notepad++一样,改了cmd命令(笑)

脚本如下:

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
{
"version": "0.2.0",
"configurations": [
{
"name": "GDB",
"type": "cppvsdbg",
"request": "launch",
"program": "cmd",
"args": [
"/C",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"&",
"pause" //这样就可以出现"请按任意键继续. . ."了
],
"stopAtEntry": false,
"cwd": "${workspaceRoot}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "D:/Software/GCC/bin/gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": false,
}
],
"preLaunchTask": "Compile"
},
]
}