Linux 如何使用 mingw-w64 编译和链接 32 位 Windows 可执行文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19690504/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 01:14:15  来源:igfitidea点击:

How do I compile and link a 32-bit Windows executable using mingw-w64

linuxmingw32bit-64bit32-bitmingw-w64

提问by user103935

I am using Ubuntu 13.04 and installed mingw-w64 using apt-get install mingw-w64. I can compile and link a working 64-bit version of my program with the following command:

我正在使用 Ubuntu 13.04 并使用apt-get install mingw-w64. 我可以使用以下命令编译和链接我的程序的工作 64 位版本:

x86_64-w64-mingw32-g++ code.cpp -o app.exe

Which generates a 64-bit app.exefile.

它生成一个 64 位app.exe文件。

What binary or command line flags do I use to generate a 32-bit version of app.exe?

我使用什么二进制或命令行标志来生成 32 位版本的 app.exe?

采纳答案by Alexander Shukaev

That depends on which variant of toolchain you're currently using. Both DWARFand SEHvariants (which come starting from GCC 4.8.0) are only single-target. You can see it yourself by inspecting the directory structure of their distributions, i.e. they contain only the libraries with either 64- and 32-bit addressing, but not both. On the other hand, plain old SJLJdistributions are indeed dual-target, and in order to build 32-bit target, just supply -m32flag. If that doesn't work, then just build with i686-w64-mingw32-g++.

这取决于您当前使用的工具链的哪个变体。既DWARFSEH变体(它们来自GCC 4.8.0开始)仅单一目标。您可以通过检查其发行版的目录结构自行查看,即它们仅包含具有 64 位和 32 位寻址的库,但不能同时包含两者。另一方面,普通的旧SJLJ发行版确实是双目标,为了构建 32 位目标,只需提供-m32标志。如果这不起作用,那么只需使用i686-w64-mingw32-g++.

BONUS

奖金



By the way, the three corresponding dynamic-link libraries(DLLs) implementing each GCC exception modelare

顺便说一下,实现每个GCC 异常模型的三个相应的动态链接库(DLL)是

  1. libgcc_s_dw2-1.dll(DWARF);
  2. libgcc_s_seh-1.dll(SEH);
  3. libgcc_s_sjlj-1.dll(SJLJ).
  1. libgcc_s_dw2-1.dll(矮人);
  2. libgcc_s_seh-1.dll(SEH);
  3. libgcc_s_sjlj-1.dll(SJLJ)。

Hence, to find out what exception model does your current MinGW-w64 distribution exactly provide, you can either

因此,要找出您当前的 MinGW-w64 发行版究竟提供了什么异常模型,您可以

  1. inspect directory and file structure of MinGW-w64 installation in hope to locate one of those DLLs (typically in bin); or
  2. build some real or test C++ code involving exception handling to force linkage with one of those DLLs and then see on which one of those DLLs does the built target depend (for example, can be seen with Dependency Walkeron Windows); or
  3. take brute force approach and compile some test code to assembly (instead of machine code) and look for presence of references like ___gxx_personality_v*(DWARF), ___gxx_personality_seh*(SEH), ___gxx_personality_sj*(SJLJ); see Obtaining current GCC exception model.
  1. 检查 MinGW-w64 安装的目录和文件结构,希望找到这些 DLL 之一(通常在 中bin);或者
  2. 构建一些涉及异常处理的真实或测试 C++ 代码,以强制与这些 DLL 之一进行链接,然后查看构建的目标依赖于这些 DLL 中的哪一个(例如,可以在 Windows 上使用Dependency Walker进行查看);或者
  3. 采取蛮力方法并将一些测试代码编译为汇编(而不是机器代码),并查找诸如___gxx_personality_v*(DWARF)、___gxx_personality_seh*(SEH)、___gxx_personality_sj*(SJLJ) 之类的引用的存在;请参阅获取当前的 GCC 异常模型