VSCode的C++开发环境配置
图文主要参考网页:VSCode Document Using GCC with MinGW
MSYS2 org website
配置前提条件
- 安装VSCode,建议从官网安装
- 打开VSCode,从extension: marketplace软件扩展的搜索框中输入C++,安装C/C++扩展
- 下载并安装MSYS2
a. 下载MSYS2
b. 运行MSYS2,系统要求windows7及以上系统
c. 选择软件安装位置,建议安装在非系统盘
d. 安装完成后,运行MSYS2 64bit
e. 更新MSYS2及其组件,使用命令:pacman -Syu。执行效果
$ pacman -Syu
:: Synchronizing package databases...
mingw32 805.0 KiB
mingw32.sig 438.0 B
mingw64 807.9 KiB
mingw64.sig 438.0 B
msys 289.3 KiB
msys.sig 438.0 B
:: Starting core system upgrade...
warning: terminate other MSYS2 programs before proceeding
resolving dependencies...
looking for conflicting packages...
Packages (6) bash-5.1.004-1 filesystem-2021.01-1
mintty-1~3.4.4-1 msys2-runtime-3.1.7-4
pacman-5.2.2-9 pacman-mirrors-20201208-1
Total Download Size: 11.05 MiB
Total Installed Size: 53.92 MiB
Net Upgrade Size: -1.24 MiB
:: Proceed with installation? [Y/n]
:: Retrieving packages...
bash-5.1.004-1-x86_64 2.3 MiB
filesystem-2021.01-1-any 33.2 KiB
mintty-1~3.4.4-1-x86_64 767.2 KiB
msys2-runtime-3.1.7-4-x86_64 2.6 MiB
pacman-mirrors-20201208-1-any 3.8 KiB
pacman-5.2.2-9-x86_64 5.4 MiB
(6/6) checking keys in keyring 100%
(6/6) checking package integrity 100%
(6/6) loading package files 100%
(6/6) checking for file conflicts 100%
(6/6) checking available disk space 100%
:: Processing package changes...
(1/6) upgrading bash 100%
(2/6) upgrading filesystem 100%
(3/6) upgrading mintty 100%
(4/6) upgrading msys2-runtime 100%
(5/6) upgrading pacman-mirrors 100%
(6/6) upgrading pacman 100%
:: To complete this update all MSYS2 processes including this terminal will be closed. Confirm to proceed [Y/n]
更新其他基础包,使用命令:pacman -Su
$ pacman -Su
:: Starting core system upgrade...
there is nothing to do
:: Starting full system upgrade...
resolving dependencies...
looking for conflicting packages...
Packages (20) base-2020.12-1 bsdtar-3.5.0-1
[... more packages listed ...]
Total Download Size: 12.82 MiB
Total Installed Size: 44.25 MiB
Net Upgrade Size: 3.01 MiB
:: Proceed with installation? [Y/n]
[... downloading and installation continues ...]
使用命令:pacman -S --needed base-devel mingw-w64-x86_64-toolchain,安装MinGW-w64
$ pacman -S --needed base-devel mingw-w64-x86_64-toolchain
warning: file-5.39-2 is up to date -- skipping
[... more warnings ...]
:: There are 48 members in group base-devel:
:: Repository msys
1) asciidoc 2) autoconf 3) autoconf2.13 4) autogen
[... more packages listed ...]
Enter a selection (default=all):
:: 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
[... more packages listed ...]
Enter a selection (default=all):
resolving dependencies...
looking for conflicting packages...
Packages (123) docbook-xml-4.5-2 docbook-xsl-1.79.2-1
[... more packages listed ...]
m4-1.4.18-2 make-4.3-1 man-db-2.9.3-1
mingw-w64-x86_64-binutils-2.35.1-3
mingw-w64-x86_64-crt-git-9.0.0.6090.ad98746a-1
mingw-w64-x86_64-gcc-10.2.0-6
mingw-w64-x86_64-gcc-ada-10.2.0-6
mingw-w64-x86_64-gcc-fortran-10.2.0-6
mingw-w64-x86_64-gcc-libgfortran-10.2.0-6
mingw-w64-x86_64-gcc-libs-10.2.0-6
mingw-w64-x86_64-gcc-objc-10.2.0-6
mingw-w64-x86_64-gdb-10.1-2
mingw-w64-x86_64-gdb-multiarch-10.1-2
[... more packages listed ...]
Total Download Size: 196.15 MiB
Total Installed Size: 1254.96 MiB
:: Proceed with installation? [Y/n]
[... downloading and installation continues ...]
到此,通过MSYS2安装MinGW-w64完成。
4. 将MinGW-w64的bin文件夹添加到系统路径path中参考博客并使用命令:g++ --version或gdb --version进行安装正常的验证
调试程序
创建文件夹
创建一个新的空文件夹用于存放VSCode的项目projects,在此文件夹下创建一个子文件夹,命名为helloworld,并用VSCode进行打开
注意:建议在完成基础环境配置后再安装Simplified Chinese插件
创建源代码文件
在VSCode中,在helloworld文件夹下创建C++文件,命名为helloworld.cpp
在cpp文件中,输入以下源代码:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
vector<string> msg {"Hello", "C++", "World", "from", "VS Code", "and the C++ extension!"};
for (const string& word : msg)
{
cout << word << " ";
}
cout << endl;
}
将文件保存(Ctrl+S),也可以在File(文件)选项卡下选择勾选“自动保存”选项。
tasks.json文件
从菜单栏选项卡的terminal(终端)选项卡下,选择Configuration Default Build Task,在出现的下图列表中,选择g++.exe build active file(g++.exe构建活动文件)。
完成该操作后,在 .vscode 文件夹下出现 task.json文件,并已经在editor中打开。task.json文件的内容应与以下代码类似:
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "C:/msys64/mingw64/bin/g++.exe", //更改为自己的g++.exe文件的绝对路径
"args": ["-g", "${file}", "-o", "${fileDirname}\\${fileBasenameNoExtension}.exe"],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": ["$gcc"],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: C:/msys64/mingw64/bin/g++.exe" //更改为自己的g++.exe文件的绝对路径
}
],
"version": "2.0.0"
}
参数解释请参考:Variable Reference
从菜单栏的Terminal(终端)选项卡下,选择Run build task进行exe程序的生成
launch.json
在菜单栏Run(运行)选项卡下,选择Add Configuration(添加配置),选择C++(GDB/LLDB),在之后的下拉列表中,选择g++.exe build and debug active file
注意:需要提前将MinGW-w64添加到系统环境变量的path中,否则会出现搜索不到g++.exe的情况
完成上述步骤,在.vscode文件夹下会有launch.json文件创建,并在editor中打开,文件内容与下面代码类似:
{
"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": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\msys64\\mingw64\\bin\\gdb.exe", //更改为自己的gdb.exe文件的绝对路径
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++.exe build active file"
}
]
}
完成该步骤,即可进行C++的程序调试
c_cpp_properties.json
使用快捷键Ctrl+Shift+P打开搜索栏,输入"C/C++“,选择命令:C/C++: Edit Configurations (UI) from the Command Palette。即打开Configuration的UI界面,在此界面上即可对相关属性properties做一定更改,相关更改会自动写入c_cpp_properties.json文件。新建的c_cpp_properties.json文件内容与下面代码类似:
{
"configurations": [
{
"name": "GCC",
"includePath": ["${workspaceFolder}/**"],
"defines": ["_DEBUG", "UNICODE", "_UNICODE"],
"windowsSdkVersion": "10.0.18362.0",
"compilerPath": "C:/msys64/mingw64/bin/g++.exe", //更改为自己的g++.exe文件的绝对路径
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "windows-gcc-x64"
}
],
"version": 4
}
注意 :建议直接在c_cpp_properties.json文件中进行代码更改,因为UI界面中存在部分下拉列表没有相应所需的选项的情况
到此为止,VSCode的C++基础环境配置完成