Skip to content

C 和 C++运行环境安装指南

本指南旨在帮助您安装和配置 C/C++运行环境,包括 GCC、G++、GDB 等工具。

一、安装 Visual Studio Code (VSCode)

VSCode 是一款轻量级但强大的代码编辑器,支持众多编程语言。

下载 VSCode:访问 VSCode 官网 下载适用于您操作系统的安装程序。

VSCode安装程序示例

二、安装 VSCode C/C++插件

为了在 VSCode 中顺利开发 C/C++程序,您需要安装相关插件。

Windows 用户:安装 GCC and Mingw-w64 on Windows

macOS 用户:安装 Clang on macOS

VSCode C/C++插件示例

三、安装 MSYS2

MSYS2 是一个提供类 Unix 环境的软件,它使 Windows 用户能够运行 GCC、GDB 等工具。

下载 MSYS2

安装 MSYS2 并配置环境

  1. 安装 MSYS2 后,打开 MSYS2 终端。

  2. 输入并执行以下命令以安装必要的工具链:

    pacman -S --needed base-devel mingw-w64-ucrt-x86_64-toolchain
  3. 当系统提示时输入Y确认安装。

alt text

配置环境变量

C:\msys64\ucrt64\bin添加到您的系统环境变量中,以便在任何命令行界面中都能访问 GCC、GDB 等工具。

alt text

四、验证安装

  1. 打开 PowerShell 或任何命令行界面,输入以下命令以检查工具是否成功安装:
powershell
gcc --version
g++ --version
gdb --version
  1. 如果安装成功,您将看到如下所示的版本信息输出:
powershell
PS C:\Users\Administrator> gcc --version
gcc.exe (Rev3, Built by MSYS2 project) 13.2.0
Copyright (C) 2023 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.

PS C:\Users\Administrator> g++ --version
g++.exe (Rev3, Built by MSYS2 project) 13.2.0
Copyright (C) 2023 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.

PS C:\Users\Administrator> gdb --version
GNU gdb (GDB) 14.1
Copyright (C) 2023 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
PS C:\Users\Administrator>
  1. 恭喜您,现在您已经成功安装并配置了 C/C++开发环境!