msys2+mingw32环境搭建

安装 msys2 64bit(msys2 http://www.msys2.org/),安装包及工具链。

pacman -Syu => pacman -Su => base-devel(msys) =>
增加中国科学技术开源软件镜像地址 => mingw-w64-i686-gcc =>
mingw-w64-i686-gdb => mingw-w64-i686-objc =>
mingw-w64-i686-make => mingw-w64-i686-pkg-config =>
mingw-w64-i686-glib2 => mingw-w64-i686-pixman =>
mingw-w64-i686-libtool => pthread(mingw) =>
mingw-w64-i686-libxml2 => mingw-w64-i686-clang
1
2
3
4
5
6
7
问题及解决:
1)增加中国科学技术开源软件镜像地址:
分别在msys64/etc/pacman.d路径下的mirrorlist.msys、mirrorlist.mingw32、mirrorlist.mingw64文件的地址开头添加:

#mirrorlist.msys
Server = http://mirrors.ustc.edu.cn/msys2/msys/$arch/
#mirrorlist.mingw32
Server = http://mirrors.ustc.edu.cn/msys2/mingw/i686/
#mirrorlist.mingw64
Server = http://mirrors.ustc.edu.cn/msys2/mingw/x86_64/
1
2
3
4
5
6
2) [ffs函数未定义] => 拷贝 lib/binutils/libiberty.a 到 /lib 中(对比config-host.mak并反汇编libiberty.a发现该库提供ffs函数)
3) pthread库安装:

pthreads-w32-2-9-1-release
make clean GC-static
cp libpthreadGC2.a /mingw/lib
cp pthread.h sched.h /mingw/include

(一) MSYS2 自带的开发环境,安装的包叫 msys2-devel

在 MSYS2 shell 下,可以通过 pacman 命令查询和安装
1
1)查询,可以看到包含 gcc

$ pacman -S msys2-devel
:: 共有 6 组员在组 msys2-devel 中:
:: 软件库 msys
1) binutils 2) cocom 3) gcc 4) gcc-fortran 5) msys2-w32api-headers
6) msys2-w32api-runtime
1
2
3
4
5
6
2)安装

pacman -S msys2-devel
1
(二)MinGW-w64 的

安装

$ pacman -S mingw-w64-x86_64-toolchain
:: 共有 17 组员在组 mingw-w64-x86_64-toolchain 中:
:: 软件库 mingw64
1) mingw-w64-x86_64-binutils
2) mingw-w64-x86_64-crt-git
3) mingw-w64-x86_64-gcc
4) mingw-w64-x86_64-gcc-ada
5) mingw-w64-x86_64-gcc-fortran
6) mingw-w64-x86_64-gcc-libgfortran
7) mingw-w64-x86_64-gcc-libs
8) mingw-w64-x86_64-gcc-objc
9) mingw-w64-x86_64-gdb
10) mingw-w64-x86_64-headers-git
11) mingw-w64-x86_64-libmangle-git
12) mingw-w64-x86_64-libwinpthread-git
13) mingw-w64-x86_64-make
14) mingw-w64-x86_64-pkg-config
15) mingw-w64-x86_64-tools-git
16) mingw-w64-x86_64-winpthreads-git
17) mingw-w64-x86_64-winstorecompat-git
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
可以看到,也有gcc.

运行不同的 shell,则执行对应版本的 gcc.

(图中第一个 shell 是 MinGW 32-bit 版本的,类似于 64-bit 版本的)

那么,这两者有什么区别呢?

一言以蔽之,前者编译出来的可执行文件,要依赖 MSYS2 提供的动态链接库,而后者不需要。下面详细说明一下:
(一) MSYS2 下的 gcc 编译环境,编译的可执行文件要依赖于 msys-2.0.dll,这个 DLL 提供了 Linux 下编程的提供的函数和接口,例如 fork 函数。
这个编译环境对于编译基于 Linux 下编写的软件,是非常适合的。例如编译 GNU 提供的各种工具。例如,你想编译最新版本的 GNU grep 工具,MSYS2 下的这个环境是非常适合的。
这个环境非常类似于 cygwin.
一个简单的 hello world 程序:

#include <stdio.h>
void main()
{
printf("Hello world ! ");
}
1
2
3
4
5
编译出来的 a.exe, 用 dumpbin (这个是visual studio 提供的工具,当然,你也可以安装 Dependency Walker 这个独立小巧的工具来看)来查看 DLL 依赖关系:

d:hello_world> dumpbin /dependents a.exe
1
看到:

Microsoft (R) COFF/PE Dumper Version 9.00.21022.08
Copyright (C) Microsoft Corporation. All rights reserved.


Dump of file a.exe

File Type: EXECUTABLE IMAGE

Image has the following dependencies:

msys-2.0.dll
KERNEL32.dll
1
2
3
4
5
6
7
8
9
10
11
12
其中依赖了 msys-2.0.dll. (KERNEL32.dll 是 windows 提供的)。

在 windows 的 cmd 下直接运行,如果没有把 msys-2.0.dll 的路径告知的话,则会运行不成功。


(二)用 MinGW64 的编译环境,得到 a_mingw.exe

d:hello_world>dumpbin /dependents a_mingw.exe
1
看到

Microsoft (R) COFF/PE Dumper Version 9.00.21022.08
Copyright (C) Microsoft Corporation. All rights reserved.


Dump of file a_mingw.exe

File Type: EXECUTABLE IMAGE

Image has the following dependencies:

KERNEL32.dll
msvcrt.dll
1
2
3
4
5
6
7
8
9
10
11
12
不再依赖于 msys-2.0.dll,上面两个 dll 都是 windows 提供的。

所以,如果源代码就是基于 windows 开发的,那使用 MinGW 的编译环境比较好,编译出来的可执行文件,不用再依赖 MSYS 提供的动态链接库。当然,前提是代码中不能使用 Linux 的东西,即 POSIX 的那套东西。

备注:

msvcrt.dll 提供的接口,可以参考 C Run-Time Library Reference
 

原文地址:https://www.cnblogs.com/wuhh123/p/10569636.html