Win版 Visual Studio Code配置C++环境

记一次简单的VS Code C++编程环境配置

Using GCC with MinGWhttps://code.visualstudio.com/docs/cpp/config-mingw

一、下载并安装Visual Studio Code

Visual Studio Code - Code Editing. Redefinedhttps://code.visualstudio.com/

二、在安装完成的Visual Studio Code中安装C/C++插件
三、下载并安装MSYS2

MSYS2https://www.msys2.org/安装完成MSYS2后,打开一个MSYS2终端窗口,运行命令安装 MinGW-w64 toolchain

bash 复制代码
pacman -S --needed base-devel mingw-w64-ucrt-x86_64-toolchain

在按下Enter键确认安装包数后,输入Y确认继续安装

四、配置环境变量

安装完成 MinGW-w64 toolchain 后,将MinGW-w64文件夹路径添加到Window环境变量中:此电脑---右键---属性---高级系统设置---环境变量---系统变量---Path变量中编辑增加一条 C:\msys64\ucrt64\bin

**注意:**C:\msys64\ucrt64\bin为默认安装路径,环境变量配置错误可能会导致 #include 错误

五、检查MinGW-w64是否成功安装

在命令提示符中运行命令,检查是否成功安装

bash 复制代码
gcc --version
g++ --version
gdb --version
六、在Visual Studio Code中编写一个简单的C++程序
cpp 复制代码
#include <iostream>
using namespace std;
int main(){
    cout << "Hello world!" << endl;
    return 0;
}

**注意:**编写C++程序时应选择使用g++.exe构建和调试活动文件

使用gcc.exe构建和调试活动文件会出现 undefined reference to `std::cout' 以及如下报错弹窗

七、问题与解决

1.使用char存放汉字,输入输出使用cin和cout时,Visual Studio Code的终端无法输入中文字符

cpp 复制代码
#include <iostream>
using namespace std;
int main(){
    char text[8];
    cin >> text;
    cout << text << endl;
    return 0;
}

一种可能的解决方法:在exe中执行

cpp 复制代码
#include <iostream>
#include <windows.h>
using namespace std;
int main(){
    char text[8];
    cin >> text;
    cout << text << endl;
    system("pause");
    return 0;
}
相关推荐
blasit1 天前
笔记:Qt C++建立子线程做一个socket TCP常连接通信
c++·qt·tcp/ip
肆忆_2 天前
# 用 5 个问题学懂 C++ 虚函数(入门级)
c++
不想写代码的星星2 天前
虚函数表:C++ 多态背后的那个男人
c++
端平入洛4 天前
delete又未完全delete
c++
端平入洛5 天前
auto有时不auto
c++
哇哈哈20216 天前
信号量和信号
linux·c++
多恩Stone6 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
蜡笔小马6 天前
21.Boost.Geometry disjoint、distance、envelope、equals、expand和for_each算法接口详解
c++·算法·boost
超级大福宝6 天前
N皇后问题:经典回溯算法的一些分析
数据结构·c++·算法·leetcode
weiabc6 天前
printf(“%lf“, ys) 和 cout << ys 输出的浮点数格式存在细微差异
数据结构·c++·算法