使用 MinGW 在 Windows 上使用 GNU
Windows GNU MinGW

> 
> 在 Windows 上安装 GNU 编译器集合（gcc）和其他 GNU 组件来启用 GNU Autotools。
> 
> 
> 


![](/data/attachment/album/202008/25/085619rr331p13shpt6htp.jpg)


如果你是一名使用 Windows 的黑客，你不需要专有应用来编译代码。借助 [Minimalist GNU for Windows](http://mingw.org)（MinGW）项目，你可以下载并安装 [GNU 编译器集合（GCC）](https://gcc.gnu.org/)以及其它几个基本的 GNU 组件，以在 Windows 计算机上启用 [GNU Autotools](https://opensource.com/article/19/7/introduction-gnu-autotools)。


### 安装 MinGW


安装 MinGW 的最简单方法是通过 mingw-get，它是一个图形用户界面 （GUI） 应用，可帮助你选择要安装哪些组件，并让它们保持最新。要运行它，请从项目主页[下载 mingw-get-setup.exe](https://osdn.net/projects/mingw/releases/)。像你安装其他 EXE 一样，在向导中单击完成安装。


![Installing mingw-get](/data/attachment/album/202008/25/085733vqztdgvhf3g4codt.jpg "Installing mingw-get")


### 在 Windows 上安装 GCC


目前为止，你只安装了一个程序，或者更准确地说，一个称为 mingw-get 的专用的*包管理器*。启动 mingw-get 选择要在计算机上安装的 MinGW 项目应用。


首先，从应用菜单中选择 mingw-get 启动它。


![Installing GCC with MinGW](/data/attachment/album/202008/25/085735rp72bybqy4zvz65t.jpg "Installing GCC with MinGW")


要安装 GCC，请单击 GCC 和 G++ 包来标记要安装 GNU C、C++ 编译器。要完成此过程，请从 mingw-get 窗口左上角的**安装**菜单中选择**应用更改**。


安装 GCC 后，你可以使用完整路径在 [PowerShell](https://opensource.com/article/19/8/variables-powershell) 中运行它：



```
PS&gt; C:\MinGW\bin\gcc.exe --version
gcc.exe (MinGW.org GCC Build-x) x.y.z
Copyright (C) 2019 Free Software Foundation, Inc.

```

### 在 Windows 上运行 Bash


虽然它自称 “minimalist”（最小化），但 MinGW 还提供一个可选的 [Bourne shell](https://en.wikipedia.org/wiki/Bourne_shell) 命令行解释器，称为 MSYS（它代表<ruby> 最小系统 <rt>  Minimal System </rt></ruby>）。它是微软的 `cmd.exe` 和 PowerShell 的替代方案，它默认是 Bash。除了是（自然而然的）最流行的 shell 之一外，Bash 在将开源应用移植到 Windows 平台时很有用，因为许多开源项目都假定了 [POSIX](https://opensource.com/article/19/7/what-posix-richard-stallman-explains) 环境。


你可以在 mingw-get GUI 或 PowerShell 内安装 MSYS：



```
PS> mingw-get install msys

```

要尝试 Bash，请使用完整路径启动它：



```
PS> C:\MinGW\msys/1.0/bin/bash.exe
bash.exe-$ echo $0
"C:\MinGW\msys/1.0/bin/bash.exe"

```

### 在 Windows 上设置路径


你可能不希望为要使用的每个命令输入完整路径。将包含新 GNU 可执行文件的目录添加到 Windows 中的路径中。需要添加两个可执行文件的根目录：一个用于 MinGW（包括 GCC 及其相关工具链），另一个用于 MSYS（包括 Bash、GNU 和 [BSD](https://opensource.com/article/19/3/netbsd-raspberry-pi) 项目中的许多常用工具）。


若要在 Windows 中修改环境，请单击应用菜单并输入 `env`。


![Edit your env](/data/attachment/album/202008/25/085736p9w4khvo93v93kdh.jpg "Edit your env")


这将打开“首选项”窗口。点击窗口底部附近的“环境变量”按钮。


在“环境变量”窗口中，双击底部面板中的“路径”选区。


在“编辑环境变量”窗口中，单击右侧的“新增”按钮。创建一个新条目 `C:\MinCW\msys\1.0\bin`，然后单击 “确定”。以相同的方式创建第二条 `C:\MinGW\bin`，然后单击 “确定”。


![Set your env](/data/attachment/album/202008/25/085737b6alynrhzv18dd47.jpg "Set your env")


在每个首选项窗口中接受这些更改。你可以重启计算机以确保所有应用都检测到新变量，或者只需重启 PowerShell 窗口。


从现在开始，你可以调用任何 MinGW 命令而不指定完整路径，因为完整路径位于 PowerShell 继承的 Windows 系统的 `%PATH%` 环境变量中。


### Hello world


你已经完成设置，因此可以对新的 MinGW 系统进行小测试。如果你是 [Vim](https://opensource.com/resources/what-vim) 用户，请启动它，然后输入下面的 “hello world” 代码：



```
#include <stdio.h>
#include <iostream>

using namespace std;

int main() {
  cout << "Hello open source." << endl;
  return 0;
}

```

将文件保存为 `hello.cpp`，然后使用 GCC 的 C++ 组件编译文件：



```
PS> gcc hello.cpp --output hello

```

最后，运行它：



```
PS> .\a.exe
Hello open source.
PS>

```

MinGW 的内容远不止我在这里所能介绍的。毕竟，MinGW 打开了一个完整的开源世界和定制代码的潜力，因此请充分利用它。对于更广阔的开源世界，你还可以[试试 Linux](https://opensource.com/article/19/7/ways-get-started-linux)。当所有的限制都被消除后，你会惊讶于可能的事情。但与此同时，请试试 MinGW，并享受 GNU 的自由。




---


via: <https://opensource.com/article/20/8/gnu-windows-mingw>


作者：[Seth Kenlon](https://opensource.com/users/seth) 选题：[lujun9972](https://github.com/lujun9972) 译者：[geekpi](https://github.com/geekpi) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
If you're a hacker running Windows, you don't need a proprietary application to compile code. With the [Minimalist GNU for Windows](http://mingw.org) (MinGW) project, you can download and install the [GNU Compiler Collection](https://gcc.gnu.org/) (GCC) along with several other essential GNU components to enable [GNU Autotools](https://opensource.com/article/19/7/introduction-gnu-autotools) on your Windows computer.

## Install MinGW

The easiest way to install MinGW is through mingw-get, a graphical user interface (GUI) application that helps you select which components to install and keep them up to date. To run it, [download mingw-get-setup.exe](https://osdn.net/projects/mingw/releases/) from the project's host. Install it as you would any other EXE file by clicking through the installation wizard to completion.

![Installing mingw-get Installing mingw-get](https://opensource.com/sites/default/files/uploads/mingw-install.jpg)

(Seth Kenlon, CC BY-SA 4.0)

## Install GCC on Windows

So far, you've only installed an installer—or more accurately, a dedicated *package manager* called mingw-get. Launch mingw-get to select which MinGW project applications you want to install on your computer.

First, select **mingw-get** from your application menu to launch it.

![Installing GCC with MinGW Installing GCC with MinGW](https://opensource.com/sites/default/files/uploads/mingw-packages.jpg)

(Seth Kenlon, CC BY-SA 4.0)

To install GCC, click the GCC and G++ package to mark GNU C and C++ compiler for installation. To complete the process, select **Apply Changes** from the **Installation** menu in the top-left corner of the mingw-get window.

Once GCC is installed, you can run it from [PowerShell](https://opensource.com/article/19/8/variables-powershell) using its full path:

```
PS> C:\MinGW\bin\gcc.exe --version
gcc.exe (MinGW.org GCC Build-x) x.y.z
Copyright (C) 2019 Free Software Foundation, Inc.
```


## Run Bash on Windows

While it calls itself "minimalist," MinGW also provides an optional [Bourne shell](https://en.wikipedia.org/wiki/Bourne_shell) command-line interpreter called MSYS (which stands for Minimal System). It's an alternative to Microsoft's `cmd.exe`

and PowerShell, and it defaults to Bash. Aside from being one of the (justifiably) most popular shells, Bash is useful when porting open source applications to the Windows platform because many open source projects assume a [POSIX](https://opensource.com/article/19/7/what-posix-richard-stallman-explains) environment.

You can install MSYS from the mingw-get GUI or from within PowerShell:

`PS> mingw-get install msys`


To try out Bash, launch it using its full path:

```
PS> C:\MinGW\msys/1.0/bin/bash.exe
bash.exe-$ echo $0
"C:\MinGW\msys/1.0/bin/bash.exe"
```


## Set the path on Windows


You probably don't want to have to type the full path for every command you want to use. Add the directory containing your new GNU executables to your path in Windows. There are two root directories of executables to add: one for MinGW (including GCC and its related toolchain) and another for MSYS (including Bash and many common tools from the GNU and [BSD](https://opensource.com/article/19/3/netbsd-raspberry-pi) projects).

To modify your environment in Windows, click on the application menu and type `env`

.

![Edit your env Edit your env](https://opensource.com/sites/default/files/uploads/mingw-env.jpg)

(Seth Kenlon, CC BY-SA 4.0)

A Preferences window will open; click the **Environment variables** button located near the bottom of the window.

In the **Environment variables** window, double-click the **Path** selection from the bottom panel.

In the **Edit Environment variables** window that appears, click the **New** button on the right. Create a new entry reading **C:\MinCW\msys\1.0\bin** and click **OK**. Create a second new entry the same way, this one reading **C:\MinGW\bin**, and click **OK**.

![Set your env Set your env](https://opensource.com/sites/default/files/uploads/mingw-env-set.jpg)

(Seth Kenlon, CC BY-SA 4.0)

Accept these changes in each Preferences window. You can reboot your computer to ensure the new variables are detected by all applications, or just relaunch your PowerShell window.

From now on, you can call any MinGW command without specifying the full path, because the full path is in the `%PATH%`

environment variable of your Windows system, which PowerShell inherits.

## Hello world

You're all set up now, so put your new MinGW system to a small test. If you're a [Vim](https://opensource.com/resources/what-vim) user, launch it, and enter this obligatory "hello world" code:

```
#include <stdio.h>
#include <iostream>
using namespace std;
int main() {
cout << "Hello open source." << endl;
return 0;
}
```


Save the file as `hello.cpp`

, then compile it with the C++ component of GCC:

`PS> gcc hello.cpp --output hello`


And, finally, run it:

```
PS> .\a.exe
Hello open source.
PS>
```


There's much more to MinGW than what I can cover here. After all, MinGW opens a whole world of open source and potential for custom code, so take advantage of it. For a wider world of open source, you can also [give Linux a try](https://opensource.com/article/19/7/ways-get-started-linux). You'll be amazed at what's possible when all limits are removed. But in the meantime, give MinGW a try and enjoy the freedom of the GNU.

## 1 Comment