在 Windows 上安装 MinGW

在 Windows 上安装 MinGW

在 Windows 系统上进行 C/C++ 开发需要安装合适的编译工具链。MinGW-w64 是一个流行的选择,它提供了 GCC 编译器工具链的 Windows 移植版本。

本教程将指导你如何通过 MSYS2 安装并配置 MinGW-w64,以便在 Windows 上使用 g++ 编译器进行 C++ 开发。🛠️

C/C++ development on Windows requires the installation of an appropriate compiler toolchain. MinGW-w64 is a popular choice, providing a Windows port of the GCC compiler toolchain.

This tutorial will guide you through installing and configuring MinGW-w64 via MSYS2, allowing you to use the g++ compiler for C++ development on Windows.🛠️

我们推荐使用 MSYS2 来安装和管理 MinGW-w64,因为它提供了一个方便的包管理器,使工具的安装和更新变得更加简单。📦

We recommend using MSYS2 to install and manage MinGW-w64, as it provides a convenient package manager that makes tool installation and updates much simpler.📦

步骤概览

下载并安装 MSYS2 - 获取包管理系统

更新 MSYS2 基础包 - 确保系统是最新的

通过 MSYS2 安装 MinGW-w64 GCC 工具链 - 包含 g++ 编译器

将 MinGW-w64 的 bin 目录添加到 Windows 的 PATH 环境变量 - 使命令可全局访问

验证安装 - 确认一切工作正常

Download and install MSYS2 - Get the package management system

Update MSYS2 base packages - Ensure the system is up-to-date

Install MinGW-w64 GCC toolchain via MSYS2 - Including the g++ compiler

Add MinGW-w64 bin directory to Windows PATH environment variable - Make commands globally accessible

Verify the installation - Confirm everything is working properly

详细步骤

1. 下载并安装 MSYS2

访问 MSYS2 官方网站

在首页找到 "Installation" 部分,下载最新的 .exe 安装程序

运行下载的安装程序,按照提示进行安装

建议保持默认的安装路径 (例如 C:\msys64)

安装完成后,勾选 "Run MSYS2 now" 并点击 "Finish"

Visit the MSYS2 official website

Find the "Installation" section on the homepage and download the latest .exe installer

Run the downloaded installer and follow the prompts

It's recommended to keep the default installation path (e.g., C:\msys64)

After installation completes, check "Run MSYS2 now" and click "Finish"

2. 更新 MSYS2 基础包

MSYS2 安装完成后会打开一个类似 Linux 终端的窗口 (MSYS2 MSYS)

在这个窗口中,首先更新包数据库和核心系统包。输入以下命令并按 Enter:

After MSYS2 installation completes, a Linux-like terminal window (MSYS2 MSYS) will open

In this window, first update the package database and core system packages. Enter the following command and press Enter:

pacman -Syu

它可能会提示你关闭窗口 (Terminate batch job (Y/N)?),按 Y 然后按 Enter。你需要手动关闭这个 MSYS2 窗口

从 Windows 开始菜单重新启动 MSYS2 (找到 "MSYS2 MSYS" 并运行)

再次运行更新命令,以确保所有基础包都是最新的:

It might prompt you to close the window (Terminate batch job (Y/N)?); press Y then Enter. You need to manually close this MSYS2 window

Restart MSYS2 from the Windows Start menu (find "MSYS2 MSYS" and run it)

Run the update command again to ensure all base packages are up to date:

pacman -Su

根据提示完成更新

Follow the prompts to complete the update

3. 安装 MinGW-w64 GCC 工具链

在仍然打开的 MSYS2 MSYS 窗口中,现在安装适用于 64 位 Windows 的 MinGW-w64 GCC 工具链。运行以下命令:

In the still open MSYS2 MSYS window, now install the MinGW-w64 GCC toolchain for 64-bit Windows. Run the following command:

pacman -S --needed base-devel mingw-w64-x86_64-toolchain

系统会列出将要安装的包 (包括 gcc, g++, make 等)

当询问选择时,直接按 Enter (选择全部)

当询问是否继续安装时,输入 Y 并按 Enter

等待安装过程完成

The system will list the packages to be installed (including gcc, g++, make, etc.)

When asked for selection, simply press Enter (to select all)

When asked if you want to continue installation, type Y and press Enter

Wait for the installation process to complete

4. 添加 MinGW-w64 到 Windows PATH

查找 bin 目录

找到 MinGW-w64 的 bin 目录:

默认安装路径通常为 C:\msys64\mingw64\bin

Locate the bin directory

Find the MinGW-w64 bin directory:

The default installation path is usually C:\msys64\mingw64\bin

添加到 PATH 环境变量

在 Windows 搜索栏中搜索 "环境变量",打开系统属性

点击 "高级" 选项卡下的 "环境变量" 按钮

在 "用户变量" 中选中 Path,然后点击 "编辑"

点击 "新建",将 MinGW-w64 bin 目录路径粘贴进去 (例如 C:\msys64\mingw64\bin)

点击 "确定" 关闭所有窗口

Add to PATH environment variable

Search for "environment variables" in the Windows search bar, open System Properties

Click the "Environment Variables" button under the "Advanced" tab

In "User variables," select Path and click "Edit"

Click "New" and paste the MinGW-w64 bin directory path (e.g., C:\msys64\mingw64\bin)

Click "OK" to close all windows

验证安装

关闭所有已经打开的 PowerShell 或 CMD 窗口

打开一个新的 PowerShell 或 CMD 窗口

输入以下命令检查 g++ 版本:

Close all open PowerShell or CMD windows

Open a new PowerShell or CMD window

Enter the following command to check the g++ version:

g++ --version

如果看到类似下面的版本信息输出,说明 g++ 安装成功并且 PATH 配置正确:

If you see version information output similar to the following, it means g++ has been successfully installed and the PATH is correctly configured:

g++ (Rev2, 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.

您还可以通过编译一个简单的 C++ 程序来进一步验证安装:

You can further verify the installation by compiling a simple C++ program:

1. 创建一个名为 hello.cpp 的文件,内容如下:

1. Create a file named hello.cpp with the following content:

#include

int main() {

std::cout << "Hello, world!" << std::endl;

return 0;

}

2. 在命令行中编译并运行这个程序:

2. Compile and run this program from the command line:

g++ hello.cpp -o hello

hello

如果看到输出 "Hello, world!",则表示您的 g++ 编译器工作正常。

If you see the output "Hello, world!", it indicates that your g++ compiler is working properly.

结语

恭喜!你已经成功在 Windows 系统上安装了 MinGW-w64 工具链,包括 g++ 编译器。现在你可以在 Windows 上便捷地进行 C/C++ 开发了。

这个设置不仅适用于小型项目,还可以用于更复杂的开发工作。🎉

Congratulations! You have successfully installed the MinGW-w64 toolchain, including the g++ compiler, on your Windows system. Now you can conveniently develop C/C++ applications on Windows.

This setup is suitable not only for small projects but also for more complex development work.🎉

如果你在安装过程中遇到任何问题,可以查阅 MSYS2 官方文档 或在下方留言,我会尽快回复。📚

If you encounter any issues during the installation process, you can refer to the MSYS2 official documentation or leave a comment below, and I will respond as soon as possible.📚

— HealthJian ✍️

相关内容

阴阳师怎么获得太鼓 阴阳师太鼓获取攻略大全
365体育投注账号被冻结

阴阳师怎么获得太鼓 阴阳师太鼓获取攻略大全

📅 06-30 👁️ 8654
剑侠世界起源开服时间表2025 新区开服时间
365体育投注账号被冻结

剑侠世界起源开服时间表2025 新区开服时间

📅 07-09 👁️ 2990
【平时为民 战时为兵】2025年民兵常识宣传普及
365体育投注账号被冻结

【平时为民 战时为兵】2025年民兵常识宣传普及

📅 09-26 👁️ 5629