1 安装 VSCode
直接去官网(https://code.visualstudio.com/)下载并安装即可。
2 配置C/C++编译环境
-
方案一
如果是在Windows,需要安装 MingW,可以去官网(https://sourceforge.net/projects/mingw-w64/)下载安装包。
注意安装路径不要出现中文。
打开 windows shell 界面,输入
mingw-get install gcc
、mingw-get install g++
。
如果安装的时候提示安装包被错误下载,可使用方案二安装。 -
方案二
进入网址https://sourceforge.net/projects/mingw-w64/files/,下滑,下载图中所示压缩包。
找一个无中文路径解压缩。
添加环境变量。在Path中加入解压后的文件夹中的bin目录,如C:\ProgramFiles\MinGW\mingw64\bin。
-
测试
在 windows shell 界面输入gcc -v
、g++ -v
,正确出现版本号则配置成功。
3 安装插件
搜索插件 C/C++ 和 Code Runner 并安装。
4 编辑配置文件
首先新建一个文件夹,并用 VSCode 打开。
在该文件夹中创建名为 .vscode 的文件夹。
在该文件夹中创建下面几个文件。以后的C/C++代码文件必须放在这个Code文件夹里,或者说有.vscode文件夹的文件夹里。
- c_cpp_properties.json
将 compilerPath 换成你的g++路径。{ "configurations": [ { "name": "Win64", "includePath": ["${workspaceFolder}/**"], "defines": ["_DEBUG", "UNICODE", "_UNICODE"], "windowsSdkVersion": "10.0.18362.0", "compilerPath": "C:\\MinGW\\bin\\g++.exe", "cStandard": "c17", "cppStandard": "c++17", "intelliSenseMode": "gcc-x64" } ], "version": 4 }
- launch.json
将 miDebuggerPath 换成你的gdb路径。{ "version": "0.2.0", "configurations": [ { "name": "(gdb) Launch", "type": "cppdbg", "request": "launch", "program": "${fileDirname}\\${fileBasenameNoExtension}.exe", "args": [], "stopAtEntry": false, "cwd": "${workspaceRoot}", "environment": [], "externalConsole": true, "MIMode": "gdb", "miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe", "preLaunchTask": "g++", "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ] } ] }
- settings.json
{ "json.schemaDownload.enable": false, "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 }
- tasks.json
{ "version": "2.0.0", "tasks": [ { "label": "g++", "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 } } ] }