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

时间:2023-01-22 07:09:58

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安装mingw-w64安装mingw-w64。我可以使用以下命令编译并链接我的程序的64位版本:

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

Which generates a 64-bit app.exe file.

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

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

我使用什么二进制或命令行标志来生成一个32位的应用程序版本?

1 个解决方案

#1


25  

That depends on which variant of toolchain you're currently using. Both DWARF and SEH variants (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 SJLJ distributions are indeed dual-target, and in order to build 32-bit target, just supply -m32 flag. If that doesn't work, then just build with i686-w64-mingw32-g++.

这取决于您当前使用的工具链的变体。矮人和SEH变体(从GCC 4.8.0开始)都只是单一目标。您可以通过检查其发行版的目录结构来自己查看它,例如,它们只包含64位和32位寻址的库,而不是同时包含64位和32位寻址的库。另一方面,普通的旧SJLJ发行版确实是双目标的,为了构建32位目标,只需提供-m32标志。如果不行,那么使用i686-w64-mingw32-g+构建。

BONUS


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

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

  1. libgcc_s_dw2-1.dll (DWARF);
  2. libgcc_s_dw2-1。dll(矮);
  3. libgcc_s_seh-1.dll (SEH);
  4. libgcc_s_seh-1。dll(医师);
  5. libgcc_s_sjlj-1.dll (SJLJ).
  6. 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. 检查MinGW-w64安装的目录和文件结构,希望找到其中一个dll(通常在bin中);或
  3. 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 Walker on Windows); or
  4. 构建一些涉及异常处理的真实或测试c++代码,以强制连接其中一个dll,然后查看构建目标所依赖的哪个dll(例如,可以在Windows上看到依赖Walker);或
  5. 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.
  6. 使用蛮力方法,将一些测试代码编译到程序集(而不是机器代码),并查找出现的引用,如___gxx_personality_v* (DWARF)、___gxx_personality_seh* (SEH)、___gxx_personality_sj* (SJLJ);请参阅获得当前GCC异常模型。

#1


25  

That depends on which variant of toolchain you're currently using. Both DWARF and SEH variants (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 SJLJ distributions are indeed dual-target, and in order to build 32-bit target, just supply -m32 flag. If that doesn't work, then just build with i686-w64-mingw32-g++.

这取决于您当前使用的工具链的变体。矮人和SEH变体(从GCC 4.8.0开始)都只是单一目标。您可以通过检查其发行版的目录结构来自己查看它,例如,它们只包含64位和32位寻址的库,而不是同时包含64位和32位寻址的库。另一方面,普通的旧SJLJ发行版确实是双目标的,为了构建32位目标,只需提供-m32标志。如果不行,那么使用i686-w64-mingw32-g+构建。

BONUS


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

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

  1. libgcc_s_dw2-1.dll (DWARF);
  2. libgcc_s_dw2-1。dll(矮);
  3. libgcc_s_seh-1.dll (SEH);
  4. libgcc_s_seh-1。dll(医师);
  5. libgcc_s_sjlj-1.dll (SJLJ).
  6. 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. 检查MinGW-w64安装的目录和文件结构,希望找到其中一个dll(通常在bin中);或
  3. 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 Walker on Windows); or
  4. 构建一些涉及异常处理的真实或测试c++代码,以强制连接其中一个dll,然后查看构建目标所依赖的哪个dll(例如,可以在Windows上看到依赖Walker);或
  5. 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.
  6. 使用蛮力方法,将一些测试代码编译到程序集(而不是机器代码),并查找出现的引用,如___gxx_personality_v* (DWARF)、___gxx_personality_seh* (SEH)、___gxx_personality_sj* (SJLJ);请参阅获得当前GCC异常模型。