在使用VSCode进行编写c++代码时,在配置上遇到了很多问题,最终解决,在这里纪录一下!
1、安装c++环境
安装MinGW32,配置环境变量
可参考这篇文章
本来想上传MinGW,发现zip我已经删除了…
2、配置VSCode 文件
配置四个.json文件
先创建一个你打算存放代码的文件夹(称作工作区),路径不能含有中文和空格和引号。c语言和c++需要建立不同的工作区(除非你懂得下面json文件的某些选项,则可以做到一个工作区使用不同的build task)。
打开VS Code,选打开文件夹(不要选“添加工作区文件夹”,理由见上一句),选择刚才那个文件夹,点VS Code上的新建文件夹,名称为.vscode(这样做的原因是Windows的Explorer不允许创建的文件夹第一个字符是点),然后创建 launch.json,tasks.json,settings.json,c_cpp_properties.json放到.vscode文件夹下,效果图:
launch.json代码:
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "g++.exe build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "D:\\developenvc\\devcpp\\Dev-Cpp\\MinGW32\\bin\\gdb.exe", // 此处根据自己电脑配置填写
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "g++.exe build active file"
}
]
}
tasks.json代码:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "g++.exe build active file",
"command": "g++.exe",
"args": [
"-g",
"-std=c++14",
"${file}",
"-Wall",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "D:\\developenvc\\devcpp\\Dev-Cpp\\MinGW32\\bin"
},
"problemMatcher": [
"$gcc"
]
}
]
}
如果使用MinGW,编译C用gcc,编译c++用g++,并把-target和-fcolor那两条删去。如果不想要额外警告,把-Wall那一条删去。
settings.json代码:
{
"files.associations": {
"typeinfo": "cpp",
"ostream": "cpp",
"iostream": "cpp",
"array": "cpp",
"*.tcc": "cpp",
"cctype": "cpp",
"clocale": "cpp",
"cstdarg": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"unordered_map": "cpp",
"vector": "cpp",
"exception": "cpp",
"fstream": "cpp",
"functional": "cpp",
"initializer_list": "cpp",
"iosfwd": "cpp",
"istream": "cpp",
"limits": "cpp",
"new": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"utility": "cpp",
"deque": "cpp",
"queue": "cpp",
"atomic": "cpp",
"cmath": "cpp",
"cstddef": "cpp",
"algorithm": "cpp",
"map": "cpp",
"memory": "cpp",
"memory_resource": "cpp",
"optional": "cpp",
"string": "cpp",
"string_view": "cpp",
"system_error": "cpp"
}
}
c_cpp_properties.json代码:
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "D:\\developenvc\\devcpp\\Dev-Cpp\\MinGW32\\bin\\g++.exe",
"cStandard": "c11",
"cppStandard": "c++14",
"intelliSenseMode": "clang-x64"
}
],
"version": 4
}
此文件内容来自于Microsoft/vscode-cpptools;这个json不允许有注释(其实按照标准本来就不能有)。
Windows下的路径为反斜杠,原本应使用两个反斜杠来转义,但直接用斜杠在VS Code中也接受。
关于VS Code输出会出现乱码,很多人都遇到过。这是因为VS Code内部用的是utf-8编码,cmd/powershell是gbk编码。直接编译,会把“你好”输出成“浣犲ソ”。
其实可以点右下角 选Reopen with Encoding 选Chinese GBK 就可以正常输入中文了,而且输出也是正常的
还可以在设置里搜索encode,把files.encoding改成gbk,而且最好把files.autoGuessEncoding也改成true