相信我,比小白更小白的 VScode C++ 环境搭建

本文首发于 我的博客

在网上,总有些教程告诉你应该怎么做

但我发现这些教程都非常不友好,总会出现一些奇怪的问题

然后悻悻地复制「preLaunchTask“Compile”已终止,退出代码为 1」给大佬试图解决

(来自某小白两年前的心酸)

这是能保证让你配置无脑且不出问题的、绝对简单的一篇博文

Trust me.

准备工作

首先装好 VScode,相信没什么问题,装好 C++插件(搜索出来第一个就是)和中文插件 如果喜欢英文我也不拦你

然后点击 这个链接

下拉,在最新版本号下找到「x86_64-posix-seh」

鉴于国内网络问题(懂得都懂),直接下载这个免安装版本

创建一个你要放代码文件的文件夹,比如我的叫做「C++」

将下载后的文件解压到这个文件夹里,点进文件夹

再分别点进 mingw64、bin

然后复制如图中所选的位置(你的肯定和我显示的不一样)

然后快捷键 「win+q」,输入 path,弹出「编辑系统环境变量」

点进去,按照图片步骤点击

在第二张图所示位置中,点击新建,把你刚刚复制的粘贴进去

一路确定,这样你就完成了 mingw64 编译器和调试器的环境配置

试验一下,「win+R」输入「powershell」,回车,在弹出的框框中输入「g++ -v」

弹出如下

Using built-in specs.
COLLECT_GCC=C:\Users\26070\Documents\C++\mingw64\bin\g++.exe
COLLECT_LTO_WRAPPER=C:/Users/26070/Documents/C++/mingw64/bin/../libexec/gcc/x86_64-w64-mingw32/8.1.0/lto-wrapper.exe
Target: x86_64-w64-mingw32
Configured with: ../../../src/gcc-8.1.0/configure --host=x86_64-w64-mingw32 --build=x86_64-w64-mingw32 --target=x86_64-w64-mingw32 --prefix=/mingw64 --with-sysroot=/c/mingw810/x86_64-810-posix-seh-rt_v6-rev0/mingw64 --enable-shared --enable-static --disable-multilib --enable-languages=c,c++,fortran,lto --enable-libstdcxx-time=yes --enable-threads=posix --enable-libgomp --enable-libatomic --enable-lto --enable-graphite --enable-checking=release --enable-fully-dynamic-string --enable-version-specific-runtime-libs --disable-libstdcxx-pch --disable-libstdcxx-debug --enable-bootstrap --disable-rpath --disable-win32-registry --disable-nls --disable-werror --disable-symvers --with-gnu-as --with-gnu-ld --with-arch=nocona --with-tune=core2 --with-libiconv --with-system-zlib --with-gmp=/c/mingw810/prerequisites/x86_64-w64-mingw32-static --with-mpfr=/c/mingw810/prerequisites/x86_64-w64-mingw32-static --with-mpc=/c/mingw810/prerequisites/x86_64-w64-mingw32-static --with-isl=/c/mingw810/prerequisites/x86_64-w64-mingw32-static --with-pkgversion='x86_64-posix-seh-rev0, Built by MinGW-W64 project' --with-bugurl=https://sourceforge.net/projects/mingw-w64 CFLAGS='-O2 -pipe -fno-ident -I/c/mingw810/x86_64-810-posix-seh-rt_v6-rev0/mingw64/opt/include -I/c/mingw810/prerequisites/x86_64-zlib-static/include -I/c/mingw810/prerequisites/x86_64-w64-mingw32-static/include' CXXFLAGS='-O2 -pipe -fno-ident -I/c/mingw810/x86_64-810-posix-seh-rt_v6-rev0/mingw64/opt/include -I/c/mingw810/prerequisites/x86_64-zlib-static/include -I/c/mingw810/prerequisites/x86_64-w64-mingw32-static/include' CPPFLAGS=' -I/c/mingw810/x86_64-810-posix-seh-rt_v6-rev0/mingw64/opt/include -I/c/mingw810/prerequisites/x86_64-zlib-static/include -I/c/mingw810/prerequisites/x86_64-w64-mingw32-static/include' LDFLAGS='-pipe -fno-ident -L/c/mingw810/x86_64-810-posix-seh-rt_v6-rev0/mingw64/opt/lib -L/c/mingw810/prerequisites/x86_64-zlib-static/lib -L/c/mingw810/prerequisites/x86_64-w64-mingw32-static/lib '
Thread model: posix
gcc version 8.1.0 (x86_64-posix-seh-rev0, Built by MinGW-W64 project)

只需要注意最后一行就行了,如果显示了版本号,即「gcc version…」

那么恭喜你,搭建的环境是没问题的

如果弹出不符,那么请回头重新来吧

json 文件

首先记好打开命令面板的快捷键「ctrl+shift+P」,后面我们会经常用到

用 VScode 打开你所要存放代码的文件夹,用「ctrl+`(在键盘字母 Q 的上面的 1 的左边)」打开终端,输入如下内容

mkdir projects
cd projects
mkdir helloworld
cd helloworld
code .

注意最后一行还有回车

VScode 会弹出一个新窗口

在这个新窗口中新建一个 cpp 文件

比如我创建的是 「1.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;
}

保存,或是自动保存

点击 F5,弹出如下图

分别选择如图所示的选项

一通运行后,生成了「.vscode」文件夹,包含「tasks.json」及「launch.json」两个文件

接下来是「c_cpp_properties.json」

打开命令面板,输入 「c/c++」,如图所示

果断第一个(后面带括号 UI 的那个),看到如下页面

我们要改这么几个部分,如图所示

具体个人喜爱自己选择

以上,json 文件全部配置完毕,每次打完代码后直接 F5 即可

也有人说 code-runner,鉴于 C++ 属于编译语言,个人觉得并不实用

如果你想调试,打个断点,具体操作不细讲

至于还有一个 settings.json,个人觉得无用,如果需要可以参考其他文章

本文参考 官方doc

以上

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值