00021-MSYS2 教程-windows10
前言
MSYS2 是一个适用于 Windows 的软件分发和构建平台。
MSYS2 是一系列工具和库,为您提供易于使用的环境,用于构建、安装和运行 Windows 软件。包含名为 mintty
的 bash
命令行终端、git
等版本控制系统、tar
等工具,这些工具是 Cygwin
的修改版本。MSYS2 为 GCC
、mingw-w64
、CPython
、CMake
、Meson
、OpenSSL
、FFmpeg
、Rust
、Ruby
等提供了最新的原生版本。
MSYS2 具有一个名为 Pacman
的软件包管理系统。
操作系统:Windows 10 专业版
参考文档
安装
关于如何安装 MSYS2
可以参考官方文档 MSYS2 或者 00020-GCC on Windows-windows10。
MSYS2 vs Cygwin
The unixy tools in MSYS2 are directly based on Cygwin, so there is some overlap there. While Cygwin focuses on building Unix software on Windows as is, MSYS2 focuses on building native software built against the Windows APIs.
Environments
MSYS2 具有不同的环境/子系统,首先,你必须决定使用哪一个。环境之间的差异主要是环境变量、默认编译器/链接器、体系结构和使用的系统库等。如果你不确定,请使用 MINGW64
。
MSYS 环境包含基础的 unix/cygwin 工具,它处于 /msys64/usr 目录,这些工具始终处于激活状态。所有其他环境都继承自 MSYS 环境,并在其上添加各种工具。
例如,在 MINGW64 环境中,环境变量 $PATH
为 /msys64/mingw64/bin:/msys64/usr/bin
,可以获得所有 mingw64 的工具以及所有 msys 工具。
Overview
Name | Prefix | Toolchain | Architecture | C Library | C++ Library | |
---|---|---|---|---|---|---|
MSYS | /usr |
gcc | x86_64 | cygwin | libstdc++ | |
MINGW64 | /mingw64 |
gcc | x86_64 | msvcrt | libstdc++ | |
UCRT64 | /ucrt64 |
gcc | x86_64 | ucrt | libstdc++ | |
CLANG64 | /clang64 |
llvm | x86_64 | ucrt | libc++ | |
MINGW32 | /mingw32 |
gcc | i686 | msvcrt | libstdc++ | |
CLANG32 | /clang32 |
llvm | i686 | ucrt | libc++ | |
CLANGARM64 | /clangarm64 |
llvm | aarch64 | ucrt | libc++ |
The active environment is selected via the MSYSTEM environment variable. Setting MSYSTEM to UCRT64 and starting a login shell will put you in that environment.
GCC vs LLVM/Clang
These are the default compilers/toolchains used for building all packages in the respective repositories.
基于 GCC 的环境:
-
目前已广泛测试/使用。
-
Fortran 支持。
-
虽然在 MINGW 环境中也存在一个 Clang 包,但该包仍然使用 GNU 链接器和 GNU C++ 库。在某些情况下,Clang 也用于构建软件包,如上游更喜欢 Clang 而不是 GCC。
基于 LLVM/Clang 的环境:
-
仅使用 LLVM 工具、LLD 作为链接器,LIBC++ 作为C++标准库。
-
Clang 提供 ASAN 支持。
-
对 TLS 的原生支持(Thread-local storage)。
-
LLD 比 LD 快,但不支持 LD 支持的所有特性。
-
一些工具与 GNU 工具不完全等效。
-
支持 Microsoft Windows 10 上的 ARM64/AArch64 架构。
MSVCRT vs UCRT
这些是 Microsoft Windows 上 C 标准库的两个变体。
默认情况下,MSVCRT(Microsoft Visual C++ Runtime)在所有 Microsoft Windows 版本上都可用,但由于向后兼容性问题,与 C99 不兼容并且缺少某些功能。
-
与 C99 不兼容。
-
mingw-w64 提供替换函数,在许多情况下兼容 C99。
-
不支持 UTF-8 locale。
-
与 MSVCRT 链接的二进制文件不应与 UCRT 二进制文件混合,因为内部结构和数据类型不同。(更严格地说,不应混合使用为不同目标构建的对象文件或静态库。为不同的 CRT 构建的 DLL 可以混合使用,只要它们不共享 CRT 对象。)相同的规则适用于 MSVC 编译的二进制文件,因为 MSVC 默认使用 UCRT(如果未更改)。
-
开箱即用,适用于每个 Microsoft Windows 版本。
UCRT(Universal C Runtime)是一个较新的版本,默认情况下也被 Microsoft Visual Studio 使用。它与 MSVC 效果相同。
-
在生成和运行时,与 MSVC 具有较好的兼容性。
-
默认情况下,它仅在 Windows 10 上提供,对于旧版本,您必须自己提供它。
Terminals
Mintty
Mintty 是 MSYS2 中的默认终端应用程序,包含在安装程序中。所有 MSYS2 环境有单独的启动器以及相应的.ini配置文件 (msys2{.exe,.ini}/mingw32{.exe,.ini}/mingw64{.exe,.ini}),因此可以轻松配置环境并将启动器固定到 Windows 任务栏。
Windows Terminal
如果你在电脑上安装了很多环境,就会发现有一大堆各种不同的命令行入口,管理起来十分不便。使用 Windows Terminal 可以将各种命令行界面入口整合到一起,界面也更加美观,还能改善部分命令行界面的复制粘贴体验。
Windows Terminal 可以直接在 win10 自带的微软商店里免费下载。而在 win11 则是默认的命令行程序(但是仍然推荐去商店更新一下,因为自带的版本比较古旧,甚至没有图形化的设置界面)。
我安装的是 Windows Terminal Preview。
Windows Terminal 默认情况下支持 cmd、powershell 和 WSL,也可以扩展支持 MSYS2 shell。
-
打开 Windows Terminal Preview,在选项卡下拉菜单中选择 “设置”,然后点选择 “打开 JSON 文件”,这将打开一个名为
settings.json
的 JSON 配置文件。 -
修改配置文件
profiles
键的代码。请注意,这些示例假定 MSYS2 安装在D:\lyf_computer_language\msys64
,Git for Windows 安装在D:\lyf_computer_language\Git
。
1 | "defaultProfile": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}", |
You can make one of the MSYS2 profiles the default by setting the defaultProfile key to the guid value of one of the profile entries.
Issues
如果 Windows PowerShell
报告 系统上禁止运行脚本
问题。可以参考 问题解决:系统上禁止运行脚本。
-
以
管理员权限
打开Windows PowerShell
,输入set-ExecutionPolicy RemoteSigned
,输入a
或者y
。 -
测试是否更改成功:输入
get-executionpolicy
,结果为RemoteSigned
表示成功。
Package Management
Package repositories
The MSYS2 software distribution uses a port of pacman
(known from Arch Linux) to manage (install, remove and update) binary packages and also to build those packages in the first place.
There are 6 package repositories, the “classical” ones msys2
, mingw32
, and mingw64
and the newer ucrt64
, clang32
, and clang64
.
The packages in msys2
are named just like on a Linux distribution, the packages in the others are prefixed by either mingw-w64-i686-
for 32-bit packages, or mingw-w64-x86_64-
for 64-bit packages with a secondary prefix clang
or ucrt
where applicable.
Finding a package
查找软件包1 | pacman -Ss <name or part of the name of the package> |
你能发现 mingw-w64-x86_64-openjpeg2
包已被安装,其他包没有被安装。
1 | pacman -Qs <name or part of the name of the package> |
Installing a package
安装包1 | pacman -S <name of the package> |
If the package has dependencies which are not installed,
pacman
will ask you whether you would like to install the dependencies in the first place.
pacman -S
also accepts virtual package names and package group names. Virtual package names can be often encountered with packages built from Git, e.g.msys2-launcher-git
can be installed by requestingmsys-launcher
. Package groups simplify installation of related packages, e.g. installbase-devel
to get basic development tools.Please note that neither of those are real packages, so the commands below won’t accept these names and you need to use the real package names instead.
Uninstalling a package
The following command will remove a package (but not its dependencies nor any files produced by running it):
1 | pacman -R <name of the package> |
Installing a specific version of a package or a stand-alone packages
Older (or pre-release) versions of packages can be installed directly from the package archive (
.tar.zst
or.tar.xz
). The data store for the repositories contains older versions of packages, but beware that you might need to recursively find correct versions of dependencies for the desired package. Once downloaded, the package can be installed like this:
1 | pacman -U <packagefile.tar.zst> |
1 | pacman -U <packagefile.tar.xz> |
Finding dependencies of a package
You can use pactree
to figure out which packages are needed to make a package working properly:
1 | lyf@DESKTOP-GV2QHKN MSYS ~ |
Alternatively you can use pacman -Qi
to get the list of direct
dependencies of a package:
1 | lyf@DESKTOP-GV2QHKN MSYS ~ |
Finding out which package a file belongs to
Use the following command to trace a file back to its owning package:
1 | pacman -Qo <full file path> |
Note that this operation only compares the file paths, so proper capitalization and the .exe
suffix (if applicable) is required. Also note that this works only on installed packages
, it will not scan the whole package repositories.
Finding which package will install the file you need
The two recommended tools that can scan a repository and find packages that contain specific files are pacman -F
and pkgfile
. Below are examples of pacman -F
usage:
Call pacman -Fy
to update your package database. To find an exact match
, call pacman -F <filename>
(don’t include the path in the filename). To find a substring match
, call pacman -Fx <filename>
.
Note that this operation only compares the file paths, so proper capitalization and the .exe
suffix (if applicable) is required.
Listing installed packages
1 | lyf@DESKTOP-GV2QHKN MSYS ~ |
Package Naming
Overview
不同环境的 the package prefix
- MINGW_PACKAGE_PREFIX
。
Name | Package Prefix | |
---|---|---|
MSYS | None |
|
MINGW64 | mingw-w64-x86_64- |
|
UCRT64 | mingw-w64-ucrt-x86_64- |
|
CLANG64 | mingw-w64-clang-x86_64- |
|
MINGW32 | mingw-w64-i686- |
|
CLANG32 | mingw-w64-clang-i686- |
|
CLANGARM64 | mingw-w64-clang-aarch64- |
Avoiding writing long package names
Use pacboy
to install mingw packages without having to type the long package names (install pacboy
first using pacman -S pactoys
if necessary). Examples:
-
pacboy
installs the listed packages for one or more environments. The selection of environments for each package is controlled byappending suffixes
on the package name. -
In particular, adding the
:p
suffix installs the package forthe current environment only
. -
If no suffix is given, the selection of environments depends on what environment you're currently in
- e.g. in a mingw64 environment, it currently defaults to installing the package for both mingw32 and mingw64.
1 | lyf@DESKTOP-GV2QHKN MSYS ~ |
Here are examples of using the :x
, :i
and :m
suffixes for installing packages for the mingw64
, mingw32
and both environments
:
1 | lyf@DESKTOP-GV2QHKN MSYS ~ |
Repositories and Mirrors
Active environments
The package manager uses only environments that are configured under /etc/pacman.conf
. You may add/uncomment/remove/comment those as needed:
1 | # always include msys! |
To launch an environment either use the wrapper executables like ucrt64.exe
or call msys2_shell.cmd
with either the matching parameter like msys2_shell.cmd -clang64
or by setting MSYSTEM
.
Tips and Tricks
Show the license information of a package
To list the license of an installed package you can run:
1 | pacman -Qi meson | grep '^Licenses' |
To list the license of a package in the sync database you can run:
1 | pacman -Si meson | grep '^Licenses' |
To list all license files installed by a package in the recommended location:
1 | pacman -Ql meson | grep -E "/share/licenses/.+/.+" |
Note that not every package includes the license text as a file, nor puts it in this specific recommended location.
Listing the content of a package
If you would like to know what has been installed as a part of a specific package use the following command:
1 | pacman -Ql <name of the package> |
Example:
1 | pacman -Ql mingw-w64-x86_64-pugixml |
1 | mingw-w64-x86_64-pugixml /mingw64/ |
As you can see the package contains:
-
a binary executable library file (libpugixml.dll),
-
a static library (libpugixml.dll.a),
-
2 header files (pugixml.hpp, pugiconfig.hpp),
-
2 cmake files,
-
and a PKGCONFIG file (pugixml.pc).
请注意上面的命令不能够在
Mintty
中输出想要的结果。可以使用Windows PowerShell
运行上面的命令。要在Windows PowerShell
运行上面的命令,需要配置环境变量,可以参考 00020-GCC on Windows-windows10
1 | lyf@DESKTOP-GV2QHKN MSYS ~ |
1 | 版权所有 (C) Microsoft Corporation。保留所有权利。 |
Using CMake in MSYS2
Installing
When building projects for Windows with CMake (as opposed to building projects that are going to run in MSYS2 posix emulation runtime) make sure to install the MinGW version of CMake, i.e. installing e.g. mingw-w64-x86_64-cmake
.
You also want to install a tool for actually doing the build. The current recommended default is Ninja, which you can install from the mingw-w64-x86_64-ninja
package.
Other alternatives are make
or mingw-w64-x86_64-make
, for building with GNU Make
running either as a MSYS2
or MinGW
process.
Building
When running the CMake configuration command, it’s recommended to explicitly specify the desired build file generator with the -G
option. MSYS2 provided CMake defaults to Ninja
(but this is not the default in upstream CMake, so it's safest to explicitly specify it
).
Thus, to configure and build a CMake based project, you can run the following commands:
1 | cmake -G Ninja <path-to-source> -DCMAKE_BUILD_TYPE=Release |
The relevant generator alternatives are:
-
-G Ninja
-
-G "MSYS Makefiles"
-
-G "MinGW Makefiles"
If building by invoking cmake --build
, the same command works for all generator choices. Alternatively, to build by directly invoking the build tool, you can call ninja
, make
or mingw32-make
respectively for those three alternatives.
MSYS2-Introduction
MSYS2
is software distribution and a building platform for Windows. It provides a Unix-like environment
, a command-line interface and a software repository making it easier to install, use, build and port software on Windows. That means Bash
, Autotools
, Make
, Git
, GCC
, GDB
…, all easily installable through Pacman
, a fully-featured package manager.
It is an independent rewrite of MSys, based on modern Cygwin (POSIX compatibility layer) and MinGW-w64 with the aim of better interoperability with native Windows software.
Both 32-bit and 64-bit variants exist and receive mostly the same level of support.
Subsystems
MSYS2 consists of three subsystems and their corresponding package repositories, msys2
, mingw32
, and mingw64
.
The mingw
subsystems provide native Windows programs and are the main focus of the project. These programs are built to co-operate well with other Windows programs, independently of the other subsystems. This part builds on the MinGW-w64 project.
The msys2
subsystem provides an emulated mostly-POSIX-compliant environment for building software, package management, and shell scripting. These programs live in a virtual single-root filesystem
(the root is the MSYS2 installation directory). Some effort is made to have the programs work well with native Windows programs, but it’s not seamless. This part builds on the Cygwin project.
Each of the subsystems provides its own native (i.e. target=host) compiler toolchain, in mingw-w64-i686-toolchain
, and mingw-w64-x86_64-toolchain
. There are also cross compiler toolchains with host={i686,x86_64}-pc-msys and target={i686,x86_64}-w64-mingw32 in mingw-w64-cross-toolchain
, but these are of limited use because there are no library packages for them
.
Shells
Every subsystem has an associated “shell”, which is essentially a set of environment variables that allow the subsystems to co-operate properly. These shells can be invoked using launchers in the MSYS2 installation directory or using the shortcuts in the Windows Start menu. The launchers set the MSYSTEM
variable and open a terminal window (mintty) with a proper shell (bash). Bash in turn sources /etc/profile
which sets the environment depending on the value of MSYSTEM
.
Without the correct environment, various things may and will (sometimes silently) break. The exception is using mingw subsystems from pure Windows, which shouldn’t require any special environment apart from an entry in PATH
. Do not set MSYSTEM
outside of the shells, because that will also break things.
PATH
For optimal usage, MSYS2 automatically strips your PATH
environment variable, essentially only leaving C:\Windows\System32
and few others. This behavior can be controlled by setting the variable MSYS2_PATH_TYPE
before starting a shell or using a correct argument when executing the launcher script. Beware that mixing in programs from other MSYS2 installations, Cygwin installations, compiler toolchains or even various other programs is not supported and will probably break things in unexpected ways. Do not have these things in PATH
when running MSYS2 unless you know what you’re doing.
Use msys2
shell for running pacman
, makepkg
, makepkg-mingw
and for building POSIX-dependent software that you don’t intend to distribute. Use mingw
shells for building native Windows software and other tasks.
Packages
MSYS2 uses a port of pacman (known from Arch Linux) for package management. This brings many powerful features such as dependency resolution and simple complete system upgrades, as well as providing the build system (makepkg-mingw
) - which is used to make these packages.
Packages for msys2
are built from recipes in the msys2-packages
Git repository, packages for mingw
are in mingw-packages
. Official repositories are on GitHub under user the msys2 organization. When looking for msys2
packages or deciding to create a new one, keep in mind that MSYS2 doesn’t intend to compete with Cygwin or duplicate their efforts. The set of things that belong to the msys2
subsystem is pretty small and needs to stay that way.
You might be wondering why there appears to be only one architecture variant of the msys2
repository. In reality there are two, but the decision about which one to use is made at the time you install it, depending on whether you installed the i686
or the x86_64
version. It is possible to install both if you wish. Actually, you can have multiple installations of each on your computer, but you should never run programs from two different MSYS2 XXbit variants at the same time due to DLL address space and version conflicts. Also note that the uninstaller will only remove the most recently installed one of each variant).
File system
The virtual filesystem contains:
Paths | Contents |
---|---|
/bin , /dev , /home , /opt , /proc , /tmp , /var |
essential POSIX stuff |
/etc , /usr |
msys2 subsystem |
/mingw32 , /mingw64 |
mingw subsystems |
/c , /d , … |
mount points for Windows drives |
/*.xml , /maintenancetool.* , InstallationLog.txt |
(un)installer |
/autorebase.bat , /msys2_shell.cmd , /msys2.ico |
shell entry points |
mingw32-make
MSYS2 provides two versions of make, one in the make
package and one in the mingw-w64-{i686,x86_64}-make
packages. The latter one is called mingw32-make
on command line, is fully native and doesn’t depend on msys2
shells. The downside is that it doesn’t work with many Makefile
s. Unless you know what you’re doing, use the regular make
.
结语
第二十一篇博文写完,开心!!!!
今天,也是充满希望的一天。