MinGW64交叉编译glib静态库

经典方式交叉编译glib

  1. 编译glib需要依赖 libffi libiconv gettext pcre glib
1
2
3
# 配置编译所需工具
sudo apt update
sudo apt install binutils python3-mako autoconf intltool git wget mingw-w64
  1. 编译zlib
1
2
3
4
5
wget https://zlib.net/zlib-1.2.12.tar.gz -P ~/src
tar -xzvf ~/src/zlib-1.2.12.tar.gz -C ~/build && cd ~/build/zlib-1.2.12

AR=x86_64-w64-mingw32-ar RANLIB=x86_64-w64-mingw32-ranlib \
CC=x86_64-w64-mingw32-gcc ./configure --prefix=/home/leux/out --static
  1. 编译libffi
1
2
3
4
5
6
7
wget https://github.com/libffi/libffi/releases/download/v3.4.3/libffi-3.4.3.tar.gz -P ~/src
tar -xzvf ~/src/libffi-3.4.3.tar.gz -C ~/build && cd ~/build/libffi-3.4.3

./configure --prefix=/home/leux/out --host=x86_64-w64-mingw32 \
--disable-shared --enable-static

make && make install
  1. 编译libiconv
1
2
3
4
5
6
7
wget https://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.17.tar.gz -P ~/src
tar -xzvf ~/src/libiconv-1.17.tar.gz -C ~/build && cd ~/build/libiconv-1.17

./configure --prefix=/home/leux/out --host=x86_64-w64-mingw32 \
--disable-shared --enable-static

make && make install
  1. 编译gettext
1
2
3
4
5
6
7
8
wget https://ftp.gnu.org/pub/gnu/gettext/gettext-0.21.tar.gz -P ~/src
tar -xzvf ~/src/gettext-0.21.tar.gz -C ~/build && cd ~/build/gettext-0.21

CPPFLAGS="-I/home/leux/out/include" LDFLAGS="-L/home/leux/out/lib" \
./configure --prefix=/home/leux/out --host=x86_64-w64-mingw32 \
--enable-static --disable-shared

make && make install
  1. 编译pcre
1
2
3
4
5
6
7
8
9
wget https://github.com/PCRE2Project/pcre2/releases/download/pcre2-10.40/pcre2-10.40.tar.gz -P ~/src
tar -xzvf ~/src/pcre2-10.40.tar.gz -C ~/build && cd ~/build/pcre2-10.40

./configure --prefix=/home/leux/out --host=x86_64-w64-mingw32 \
--enable-static --disable-shared --enable-pcre16 \
--enable-utf --enable-unicode-properties --enable-cpp \
--disable-pcregrep-libz --disable-pcregrep-libbz2 --disable-pcretest-libreadline

make && make install
  1. 编译glib

2.53.12.57.1 自带 configure
2.57.22.59.0 使用 autogen.sh 生成 configure
2.59.1 开始就完全没了 configure, autogen.sh 等,转而使用 meson 来构建了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
wget https://download.gnome.org/sources/glib/2.59/glib-2.59.0.tar.xz -P ~/src
xz -d ~/src/glib-2.59.0.tar.xz && tar -xvf ~/src/glib-2.59.0.tar -C ~/build && cd ~/build/glib-2.59.0

# 修复Win32下静态编译出错问题
wget https://gitlab.gnome.org/GNOME/glib/uploads/e3c2799569ec6bb14fbe5b2a29423bd1/0001-win32-Prefer-the-use-of-constructors-over-DllMain.patch -P ~/src
patch -p1 < ~/src/0001-win32-Prefer-the-use-of-constructors-over-DllMain.patch

./autogen.sh
PKG_CONFIG_PATH=/home/leux/out/lib/pkgconfig \
CPPFLAGS="-I/home/leux/out/include" LDFLAGS="-L/home/leux/out/lib" \
./configure --prefix=/home/leux/out --host=x86_64-w64-mingw32 \
--with-libiconv=/home/leux/out --with-pcre=/home/leux/out \
--enable-static --disable-shared

make && make install
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# 补丁0001-win32-Prefer-the-use-of-constructors-over-DllMain.patch内容如下:
From bc90511c1eb333e26e0bc0eaee62375d0e788db6 Mon Sep 17 00:00:00 2001
From: Erik van Pienbroek <epienbro@fedoraproject.org>
Date: Tue, 16 Apr 2013 11:42:11 +0200
Subject: [PATCH] win32: Prefer the use of constructors over DllMain

This prevents having to depend on DllMain in static libraries

Constructors are available in both the GCC build (GCC 2.7 and later)
and the MSVC build (MSVC 2008 and later using _Pragma, earlier
versions using #pragma)
---
glib/glib-init.c | 22 ++++++++++++++--------
1 file changed, 14 insertions(+), 8 deletions(-)

diff --git a/glib/glib-init.c b/glib/glib-init.c
index 0032ee8..dd6ccbf 100644
--- a/glib/glib-init.c
+++ b/glib/glib-init.c
@@ -223,12 +223,14 @@ glib_init (void)

#if defined (G_OS_WIN32)

+HMODULE glib_dll = NULL;
+
+#if defined (DLL_EXPORT)
+
BOOL WINAPI DllMain (HINSTANCE hinstDLL,
DWORD fdwReason,
LPVOID lpvReserved);

-HMODULE glib_dll;
-
BOOL WINAPI
DllMain (HINSTANCE hinstDLL,
DWORD fdwReason,
@@ -238,11 +240,6 @@ DllMain (HINSTANCE hinstDLL,
{
case DLL_PROCESS_ATTACH:
glib_dll = hinstDLL;
- g_clock_win32_init ();
-#ifdef THREADS_WIN32
- g_thread_win32_init ();
-#endif
- glib_init ();
break;

case DLL_THREAD_DETACH:
@@ -259,7 +256,10 @@ DllMain (HINSTANCE hinstDLL,
return TRUE;
}

-#elif defined (G_HAS_CONSTRUCTORS)
+#endif /* defined (DLL_EXPORT) */
+#endif /* defined (G_OS_WIN32) */
+
+#if defined (G_HAS_CONSTRUCTORS)

#ifdef G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA
#pragma G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(glib_init_ctor)
@@ -269,6 +269,12 @@ G_DEFINE_CONSTRUCTOR(glib_init_ctor)
static void
glib_init_ctor (void)
{
+#if defined (G_OS_WIN32)
+ g_clock_win32_init ();
+#ifdef THREADS_WIN32
+ g_thread_win32_init ();
+#endif /* defined (THREADS_WIN32) */
+#endif /* defined (G_OS_WIN32) */
glib_init ();
}

--
1.8.2

最新MESON构建glib

使用meson构建glib不需要先编译其他依赖,meson会自动下载subprojects/下的依赖并编译

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# 配置编译所需工具
sudo apt update
sudo apt install binutils python3-mako autoconf intltool git wget mingw-w64
sudo apt install python3-pip && python3 -m pip install meson ninja

# 可配置代理来加速依赖源码下载
# export http_proxy=http://172.27.192.1:7890
# export https_proxy=http://172.27.192.1:7890

wget https://mirrors.ustc.edu.cn/gnome/sources/glib/2.74/glib-2.74.0.tar.xz -P ~/src
xz -d ~/src/glib-2.74.0.tar.xz && tar -xvf ~/src/glib-2.74.0.tar && cd ~/build/glib-2.74.0

meson setup _build \
--prefix=/home/leux/glib \
--cross-file ~/cross_file.txt \
-Dbuildtype=release -Dtests=false \
-Dgtk_doc=false -Dman=false \
-Dxattr=true -Ddefault_library=static

ninja -C _build -j 8
ninja -C _build -j 8 install

# 错误:meson.build:1:0: ERROR: Meson version is 0.56.2 but project requires >= 0.60.0
sudo apt install python3-pip && python3 -m pip install meson
/home/leux/.local/bin/meson

# 错误:如果遇到执行 meson 时下载卡住,可手动下载到指定文件夹
将 /home/leux/glib-2.73.3/subprojects/pcre2.wrap 中的 xxx_url 下载后
放到 /home/leux/glib-2.73.3/subprojects/packagecache/ 中
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# cross_file.txt中的内容
[binaries]
c = 'x86_64-w64-mingw32-gcc'
cpp = 'x86_64-w64-mingw32-g++'
ar = 'x86_64-w64-mingw32-ar'
ld = 'x86_64-w64-mingw32-ld'
ranlib = 'x86_64-w64-mingw32-ranlib'
strip = 'x86_64-w64-mingw32-strip'
windres = 'x86_64-w64-mingw32-windres'
windmc = 'x86_64-w64-mingw32-windmc'
pkgconfig = 'x86_64-w64-mingw32-pkg-config'
cmake = 'cmake'

[properties]
c_args = []
c_link_args = []
needs_exe_wrapper = true

[host_machine]
system = 'windows'
cpu_family = 'x86_64'
cpu = 'x86_64'
endian = 'little'