MSYS2环境是什么?我如何选择一个?

huangapple go评论78阅读模式
英文:

What are MSYS2 environments? How do I pick one?

问题

MSYS2据说有不同的环境

它们是什么?我应该选择哪一个来使用?


<sub>(这个问答旨在作为这个主题的规范重复。)</sub>

英文:

MSYS2 is said to have different environments.

What are they? How do I choose which one to use?


<sub>(This Q&A is intended as a canonical duplicate on this topic.)</sub>

答案1

得分: 0

请查看官方手册。以下环境可用:UCRT64、MINGW64、CLANG64、MSYS和其他(见链接)。

UCRT64是一个不错的默认选择,但下面解释了其他选择。

不同环境存在是为了生成不同类型的Windows可执行文件,或者使用不同工具来生成这些文件。

每个环境都有自己的软件包(编译器、预编译库和各种可执行文件),可以通过软件包名称中的共同前缀来区分(例如,UCRT64的软件包名称为mingw-w64-ucrt-x86_64-...)。

每个环境将其文件安装到MSYS2安装的单独子目录中,例如,UCRT64安装到C:\msys64\ucrt64,以此类推(但是需要特别注意,MSYS安装到C:\msys64\usr,某些软件包直接安装到C:\msys64)。

每个环境在开始菜单中都有相应的快捷方式,可以启动MSYS2终端并将相应目录添加到其PATH中。例如,MSYS2 UCRT64启动时将C:\msys64\ucrt64\bin添加到PATH中。

  • 默认情况下,MSYS2忽略系统范围内的PATH设置,并以完全定制的PATH启动其终端,运行echo $PATH以查看它。

  • 所有环境在C:\msys64\usr\bin之后将C:\msys64\<env>\bin添加到PATH中。因此,在某种程度上,所有环境都继承自MSYS环境。后面会详细介绍。

  • 这些环境还会自定义一些其他环境变量,请运行printenv查看自己。

MSYS2终端在终端提示中以品红色文本显示当前环境

如何使用特定环境?

  1. 所有安装的软件包必须具有适当的前缀。例如,对于UCRT64,使用mingw-w64-ucrt-x86_64-...(请参阅下面的表格了解其他环境)。

    这适用于所有编译器、库等。因此,如果要在UCRT64中安装GCC,请使用pacman -S mingw-w64-ucrt-x86_64-gcc,而不是pacman -S gcc

    唯一的例外是与编译过程无关的转换为Linux的实用程序,如grepsedbash等,它们没有带前缀的软件包。值得注意的是,即使存在带前缀的版本,make也应该从未带前缀的软件包中安装。)

    未带前缀的软件包都属于MSYS环境。

    警告:通常情况下,可以安装多个环境的软件包,它们不会相互干扰,但是未带前缀的MSYS软件包在所有环境的PATH中,其中一些妨碍你。例如,如果你安装pacman -S gcc,忘记安装pacman -S ...-gcc,然后尝试从非MSYS环境运行gcc,你将调用MSYS环境的GCC,而不是你环境的GCC。大多数情况下,这不是你想要的,你会收到晦涩的错误。

  2. 如果使用MSYS2终端,请使用相应的快捷方式启动终端

    这些快捷方式中包含环境名称,例如,对于UCRT64,请使用MSYS2 UCRT64快捷方式。

  3. 如果不使用MSYS2终端并从其他位置调用工具,请将相应的目录添加到PATH中,例如,对于UCRT64,请添加C:\msys64\ucrt64\bin

    在罕见的情况下,你可能还需要添加C:\msys64\usr\bin。如果需要,请确保将其添加到先前提到的目录之后

    为避免问题,最好将这些目录直接放在PATH的开头(包括系统范围和特定用户)。

如何选择环境?

UCRT64对大多数情况应该是一个不错的默认选择。

否则,选择取决于以下因素:

  • 首选编译器:GCC还是Clang(一些环境提供两者)。
  • 首选C++标准库(GCC的libstdc++或Clang的libc++,一些环境提供两者)。
  • 首选C标准库(旧的MSVCRT还是带有UTF-8支持的新UCRT)。
  • 是否需要使用消毒剂(Address Sanitizer、UB sanitizer等)。
  • 是否需要Cygwin风格的POSIX仿真。
  • 是否希望生成ARM的可执行文件
环境 UCRT64 MINGW64 CLANG64 MSYS
安装至<br/><sup>(相对于MSYS2安装目录<br/>通常为C:\msys64\...)</sup> /ucrt64 /mingw64 /clang64 /usr<br/><sup>(一些软件包安装至/?)</sup>
**软件
英文:

See the official manual. Following environments are available: UCRT64, MINGW64, CLANG64, MSYS, and others (see link).

UCRT64 is a good default, but the alternatives are explained below.

The different environments exist to produce different kinds of Windows executables, or do that with different tools.

Each environment has its own packages (compilers, precompiled libraries, and various executables), which can be distinguished by the common prefix in the package names (e.g. mingw-w64-ucrt-x86_64-... for UCRT64).

Each environment installs its files to a separate subdirectory inside the MSYS2 installation, e.g. UCRT64 installs to C:\msys64\ucrt64, and so on (except MSYS notably installs to C:\msys64\usr, and some packages directly to C:\msys64).

Each environment has the respective shortcut in the Start menu, which starts the MSYS2 terminal and adds the respective directory to its PATH. E.g. the MSYS2 UCRT64 starts with C:\msys64\ucrt64\bin in the PATH.

  • By default MSYS2 ignores the system-wide PATH setting, and starts its terminal with a fully customized PATH, run echo $PATH to see it.

  • All environments also add C:\msys64\usr\bin to PATH after C:\msys64\&lt;env&gt;\bin. So, in a way, all environments inherit from the MSYS environment. More on that below.

  • The environments also customize some other environment variables, run printenv to see for yourself.

MSYS2 terminal displays the current environment in magenta text in the terminal prompt.

How do I use a specific environment?

  1. All packages you install must have their name prefixed appropriately. E.g. for UCRT64, mingw-w64-ucrt-x86_64-... (see the table below for other environments).

    This applies to all compilers, libraries, etc. So if you want to install GCC in UCRT64, use pacman -S mingw-w64-ucrt-x86_64-gcc, NOT pacman -S gcc.

    The only exception are utilites ported Linux which are unrelated to the compilation process, such as grep, sed, bash, etc, which don't have prefixed packages. (Notably make should also be installed from an unprefixed package, even though the prefixed versions exist.)

    Unprefixed packages all belong to the MSYS environment.

    WARNING: Normally you can install packages for multiple environments, and they won't interfere with each other, except the unprefixed MSYS packages are in the PATH for all the environments, and some of them will get in your way. E.g if you install pacman -S gcc, forget to install pacman -S ...-gcc, then try to run gcc from any environment other than MSYS, you'll call GCC of the MSYS environment, instead of your environment's GCC. Most of the time this is not what you want, and you'll get cryptic errors.

  2. If you use MSYS2 terminal, start the terminal using the appropriate shortcut.

    The shortcuts have the environment names in them, e.g. for UCRT64 use the MSYS2 UCRT64 shortcut.

  3. If you don't use the MSYS2 terminal and call the tools from elsewhere, add the appropriate directory to PATH, e.g. for UCRT64 add C:\msys64\ucrt64\bin.

    In rare cases you may also need to add C:\msys64\usr\bin. If you do, make sure to add it after the previously mentioned directory.

    To avoid problems, it's a good idea to have those directories directly at the beginning of the PATH (both system-wide and user-specific).

How do I choose the environment?

UCRT64 should be a good default for most purposes.

Otherwise the choice depends on:

  • Preferred compiled: GCC or Clang (some provide both).
  • Preferred C++ standard library (GCC's libstdc++ or Clang's libc++, some provide both).
  • Preferred C standard library (the old MSVCRT, or the new UCRT with the UTF-8 support).
  • Whether you need the sanitizers (Address Sanitizer, UB sanitizer, etc).
  • Whether you need Cygwin-style POSIX emulation.
  • Whether you want to produce executables for ARM.
Environment UCRT64 MINGW64 CLANG64 MSYS
Installs to<br/><sup>(relative to MSYS2 installation<br/>directory, normally C:\msys64\...)</sup> /ucrt64 /mingw64 /clang64 /usr<br/><sup>(some packages to /?)</sup>
Package prefix mingw-w64-ucrt-x86_64- mingw-w64-x86_64- mingw-w64-clang-x86_64- none
Target architecture x86_64 x86_64 x86_64 Cygwin x86_64
Compiler GCC (also Clang) GCC (also Clang) Clang GCC (also Clang)<br/><sup>(might be outdated)</sup>
C++ standard library libstdc++ (also libc++) libstdc++ (also libc++) libc++ libstdc++
C standard library UCRT MSVCRT UCRT Cygwin
Sanitizers No<sup>1</sup> No<sup>1</sup> YES No<sup>1</sup>

<sup>1 — UBSan can be made to work, but it can't print diagnostics and can only crash on error.</sup>

Target architecture variants:

  • 32-bit

    All environments above produce x64 executables (aka x86_64).

    There are also environments for x32 executables (aka i686): MINGW32 and CLANG32.

    They are similar to the respective x64 environments, except the files are installed to the /...32 directories instead of /...64, and the package prefixes contain i686 in place of x86_64.

  • ARM

    All environments above produce executables for x86, either x32 or x64. This is what the majority of desktops and laptops run on.

    Apparently there are now some ARM laptops running Windows too. And while they can emulate x86 to run x86 apps, native ARM applications should have better performance.

    The CLANGARM64 environment can be used to produce ARM (aka aarch64) apps. It's similar to CLANG64, except the files are installed to /clangarm64, and the packages are prefixed with mingw-w64-clang-aarch64-.

C standard libraries, MSVCRT vs UCRT:

The manual explains the difference well.

In short:

  • MSVCRT (msvcrt.dll):

    • Old, comes from Microsoft Visual Studio 6.0.

    • This was the only option for MinGW until a few years ago.

    • No UTF-8 support for paths, must use UTF-16 and wchar_t to handle unicode in paths.

  • UCRT (ucrtbase.dll)

    • New and shiny, this is what the contemporary Visual Studio uses.

    • Available by default on Windows 10, on prior Windows versions must be installed manually.

    • Supports UTF-8 in paths, if you enable a UTF-8 locale in your application.

The MSYS environment:

This environment is based on MSYS2's own fork of Cygwin, which is a POSIX emulation layer.

It's primarily used to compile Linux applications that were not written in a cross-platform manner. Arguably, new applications should always be written in a cross-platform manner in the first place, and then compiled using the other environments.

MSYS2 uses it to provide bash, grep, sed, awk, and other command line utilities commonly used on Linux.

From my limited understanding, MSYS applications are normally used exclusively inside of the MSYS2 shell for development purposes, and not distrbuted to users.

Among other things, applications compiled with it see all paths in Linux style: with forward slashes / as separators, with the / (root) directory mapped to the MSYS2 installation directory, and /c magically mapped to C:\ (similary for other drive letters). They also get access to emulated Linux-specific functions, such as fork().

This emulation layer has a performance cost.

huangapple
  • 本文由 发表于 2023年6月26日 04:21:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76552264.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定