Win7 / Win10 下 msys64 安装 MinGW-w64 工具链

安装和配置

1. 安装msys64

下载MSYS2安装文件

安装到指定目录, 因为后续安装会占用十几个GB大小的磁盘空间, 因此建议不要安装到系统盘, 可以安装到D盘, 例如 D:\msys64

2. 命令行和常用命令

MSYS2 终端环境

MSYS2有多个不同的环境

  • CLANG64 (\msys64\clang64.exe)
  • CLANGARM64 (\msys64\clangarm64.exe)
  • MSYS2 (\msys64\msys2.exe)
  • MINGW64 (\msys64\mingw64.exe)
  • UCRT64 (\msys64\ucrt64.exe)

启动不同环境:

假定安装在D盘根目录

  • UCRT64: D:\msys64\ucrt64.exe
  • MinGW-w64: D:\msys64\mingw64.exe
  • MSYS: D:\msys64\msys2.exe

通常使用 MINGW64 和 UCRT64, 后者兼容性好些(使用opencv编译正常)

更新软件库

pacman -Syu

如果出现这个错误

error: failed to update mingw32 (unable to lock database)
error: failed to update mingw64 (unable to lock database)
error: failed to update msys (unable to lock database)
error: failed to synchronize all databases

可能是之前意外关闭, db.lck文件未清除, 执行以下命令

rm -f /var/lib/pacman/db.lck

修改安装源

Windows下进入msys64/etc/pacman.d/, MSYS64下则是 /etc/pacman.d/

需要修改三个文件, 都添加在Server列表第一行

mirrorlist.mingw32

Server = http://mirrors.ustc.edu.cn/msys2/mingw/i686/

mirrorlist.mingw64

Server = http://mirrors.ustc.edu.cn/msys2/mingw/x86_64/

mirrorlist.msys

Server = http://mirrors.ustc.edu.cn/msys2/msys/$arch/

常用命令

# 更新软件
pacman -Syu
# 安装软件
pacman -S [package]
# 查询已安装
pacman -Q
# 卸载软件
pacman -R [package]

安装 GCC

pacman -S mingw-w64-ucrt-x86_64-gcc

安装opencv

pacman -S mingw-w64-ucrt-x86_64-opencv

其他可选择的软件有 git mercurial cvs wget p7zip perl ruby python2

在Windows命令行中使用UCRT64环境执行命令

D:\msys64\msys2_shell.cmd -defterm -no-start -ucrt64 -c "gcc --version"

3. 配置

将mingw64的路径(例如 C:\msys64\mingw64\bin )添加到PATH目录, 添加完后, 在Eclipse CDT的Perference C/C++ -> Core Build Toolchains 中Available Toolchains就能看到GCC了

Update 2019-11-12: 配置GNU99, 打开 project properties. -> C/C++Build -> Settings, Tool Settings 标签页-> GCC C compiler -> Miscellaneous. 在 other flags 输入框, 添加 -std=c99 , Apply&Close. 配合 #include <stdbool.h> 就可以使用 bool, true, false关键字了.

MSYS2 环境区别

MSYS2 提供了三种不同的环境,每种环境都有其特定的用途和特点:

1. MSYS2 MinGW-w64 (mingw64/mingw32)

特点:

  • 提供原生 Windows 应用程序
  • 不依赖 POSIX 兼容层
  • 可以构建高性能的应用程序
  • 适用于生产环境

用途:

  • 编译和运行原生 Windows 程序
  • 构建不依赖 POSIX 兼容层的应用程序
  • 开发高性能应用程序

路径:

  • 64位: D:\msys64\mingw64\
  • 32位: D:\msys64\mingw32\

包命名:

  • 64位: mingw-w64-x86_64-*
  • 32位: mingw-w64-i686-*

2. MSYS2 UCRT64

特点:

  • 基于 Windows 的通用 C 运行时 (UCRT)
  • 比传统的 MinGW-w64 更现代
  • 使用 Windows 10/11 中的 UCRT 而不是旧的 MSVCRT
  • 提供更好的兼容性和性能

用途:

  • 开发现代 Windows 应用程序
  • 使用最新的 Windows 运行时功能
  • 构建与 Windows 10/11 兼容的应用程序

路径:

  • D:\msys64\ucrt64\

包命名:

  • mingw-w64-ucrt-x86_64-*

3. MSYS2 MSYS

特点:

  • 基于 POSIX 兼容层
  • 主要用于构建 POSIX 兼容的应用程序
  • 不适合构建高性能的原生 Windows 应用程序
  • 主要用于构建 MSYS2 本身的工具链

用途:

  • 构建 POSIX 兼容的应用程序
  • 运行依赖 POSIX 功能的脚本
  • 构建 MSYS2 本身的工具链

路径:

  • D:\msys64\usr\

包命名:

  • msys-*

选择建议

对于一般开发:

  • 推荐使用 UCRT64,因为它基于最新的 Windows 运行时,提供了更好的兼容性和性能。

对于传统项目:

  • 使用 MinGW-w64,它经过了长时间的测试和验证,适用于传统的 Windows 应用程序开发。

对于工具链开发:

  • 使用 MSYS,它提供了完整的 POSIX 兼容环境,适用于构建工具链和运行脚本。

安装包示例

在 UCRT64 中安装 OpenCV:

pacman -S mingw-w64-ucrt-x86_64-opencv

在 MinGW-w64 中安装 OpenCV:

pacman -S mingw-w64-x86_64-opencv

在 MSYS 中安装工具:

pacman -S msys/automake msys/autoconf

posted on 2019-11-06 19:33  Milton  阅读(3962)  评论(0)    收藏  举报

导航