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

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- or 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++.

BONUS


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

  1. libgcc_s_dw2-1.dll (DWARF);
  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

  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 Walker on 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.

Leave a Comment