Configure Visual Studio Code for Microsoft C++
Get Started with C++ and Mingw-w64 in Visual Studio Code
C++ programming with Visual Studio Code
第一个Get Started with C++ and Mingw-w64 in Visual Studio Code内就是介绍了,先安装扩展,接着就可以安装mingw
1、扩展
2、MSYS2
MSYS2安装好MSYS2以后,就可以使用命令行安装mingw
3、安装mingw-w
安装:pacman -S --needed base-devel mingw-w64-x86_64-toolchain
选择这直接默认就好,也就是直接按Enter回车
10696@DESKTOP-9L473HP UCRT64 ~
$ pacman -S --needed base-devel mingw-w64-x86_64-toolchain
:: There are 19 members in group mingw-w64-x86_64-toolchain:
:: Repository mingw64
1) mingw-w64-x86_64-binutils 2) mingw-w64-x86_64-crt-git 3) mingw-w64-x86_64-gcc
4) mingw-w64-x86_64-gcc-ada 5) mingw-w64-x86_64-gcc-fortran
6) mingw-w64-x86_64-gcc-libgfortran 7) mingw-w64-x86_64-gcc-libs 8) mingw-w64-x86_64-gcc-objc
9) mingw-w64-x86_64-gdb 10) mingw-w64-x86_64-gdb-multiarch 11) mingw-w64-x86_64-headers-git
12) mingw-w64-x86_64-libgccjit 13) mingw-w64-x86_64-libmangle-git
14) mingw-w64-x86_64-libwinpthread-git 15) mingw-w64-x86_64-make 16) mingw-w64-x86_64-pkgconf
17) mingw-w64-x86_64-tools-git 18) mingw-w64-x86_64-winpthreads-git
19) mingw-w64-x86_64-winstorecompat-gitEnter a selection (default=all):
resolving dependencies...
looking for conflicting packages...
4、配置环境变量
win10的配置页面即可配置
配置系统环境变量,双击PATH即可的,最下面两行
5、验证
Windows+R输入cmd运行,然后gcc --version和g++ --version就可以验证的
6、写codes
7、产生相应的配置档案
共三个的呢,.vscode/tasks.json、.vscode/launch.json、.vscode/settings.json
.vscode/tasks.json,编译运行使用的,两个路径的呢cwd和command
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "C:\\ruanjian\\msys64\\mingw64\\bin\\g++.exe", //g++软件的地址
"args": [ //g++的参数
"-fdiagnostics-color=always",
"-g", //g++指定debug
"${file}", //编译的cpp地址
"-o", //重命名可执行文件a.out
"${fileDirname}\\${fileBasenameNoExtension}.exe" //输出的名字
],
"options": {
"cwd": "${fileDirname}" //到达当前文件的目录
},
"problemMatcher": ["$gcc"],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
${fileDirname}是当前打开的cpp文件所在的目录
${fileBasenameNoExtension}是当前打开的cpp文件去掉后缀以后名字,像6.cpp,就是6
.vscode/launch.json,两个路径的呢cwd和miDebuggerPath
下面的"name": "C/C++ Runner: myself Debug Session", //我自己配置的debug配置,才是我自己配置的,另外一个"name": "C/C++ Runner: Debug Session",是自动产生的。
{
"version": "0.2.0",
"configurations": [
{
"name": "C/C++ Runner: myself Debug Session", //我自己配置的debug配置
"type": "cppdbg",
"request": "launch",
"args": [],
"stopAtEntry": false,
"externalConsole": true,
"cwd": "${fileDirname}", //到达当前文件的目录
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe", //debug的可执行文件
"MIMode": "gdb",
"miDebuggerPath": "gdb", //由于gdb.exe的地址已经加入到了环境变量,所以不需要指定地址,当然,没有加入就需要指定
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
},
{
"name": "C/C++ Runner: Debug Session",
"type": "cppdbg",
"request": "launch",
"args": [],
"stopAtEntry": false,
"externalConsole": true,
"cwd": "c:/Users/10696/Desktop/PAT_Leetcode/PAT/binarytree",
"program": "c:/Users/10696/Desktop/PAT_Leetcode/PAT/binarytree/build/Debug/outDebug",
"MIMode": "gdb",
"miDebuggerPath": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
.vscode/settings.json
{
"files.associations": {
"*.py": "python",
"iostream": "cpp",
"deque": "cpp",
"string": "cpp",
"vector": "cpp",
"random": "cpp",
"ratio": "cpp",
"ostream": "cpp",
"*.tcc": "cpp",
"string_view": "cpp",
"regex": "cpp",
"algorithm": "cpp",
"initializer_list": "cpp",
"list": "cpp",
"xhash": "cpp",
"xstring": "cpp",
"xtree": "cpp",
"xutility": "cpp",
"map": "cpp",
"unordered_map": "cpp",
"cmath": "cpp",
"queue": "cpp",
"stack": "cpp",
"type_traits": "cpp",
"xlocale": "cpp",
"atomic": "cpp",
"bit": "cpp",
"cctype": "cpp",
"cfenv": "cpp",
"clocale": "cpp",
"compare": "cpp",
"concepts": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"exception": "cpp",
"ios": "cpp",
"iosfwd": "cpp",
"istream": "cpp",
"iterator": "cpp",
"limits": "cpp",
"memory": "cpp",
"new": "cpp",
"set": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"typeinfo": "cpp",
"unordered_set": "cpp",
"utility": "cpp",
"xfacet": "cpp",
"xiosbase": "cpp",
"xlocinfo": "cpp",
"xlocnum": "cpp",
"xmemory": "cpp",
"xstddef": "cpp",
"xtr1common": "cpp",
"array": "cpp",
"bitset": "cpp",
"charconv": "cpp",
"chrono": "cpp",
"cinttypes": "cpp",
"codecvt": "cpp",
"complex": "cpp",
"condition_variable": "cpp",
"csetjmp": "cpp",
"csignal": "cpp",
"cstdarg": "cpp",
"cuchar": "cpp",
"cwctype": "cpp",
"forward_list": "cpp",
"functional": "cpp",
"memory_resource": "cpp",
"numeric": "cpp",
"optional": "cpp",
"fstream": "cpp",
"future": "cpp",
"iomanip": "cpp",
"mutex": "cpp",
"scoped_allocator": "cpp",
"shared_mutex": "cpp",
"sstream": "cpp",
"thread": "cpp",
"typeindex": "cpp",
"valarray": "cpp",
"ranges": "cpp",
"span": "cpp"
},
"C_Cpp_Runner.msvcBatchPath": "",
"C_Cpp_Runner.cCompilerPath": "gcc",
"C_Cpp_Runner.cppCompilerPath": "g++",
"C_Cpp_Runner.debuggerPath": "gdb",
"C_Cpp_Runner.cStandard": "",
"C_Cpp_Runner.cppStandard": "",
"C_Cpp_Runner.useMsvc": false,
"C_Cpp_Runner.warnings": [
"-Wall",
"-Wextra",
"-Wpedantic",
"-Wshadow",
"-Wformat=2",
"-Wcast-align",
"-Wconversion",
"-Wsign-conversion",
"-Wnull-dereference"
],
"C_Cpp_Runner.msvcWarnings": [
"/W4",
"/permissive-",
"/w14242",
"/w14287",
"/w14296",
"/w14311",
"/w14826",
"/w44062",
"/w44242",
"/w14905",
"/w14906",
"/w14263",
"/w44265",
"/w14928"
],
"C_Cpp_Runner.enableWarnings": true,
"C_Cpp_Runner.warningsAsError": false,
"C_Cpp_Runner.compilerArgs": [],
"C_Cpp_Runner.linkerArgs": [],
"C_Cpp_Runner.includePaths": [],
"C_Cpp_Runner.includeSearch": [
"*",
"**/*"
],
"C_Cpp_Runner.excludeSearch": [
"**/build",
"**/build/**",
"**/.*",
"**/.*/**",
"**/.vscode",
"**/.vscode/**"
],
"C_Cpp_Runner.useAddressSanitizer": false,
"C_Cpp_Runner.useUndefinedSanitizer": false,
"C_Cpp_Runner.useLeakSanitizer": false,
"C_Cpp_Runner.showCompilationTime": false,
"C_Cpp_Runner.useLinkTimeOptimization": false,
"C_Cpp_Runner.msvcSecureNoWarnings": false
}
8、编译
编译的时候要选择相应的配置档案编译和debug,也就是.vscode/tasks.json配置中的内容
9、编译和debug的路径
编译:vscode上方:Terminal/Run Build Task
编译+运行:vscode上方:Run/Run without debugging
编译+debug:vscode上方:Run/Start debugging
10、debug的选项
再编译以后,产生了exe文件,此时指定debug时的配置,也就是选择C/C++ Runner: myself...,也就是之前的配置.vscode/launch.json文件中,"name": "C/C++ Runner: myself Debug Session", //我自己配置的debug配置,name匹配的配置,否则debug会出错。
也就是:编译+运行:vscode上方:Run/Run without debugging之前,以及:编译+debug:vscode上方:Run/Start debugging之前,都需要事先选择相应的配置文件