Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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:

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

Which generates a 64-bit app.exe file.

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

like image 661
user103935 Avatar asked Oct 30 '13 18:10

user103935


People also ask

Can MinGW-w64 compile 32-bit?

MinGW64 is an open source C/C++ compiler for WIndows platforms based on the popular gcc available on Linux and on many other Operating Systems; Mingw64 can create both 32 and 64 bit binary executables.

Is MinGW only 32-bit?

MinGW can be run either on the native Microsoft Windows platform, cross-hosted on Linux (or other Unix), or "cross-native" on Cygwin. Although programs produced under MinGW are 32-bit executables, they can be used both in 32 and 64-bit versions of Windows.

How do I change my MinGW 32 bit to 64-bit?

open a cmd.exe and do set PATH=C:\mingw64\bin;%PATH% for 64-bit building. set PATH=C:\mingw32\bin;%PATH% for 32-bit building. You should be ready to go. Execute i686-w64-mingw32-gcc -v or x86_86-w64-mingw32-gcc -v to see that everything has gone well.


1 Answers

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.
like image 138
Alexander Shukaev Avatar answered Sep 24 '22 05:09

Alexander Shukaev