首先登录vscode官方网站,下载windows版本下的软件,之后将可执行文件运行安装vscode该软件,由于该步骤比较简单所以我不展示相关的操作步骤,如果觉得有问题的话可以自行的1百度或者使用vscode自带的文档解决。
打开vscode这个软件,并且按照下面的步骤搜索c/c++,并进行安装
有能力的可以自行阅读vscode的官方文档进行配置,也可以通过我下面的操作帮助大家进行MSYS2软件的使用。
下载链接,之后像之前安装vscode时安装该软件,如果C盘不是特别拥挤的话建议不要修改它的默认安装位置C:\msys64。安装之后点击电脑左下角的开始键,找到MSYS2 64bit可以看到它下面具有五个可执行文件的图标。
这样代表着我们安装好了该软件之后,需要对该软件进行更新。
$ 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 -Syu
mingw32 is up to date
mingw64 is up to date
msys is up to date
:: 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
$ 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 ...]
$ pacman -S mingw-w64-x86_64-gcc
$ pacman -S make
运行完上面一步之后其实重要的部分已经完成,之后我们需要将mingw64的bin文件添加到系统变量里面,具体的1操作步骤:右键我的电脑–>找到属性并点击–>点击高级系统设置–>点击环境变量–>找到用户变量的PATH这一变量名并点击–>将mingw64的bin文件添加进去,比如我的bin文件目录就是C:\msys64\mingw64\bin
之后便可以测试c/c++的编译器是否可用,使用win+R输入powershell输入一下指令,查看是否可用:
g++ --version
gdb --version
若没有报错,那就是可行的,可以往下进行,不行的话可以在评论区留言或者自己对照前面的讲解看哪里处理问题。
首先我们需要建立一个文件夹放我们的测试文件,就是在D盘下面建立一个projects文件夹,并再建立一个hello文件夹用于放置之后产生的.cpp文件。
cd d:
mkdir projects
cd projects
mkdir hello
cd hello
code .
输入上面的指令之后便可以进入Vscode,点击信任父文件夹,之后点击红圈建立一个hello.cpp文件。
并且将下列代码写入文件。
#include
#include
#include
using namespace std;
int main()
{
vector<string> msg {"Hello", "C++", "World", "from", "VS Code", "and the C++ extension!"};
for (auto word : msg)
{
cout << word << " ";
}
cout << endl;
}
首先使用crtl+shift+p打开控制面板,搜索UI,然后点击c/c++ configurations(UI),并将里面的一些东西进行替换:
ctrl+shift+P搜索task点击Configure Default Build Task,之后选择**g++**然后就可以产生tasks.json文件了。
文件配置好之后点击右上角的三角符号,便可以运行该代码并输出:
该问题出现的地方是,建立配置文件之后点击运行该文件,生成.exe文件的时候会产生该乱码。
解决方法就是找到设置->点击时间和语言。
在系统变量里找到PATH变量,把%SystemRoot%\system32添加进去(添加后记得重启)。