本文主要描述如何在Windows下使用MinGW配置VSCode以编写C++程序,如有错误,还望评论指正。
参考链接:官方教程,feynman1999的文章
下载并安装VSCode;
下载MinGW-w64,当然你也可以下载其他版本的MinGW;
打开VSCode,在扩展中搜索C++,安装官方(Microsoft)发布的C/C++扩展,之后重新加载窗口;
打开一个文件夹,路径不要包含中文。类似于Codeblocks或者其他重量级的IDE,VSCode虽然不需要(也不能)创建工程,但需要在一个文件夹下才能编译、调试C++代码。;
打开菜单栏 Terminal - Configure Default Build Tasks… 或使用快捷键Ctrl +shift + B选择create task.json,选择others,即会生成默认代码,将其修改为:
{
"tasks": [
{
"label": "compile", // 任务标识,下面launch.json里面会用到
"type": "shell",
"command": "g++",
"args": [
"-g",
"${file}",
"-o",
"${fileBasenameNoExtension}.exe",
"-ggdb3",
"-Wall",
"-static-libgcc",
"-std=c++14",
"-Wno-format",
"-finput-charset=UTF-8",
"-fexec-charset=UTF-8"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
打开菜单栏 Debug - Open Configuration 或者直接按F5,选择 C++(GDB/LLDB) 生成launch.json,将其修改为:
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}.exe", // 可执行文件
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false, // 是否使用外部控制台窗口
"MIMode": "gdb",
"miDebuggerPath": "D:/mingw64/bin/gdb.exe", // 你的gdb位置
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "compile" // 即是否在调试前编译,compile与task.josn中的label值一样
}
]
}
选择菜单栏 View - Command Palette 或使用快捷键Ctrl + shift + P打开命令面板,输入并选择C/Cpp的edit configuration,生成c_cpp_properties.json文件,修改内容为:
{
"configurations": [
{
"name": "Win32",
"includePath": [ // include路径,可以手动添加
"${workspaceFolder}/**",
"D:\\mingw64\\lib\\gcc\\x86_64-w64-mingw32\\8.1.0\\include/**"
],
"defines": [
"_DEBUG",
"UNICODE"
],
"compilerPath": "C:\\mingw64\\bin\\g++.exe", // 编译器路径
"cStandard": "c11",
"cppStandard": "c++14",
"intelliSenseMode": "clang-x64",
"browse": {
"path": [
"${workspaceFolder}"
],
"limitSymbolsToIncludedHeaders": true,
"databaseFilename": ""
}
}
],
"version": 4
}
上述三个文件配置完之后,就可以编译运行编写的C++代码了,文件架结构:
E:.
│ data.out
│ test.cpp
│ test.exe
│
└─.vscode
c_cpp_properties.json
launch.json
settings.json
tasks.json
问题小计: