Vscode配置c/c++环境

下载vscode

下载vscode

首先登录vscode官方网站,下载windows版本下的软件,之后将可执行文件运行安装vscode该软件,由于该步骤比较简单所以我不展示相关的操作步骤,如果觉得有问题的话可以自行的1百度或者使用vscode自带的文档解决。

安装c/c++扩展

打开vscode这个软件,并且按照下面的步骤搜索c/c++,并进行安装
在这里插入图片描述

下载MSYS2软件并且更新

有能力的可以自行阅读vscode的官方文档进行配置,也可以通过我下面的操作帮助大家进行MSYS2软件的使用。

下载MSYS2

下载链接,之后像之前安装vscode时安装该软件,如果C盘不是特别拥挤的话建议不要修改它的默认安装位置C:\msys64。安装之后点击电脑左下角的开始键,找到MSYS2 64bit可以看到它下面具有五个可执行文件的图标。
在这里插入图片描述

更新

这样代表着我们安装好了该软件之后,需要对该软件进行更新。

  • 打开上面的MSYS2 MinGW x64图标并且输入: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]
  • 运行完上面的命令之后,需要关闭该窗口,再打开MSYS2 MSYS该图标对应的可执行文件,首先运行:pacman -Syu,大概会出现下面的代码,跟着提示操作就可以:
$ 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 ...]
  • 运行完第二步之后,关闭该终端窗口,再使用第一步使用的MSYS2 MingGW x64可执行文件,分别执行下面两条指令安装gcc 和 make:
$ 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

若没有报错,那就是可行的,可以往下进行,不行的话可以在评论区留言或者自己对照前面的讲解看哪里处理问题。

使用MSYS2配置c/c++环境并测试是否可用

建立cpp文件

首先我们需要建立一个文件夹放我们的测试文件,就是在D盘下面建立一个projects文件夹,并再建立一个hello文件夹用于放置之后产生的.cpp文件。

cd d:
mkdir projects
cd projects
mkdir hello
cd hello
code .

输入上面的指令之后便可以进入Vscode,点击信任父文件夹,之后点击红圈建立一个hello.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 (auto word : msg)
    {
        cout << word << " ";
    }
    cout << endl;
}

建立c_cpp_properties.json文件

首先使用crtl+shift+p打开控制面板,搜索UI,然后点击c/c++ configurations(UI),并将里面的一些东西进行替换:
在这里插入图片描述
在这里插入图片描述

tasks.json文件夹

ctrl+shift+P搜索task点击Configure Default Build Task,之后选择**g++**然后就可以产生tasks.json文件了。

Debug or run the file

文件配置好之后点击右上角的三角符号,便可以运行该代码并输出:
在这里插入图片描述

中间过程中出现的问题

“cmd” 后面都是乱码

该问题出现的地方是,建立配置文件之后点击运行该文件,生成.exe文件的时候会产生该乱码。
解决方法就是找到设置->点击时间和语言。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
按照上述步骤并重启应该就可以解决该问题。

出现’cmd’ is not recognized as an internal or external command, operable program or batch file

在系统变量里找到PATH变量,把%SystemRoot%\system32添加进去(添加后记得重启)。
在这里插入图片描述

如果存在一些文件下载不了或者出现一下问题的情况大家可以在评论区留言。


版权声明:本文为weixin_45931455原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。