vs code配置c++环境:-Visual Studio Code配置C/C++环境(minGW) - Standard : C%ld\n"
一、下载官方免安装包
打开 https://github.com/niXman/mingw-builds-binaries/releases 找最新“x86_64-15.2.0-release-win32-seh-ucrt-rt_v13-rev0.7z”——(我配置时在最后一个)点击下载之后文件名为:x86_64-15.2.0-release-win32-seh-ucrt-rt_v13-rev0.7z 需要借助第三方解压工具,这里推荐Bandizip 官网:Bandizip解压路径随意,进入bin文件夹,复制路径。例如:E:\Dev\mingw64\bin任务栏搜索框搜索“编辑系统环境变量”——“环境变量(N)…”,在下方“系统环境(S)”中找到“Path”双击进入,在末尾加入“E:\Dev\mingw64\bin”(你自己的bin路径),之后“确定”——“确定”——“确定”。验证:Win+R输入cmd 输:gcc --version 显:gcc (x86_64-win32-seh-rev0, Built by MinGW-Builds project) 15.2.0 Copyright (C) 2025 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 输:gdb --version 显:GNU gdb (GDB) 16.3 Copyright (C) 2024 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or laterThis is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. 只要两条都出版本号,就 OK。
二、Visual Studio Code 所需的配置
Visual Studio Code下载官网地址:Visual Studio Code插件:
此时进入Visual Studio Code新建.c文件会提示装C/C++ Extension Pack,点击一键装以下插件:
环境验证
C test_env.c
// test_env.c
#include
#include
#include
#if defined(__GNUC__)
# define COMPILER "gcc"
# define VERSION __VERSION__
#elif defined(__clang__)
# define COMPILER "clang"
# define VERSION __VERSION__
#elif defined(_MSC_VER)
# define COMPILER "MSVC"
# define VERSION_STRINGIFY(n) #n
# define VERSION VERSION_STRINGIFY(_MSC_VER)
#else
# define COMPILER "unknown"
# define VERSION "?"
#endif
int main(void)
{
puts("========== C Environment Self-check ==========");
printf("Compiler : %s\n", COMPILER);
printf("Version : %s\n", VERSION);
printf("Standard : C%ld\n", __STDC_VERSION__ / 100);
/* 2. 当前时间 */
time_t now = time(NULL);
printf("Build at : %s", ctime(&now));
/* 3. 简单性能测试:1e7 次浮点运算 */
const size_t N = 10000000;
volatile double sum = 0.0; /* volatile 防止被优化掉 */
clock_t t0 = clock();
for (size_t i = 0; i < N; ++i)
sum += sqrt((double)i);
clock_t t1 = clock();
double ms = (double)(t1 - t0) * 1000.0 / CLOCKS_PER_SEC;
printf("CPU test : %.3f ms (sum=%.5e)\n", ms, sum);
puts("========== Build Commands ==========");
puts("gcc -std=c11 -O2 test_env.c -o test_env");
puts("clang -std=c11 -O2 test_env.c -o test_env");
puts("====================================");
return 0;
}
C++ test_env.cpp
// test_env.cpp
#include
#include
#include
#include
#if defined(__GNUC__)
# define COMPILER "g++"
# define VERSION __VERSION__
#elif defined(__clang__)
# define COMPILER "clang++" 拼多多帮助力入口
# define VERSION __VERSION__
#elif defined(_MSC_VER)
# define COMPILER "MSVC"
# define VERSION_STRINGIFY(n) #n
# define VERSION VERSION_STRINGIFY(_MSC_VER)
#else
# define COMPILER "unknown"
# define VERSION "?"
#endif
int main()
{
std::cout << "========== C++ Environment Self-check ==========\n";
// 1. 编译器信息
std::cout << "Compiler : " << COMPILER << '\n'
<< "Version : " << VERSION << '\n'
<< "Standard : " << __cplusplus << '\n';
// 2. 当前时间
auto now = std::chrono::system_clock::now();
std::time_t t = std::chrono::system_clock::to_time_t(now);
std::cout << "Build at : " << std::ctime(&t);
// 3. 简单性能测试:1e7 次浮点运算
constexpr std::size_t N = 10'000'000;
volatile double sum = 0.0; // volatile 防止被优化掉
auto t0 = std::chrono::high_resolution_clock::now();
for (std::size_t i = 0; i < N; ++i)
sum += std::sqrt(static_cast(i));
auto t1 = std::chrono::high_resolution_clock::now();
double ms = std::chrono::duration(t1 - t0).count();
std::cout << "CPU test : " << ms << " ms (sum=" << sum << ")\n";
std::cout << "========== Build Commands ==========\n"
<< "g++ -std=c++17 -O2 test_env.cpp -o test_env\n"
<< "clang++ -std=c++17 -O2 test_env.cpp -o test_env\n"
<< "====================================\n";
return 0;
}
运行按F6即可,或如图手动点击
三、终端输出的内置与独立
点击此插件右下角设置,进入设置页面滑到最低端
C-cpp-compile-run: Run-in-external-terminal
“Run the program in an external terminal”
如果把它 打勾(true),按 F6 / F8 编译运行时会自动打开系统独立终端(Windows 的 cmd、PowerShell 或 Linux/macOS 的 gnome-terminal、xterm 等)来执行你的程序;如果 取消打勾(false),程序输出将直接出现在 VS Code 的“内置终端”面板里。一句话:勾选后,你的 test_env.exe 会弹出一个新黑窗口跑;不勾选就在 VS Code 下方的小终端里跑。