MinGW
openSUSE 发行版 Leap 和 Tumbleweed 提供 mingw-w64 工具链,用于交叉编译到 Windows 环境。优点是更快的构建时间(假定进程创建更快、文件系统更快以及系统调用开销通常更小)。如果可用 WINE,则可以在 Linux 环境中之后运行这些二进制文件。
命名
软件包通常称为 mingw64-* 用于 Windows x64,mingw32-* 用于 Windows x86 目标。从所有架构(包括非 x86)都可以进行交叉编译到 Windows x64——这就是交叉编译的意义。 [如果将来提供针对 Windows ARM(32 或 64)的编译,则需要添加新的唯一前缀,这些前缀不会与 mingw64- 冲突。]
GNU 构建三元组是x86_64-w64-mingw32和i686-w64-mingw32(CPU-VENDOR-OS)分别用于 x64 和 x86。供应商字符串很少甚至从不被评估。操作系统字符串只是一个固定的字符串,大致说明了语义和可用的程序集(32 没有任何特殊含义)。例如,Cygwin 使用x86_64-pc-cygwin.
编译器
安装交叉编译工具链,例如:
zypper in mingw64-cross-gcc-c++ mingw64-cross-pkgconf
有了这个,就有了必不可少的 Hello World 演示
$ echo -en '#include <cstdio>\n' 'int main() { printf("Hello world\\n"); }' >x.cpp
$ x86_64-w64-mingw32-g++ x.cpp
$ wine ./a.exe
007c:fixme:hid:handle_IRP_MN_QUERY_ID Unhandled type 00000005
007c:fixme:hid:handle_IRP_MN_QUERY_ID Unhandled type 00000005
007c:fixme:hid:handle_IRP_MN_QUERY_ID Unhandled type 00000005
007c:fixme:hid:handle_IRP_MN_QUERY_ID Unhandled type 00000005
007c:fixme:wineusb:query_id Unhandled ID query type 0x5.
Hello world
构建三元组对于使用 autoconf 的任何软件都非常有用,它将自动找到交叉工具链
$ ./configure --host=x86_64-w64-mingw32 configure: loading site script /usr/share/site/x86_64-pc-linux-gnu checking for a BSD-compatible install... /usr/bin/install -c checking whether build environment is sane... yes checking for x86_64-w64-mingw32-strip... x86_64-w64-mingw32-strip checking for a race-free mkdir -p... /usr/bin/mkdir -p checking for gawk... gawk checking whether make sets $(MAKE)... yes checking whether make supports nested variables... yes checking how to create a pax tar archive... gnutar checking whether make supports nested variables... (cached) yes checking for x86_64-w64-mingw32-gcc... x86_64-w64-mingw32-gcc checking whether the C compiler works... yes checking for C compiler default output file name... a.exe …
请注意,交叉编译器位于mingw64-cross-gcc(在 Linux 上运行,为 Windows 生成);而mingw64-gcc软件包另一方面是一个本地编译器(换句话说,在 Windows/WINE 上运行并为 Windows 生成)。
附加软件包
在基本发行版(Leap、Tumbleweed)中,仅包含构建 openSUSE 启动器所需的最低限度的工具链 WSL 并可能是一些 EFI 可执行文件。大量的库和图形工具包在额外的仓库中提供windows:mingw:win64和windows:mingw:win32。安装例如:
# zypper ar obs://windows:mingw:win64 mingw64 # zypper in mingw64-wxWidgets-3_2-devel
使用 WINE 进行测试启动
WINE 的默认搜索路径仅包含例如:/usr/lib64/wine/x86_64-windows。这足以满足上述仅依赖于kernel32.dll和msvcrt.dll(两者均由 WINE 提供)的“Hello World”示例,但需要将其他库复制或符号链接到可执行文件的目录中,例如:
# zypper in mingw64-xxhash-devel
$ echo -en '#include <xxhash.h>\n' 'int main() { return XXH_versionNumber(); }' >x.cpp
$ x86_64-w64-mingw32-g++ x.cpp -lxxhash
$ wine a.exe
<other WINE warnings…>
00dc:err:module:import_dll Library libxxhash.dll (which is needed by L"Z:\\tmp\\a.exe") not found
00dc:err:module:loader_init Importing dlls for L"Z:\\tmp\\a.exe" failed, status c0000135
$ ln -s /usr/x86_64-w64-mingw32/sys-root/mingw/bin/libxxhash.dll .
$ wine a.exe
<WINE warnings…>
(No other output, and success)