本文主要记录在VSCode中配置C++环境,非常感谢参考文章中的博主。
首先需要安装 C++ 编译器。在 Windows 上,可以安装 MinGW 或者 Visual Studio;在 MacOS 上,可以安装 Clang 或者 Xcode;在 Linux 上,可以安装 GCC。本篇文章将介绍在 Windows系统中配置MinGW 。
官方下载:https://www.mingw-w64.org/downloads/
sourceforge下载:https://sourceforge.net/projects/mingw-w64/files/
(1)进入 sourceforge 网页,点击 x86_64-win32-seh 进行下载:
也可以点击这个链接直接安装MinGW.exe
:https://nuwen.net/mingw.html
解压该压缩包(x86_64-8.1.0-release-win32-seh-rt_v6-rev0.7z
),然后打开文件夹,找到MinGW -> bin,复制bin文件夹的路径。
接着,点击此电脑->属性->高级系统设置->环境变量->系统变量->path->新建->复制MinGW中的bin文件夹路径->点击确定。
验证MingGW是否配置成功,按Win+R,输入cmd,在控制台中输入g++ --version
。如图所示,表示配置成功。
VSCode中新建文件夹和1个c文件
这个时候,已经可以执行代码,会自动生成.vscode文件夹,里面是配置文件。
此时,.vscode文件夹中只有一个tasks.json
,在其他博客中能看到,还需要在该文件夹手动新建 c_cpp_properties.json
、launch.json
、settings.json
。
C++:项目文件树
复制以下内容到三个文件内
注意:需要手动修改有注释内容那一行的目录
1、c_cpp_properties.json
{
"configurations": [
{
"name": "Win64",
"includePath": ["${workspaceFolder}/**"],
"defines": ["_DEBUG", "UNICODE", "_UNICODE"],
"windowsSdkVersion": "10.0.18362.0",
"compilerPath": "D:\\software_package\\MinGW-win32\\mingw64\\bin\\g++.exe",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "gcc-x64"
}
],
"version": 4
}
2、 launch.json
{
"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:\\software_package\\MinGW-win32\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "task g++"
},
{
"name": "C/C++: g++.exe build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "D:\\software_package\\MinGW-win32\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Set Disassembly Flavor to Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: gcc.exe build active file"
}
]
}
3、settings.json
{
"files.associations": {
"*.py": "python",
"iostream": "cpp",
"*.tcc": "cpp",
"string": "cpp",
"unordered_map": "cpp",
"vector": "cpp",
"ostream": "cpp",
"new": "cpp",
"typeinfo": "cpp",
"deque": "cpp",
"initializer_list": "cpp",
"iosfwd": "cpp",
"fstream": "cpp",
"sstream": "cpp",
"map": "c",
"stdio.h": "c",
"algorithm": "cpp",
"atomic": "cpp",
"bit": "cpp",
"cctype": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"compare": "cpp",
"concepts": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"exception": "cpp",
"ios": "cpp",
"istream": "cpp",
"iterator": "cpp",
"limits": "cpp",
"memory": "cpp",
"random": "cpp",
"set": "cpp",
"stack": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"utility": "cpp",
"xfacet": "cpp",
"xiosbase": "cpp",
"xlocale": "cpp",
"xlocinfo": "cpp",
"xlocnum": "cpp",
"xmemory": "cpp",
"xstddef": "cpp",
"xstring": "cpp",
"xtr1common": "cpp",
"xtree": "cpp",
"xutility": "cpp",
"stdlib.h": "c",
"string.h": "c"
},
"editor.suggest.snippetsPreventQuickSuggestions": false,
"aiXcoder.showTrayIcon": true
}
4、tasks.json
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "task g++",
"command": "D:\\software_package\\MinGW-win32\\mingw64\\bin\\g++.exe", /*修改成自己bin目录下的g++.exe,这里的路径和电脑里复制的文件目录有一点不一样,这里是两个反斜杠\\*/
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"-I",
"D://Pytorch_CodeHub//C_and_C++//GNC-Tutorial", /*修改成自己放c/c++项目的文件夹,这里的路径和电脑里复制的文件目录有一点不一样,这里是两个反斜杠\\*/
"-std=c++17"
],
"options": {
"cwd": "D:\\software_package\\MinGW-win32\\mingw64\\bin" /*修改成自己bin目录,这里的路径和电脑里复制的文件目录有一点不一样,这里是两个反斜杠\\*/
},
"problemMatcher":[
"$gcc"
],
"group": "build",
}
]
}
为了更加方便的使用 VSCode 来编写 C++ 代码,将对其进行一些优化,实现以下功能:
解决中文乱码问题
打开.vscode 文件夹下的 tasks.json
文件,找到 “${fileDirname}\${fileBasenameNoExtension}.exe” 在后面加上英文 逗号 然后回车到下一行,粘贴下面文本 “-fexec-charset=GBK” 并保存
[1] vscode配置C/C++环境(超详细保姆级教学)
[2] VsCode安装和配置C++环境详细全流程
[3] VSCode 配置 C++ 环境
[4] VScode中配置C语言/C++运行环境