MSYS2开发环境搭建
软件安装
下载msys2-x86_64软件包 https://www.msys2.org/,双击安装到某根目录下,比如D:\msys64。
pacman是MSYS2自带的软件管理工具:
- 可通过修改msys64\etc\pacman.d下的三个文件修改软件源,可供选择的源有:
- 下载后的软件包默认存放目录msys64\var\cache\pacman\pkg;若命令行下载速度较慢,可以到到软件源网站使用下载工具下载相应的软件包,然后拷贝到此目录,接着使用命令行进行安装。
- 运行msys2_shell.bat:
pacman -Sy
更新本地包数据 - 升级核心包:
pacman -S --needed filesystem msys2-runtime bash libreadline libiconv libarchive libgpgme libcurl pacman ncurses libintl
, 之后需要关闭所有 MSYS2 shell,然后运行 autorebase.bat - 升级其他包:
pacman -Su
- 常用命令,参考官网Package Management:
pacman -Q
查看已安装的软件包pacman -Q -g
查看已安装的软件包组pacman -Q -g base-devel
查看软件组包含的软件pacman -Qi package
查看软件包信息,显示软件简介,构架,依赖,大小等详细信息pacman -Ql vim
查询软件包的内容pacman -Qs 关键字
从已安装的包中搜索pacman -Qo <full file path>
查找已安装文件所属包pacman -F <filename>
(don’t include the path in the filename)查询软件所在的包pacman -Ss 关键字
从仓库中搜索pacman -S -g
列出软件仓库上所有的软件包组pacman -S packages
安装软件包pacman -Sy packages
同步包数据库后再执行安装软件包pacman -Sw package
只下载包,不安装pacman -Sc
清理未安装的包文件,包文件位于 /var/cache/pacman/pkg/与/var/lib/pacman/ 目录pacman -Scc
清理所有的缓存文件pacman -R 包名
该命令将只删除包,不包含该包的依赖。pacman -Rs 包名
在删除包的同时,也将删除其依赖。
- 查看工具帮助:
pacman -h
;pacman -S -h
- 建议通过安装软件组来安装工具链
pacman -S mingw-w64-x86_64-toolchain pacman -S base-devel pacman -S vim
MSYS2应用说明
运行环境说明
-
msys64\etc\fstab中可以配置文件目录映射:比如配置
C:\Users\lenovo\Desktop /desktop
后,可以在终端直接cd /desktop
后可以直接切换到C:\Users\lenovo\Desktop目录下。 -
任务栏快捷键:msys64\usr\bin\mintty.exe拖拽到任务栏上,右击选择属性,将目标改成三种BAT脚本中的一个。
-
vim高亮配置:将msys64\etc\skel.vimrc拷贝到用户目录下。
-
MSYS2有三个执行脚本,分别是 msys2_shell.bat、mingw32_shell.bat 和 mingw64_shell.bat,查看内容可以看到其中只有一行区别,即是设定 MSYSTEM 变量。这个变量在 /etc/profile 中会用到:
MSYS2_PATH="/usr/local/bin:/usr/bin:/bin" MANPATH="/usr/local/man:/usr/share/man:/usr/man:/share/man:${MANPATH}" INFOPATH="/usr/local/info:/usr/share/info:/usr/info:/share/info:${INFOPATH}" MINGW_MOUNT_POINT= if [ -n "$MSYSTEM" ] then case "$MSYSTEM" in MINGW32) MINGW_MOUNT_POINT=/mingw32 PATH="${MINGW_MOUNT_POINT}/bin:${MSYS2_PATH}:${PATH}" PKG_CONFIG_PATH="${MINGW_MOUNT_POINT}/lib/pkgconfig:${MINGW_MOUNT_POINT}/share/pkgconfig" ACLOCAL_PATH="${MINGW_MOUNT_POINT}/share/aclocal:/usr/share/aclocal" MANPATH="${MINGW_MOUNT_POINT}/share/man:${MANPATH}" ;; MINGW64) MINGW_MOUNT_POINT=/mingw64 PATH="${MINGW_MOUNT_POINT}/bin:${MSYS2_PATH}:${PATH}" PKG_CONFIG_PATH="${MINGW_MOUNT_POINT}/lib/pkgconfig:${MINGW_MOUNT_POINT}/share/pkgconfig" ACLOCAL_PATH="${MINGW_MOUNT_POINT}/share/aclocal:/usr/share/aclocal" MANPATH="${MINGW_MOUNT_POINT}/share/man:${MANPATH}" ;; MSYS) PATH="${MSYS2_PATH}:/opt/bin:${PATH}" PKG_CONFIG_PATH="/usr/lib/pkgconfig:/usr/share/pkgconfig:/lib/pkgconfig" ;; *) PATH="${MSYS2_PATH}:${PATH}" ;; esac else PATH="${MSYS2_PATH}:${PATH}" Fi
可见,三个 .bat 的区别就是 PATH 的设置,mingw32_shell.bat 优先使用 msys64/mingw32 下的工具,mingw64_shell.bat 优先使用 msys64/mingw64 下的工具,而 msys2_shell.bat 两个都不使用,只用自身 msys 的工具。这么做的好处是当需要编译 32bit Target 的项目时使用 mingw32_shell.bat,64 bit 使用 mingw64_shell.bat,各套工具互不干扰。
-
引用Repositories and Mirrors The package manager uses only environments that are configured under /etc/pacman.conf. You may add/uncomment/remove/comment those as needed:
# always include msys! [msys] Include = /etc/pacman.d/mirrorlist.msys [mingw32] Include = /etc/pacman.d/mirrorlist.mingw [mingw64] Include = /etc/pacman.d/mirrorlist.mingw [ucrt64] Include = /etc/pacman.d/mirrorlist.mingw [clang64] Include = /etc/pacman.d/mirrorlist.mingw [clang32] Include = /etc/pacman.d/mirrorlist.mingw [clangarm64] Include = /etc/pacman.d/mirrorlist.mingw
To launch an environment either use the wrapper executables like
ucrt64.exe
or callmsys2_shell.cmd
with either the matching parameter likemsys2_shell.cmd -clang64
or by setting MSYSTEM.
【2023-02-21 win10 更新】Environments文档翻译(文档源码在此)
MSYS2 comes with different environments/subsystems and the first thing you have to decide is which one to use. The differences among the environments are mainly environment variables, default compilers/linkers, architecture, system libraries used etc. If you are unsure, go with UCRT64.
MSYS2带有多个不同环境/子系统,你要做的第一件事情就是要选择使用哪个。环境之间的主要不同时环境变量、默认编译器/连接器、架构、所用的系统库。如果你不确定使用哪个,那么就使用UCRT64吧
The MSYS environment contains the unix-like/cygwin based tools, lives under /usr and is special in that it is always active. All the other environments inherit from the MSYS environment and add various things on top of it.
MSYS
环境包括类unix/cygwin工具,位于/usr
目录,特殊指出在于它是活动的。其他环境继承MSYS
环境并在它之前添加变量
For example, in the UCRT64 environment the $PATH variable starts with /ucrt64/bin:/usr/bin so you get all ucrt64 based tools as well as all msys tools.
例如,在UCRT64
环境中$PATH
变量是/ucrt64/bin:/usr/bin
开始的,所以你能像获取msys相关命令一样获取基于ucrt64的工具命令
Name | Prefix | Toolchain | Architecture | C Library | C++ Library |
---|---|---|---|---|---|
MSYS | /usr | gcc | x86_64 | cygwin | libstdc++ |
UCRT64 | /ucrt64 | gcc | x86_64 | ucrt | libstdc++ |
CLANG64 | /clang64 | llvm | x86_64 | ucrt | libc++ |
CLANGARM64 | /clangarm64 | llvm | aarch64 | ucrt | libc++ |
CLANG32 | /clang32 | llvm | i686 | ucrt | libc++ |
MINGW64 | /mingw64 | gcc | x86_64 | msvcrt | libstdc++ |
MINGW32 | /mingw32 | gcc | i686 | msvcrt | libstdc++ |
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.
激活环境是通过选择MSYSTEM
环境变量来选择的。配置MSYSTEM
为UCRT64
,启动登录shell就会进入相应的环境
GCC vs LLVM/Clang
These are the default compilers/toolchains used for building all packages in the respective repositories.
GCC based environments:
- Widely tested/used at this point
- Fortran support
- While there also exists a Clang package in the MINGW environments, that one still uses the GNU linker and the GNU C++ library. In some cases Clang is used to build packages as well there, in case upstream prefers Clang over GCC for example.
LLVM/Clang based environments:
- Only uses LLVM tools, LLD as a linker, LIBC++ as a C++ standard library
- Clang provides ASAN support
- Native support for TLS (Thread-local storage)
- LLD is faster than LD, but does not support all the features LD supports
- Some tools lack feature parity with equivalent GNU tools
- Supports ARM64/AArch64 architecture on Microsoft Windows 10
MSVCRT vs UCRT
These are two variants of the C standard library on Microsoft Windows.
MSVCRT (Microsoft Visual C++ Runtime) is available by default on all Microsoft Windows versions, but due to backwards compatibility issues is stuck in the past, not C99 compatible and is missing some features.
- It isn’t C99 compatible, for example the printf() function family, but…
- mingw-w64 provides replacement functions to make things C99 compatible in many cases
- It doesn’t support the UTF-8 locale
- Binaries linked with MSVCRT should not be mixed with UCRT ones because the internal structures and data types are different. (More strictly, object files or static libraries built for different targets shouldn’t be mixed. DLLs built for different CRTs can be mixed as long as they don’t share CRT objects, e.g. FILE*, across DLL boundaries.) Same rule is applied for MSVC compiled binaries because MSVC uses UCRT by default (if not changed).
- Works out of the box on every Microsoft Windows versions.
UCRT (Universal C Runtime) is a newer version which is also used by Microsoft Visual Studio by default. It should work and behave as if the code was compiled with MSVC.
- Better compatibility with MSVC, both at build time and at run time.
- It only ships by default on Windows 10 and for older versions you have to provide it yourself or depend on the user having it installed.
添加右键快捷方式 <2018-10-29 win10>
msys2.reg
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\Directory\Background\shell\Mingw64]
@="MinGW &64 Bash Here"
"Icon"="D:\\msys64\\msys2.ico"
[HKEY_CLASSES_ROOT\Directory\Background\shell\Mingw64\command]
@="D:\\msys64\\msys2_shell.cmd -mingw64 -here"
[HKEY_CLASSES_ROOT\Directory\Background\shell\UCRT64]
@="ucrt64 Bash Here"
"Icon"="D:\\msys64\\ucrt64.ico"
[HKEY_CLASSES_ROOT\Directory\Background\shell\UCRT64\command]
@="D:\\msys64\\msys2_shell.cmd -ucrt64 -here"
制作软件包makepkg
MSYS2官网的软件包是通过makepkg或makepkg-mingw制作的。
可参
- Package scripts for MinGW-w64 targets to build under MSYS2.
MINGW_INSTALLS=mingw64 makepkg -sLf --skippgpcheck
- http://sourceforge.net/p/msys2/wiki/Contributing%20to%20MSYS2/,
- C:\msys64\usr\share\pacman\PKGBUILD.proto
常用软件包编译
常用软件包我们可以简单的使用命令直接从官网安装即可,比如安装openssl:
- 32bit:
pacman -S mingw-w64-i686-openssl
- 64bit:
pacman -S mingw-w64-x86_64-openssl
常用工具集安装 <版本: msys2-x86_64-20180531>
- Qt5工具集
- pacman -S mingw-w64-x86_64-cmake-doc-qt
- pacman -S mingw-w64-x86_64-qt-creator
- pacman -S mingw-w64-x86_64-qt5
- pacman -S mingw-w64-x86_64-qt5-static
- pacman -S mingw-w64-x86_64-qtbinpatcher
- pacman -S mingw-w64-x86_64-qtwebkit
- pacman -S mingw-w64-x86_64-qwt-qt5
有时候根据项目需要我们不得不自己动手编译依赖的软件包,以下是我在工作用到的库编译过程记录。
openssl
- 64bit
mkdir openssl64
cd openssl64
tar zxvf openssl-1.0.2c.tar.gz
cd openssl-1.0.2c
./configure mingw64 shared
make
make INSTALL_PREFIX=../ install
- 32bit
mkdir openssl32
cd openssl32
tar zxvf openssl-1.0.2c.tar.gz
cd openssl-1.0.2c
./configure mingw shared
make
make INSTALL_PREFIX=../ install
zlib
- 32bit
mkdir zlib32
cd zlib32
tar zxvf zlib-1.2.8.tar.gz
cd zlib-1.2.8/
make -f ./win32/Makefile.gcc
make
make install -f win32/Makefile.gcc DESTDIR=../
ffmpeg
# ERROR: speex not found using pkg-config
pacman -S pkg-config
# ./configure: line 1577: cmp: command not found
pacman -S diffutils
./configure --prefix=./DESTDIR --target-os=mingw32 --arch=x86_64 --disable-debug --disable-x86asm --enable-decoder=h264 --enable-parser=h264
参考
扩展阅读
- 利用MSYS2安装MinGW+Qt开发环境
- GCC online documentation
- https://www.mingw-w64.org/downloads/
- MSYS2 Packages
- https://github.com/msys2/MINGW-packages