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 !\n");
}
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
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM