How to Compile and Link a 32-Bit Windows Executable Using Mingw-W64

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.

Compile for 32-bit Architectures using MinGW-w64

The version you are using is not multilib enabled so you won't be able to compile 32-bit programs using the MinGW-w64.

You need this version for using it on a 64-bit platform but in order to be able to compile both 32-bit and 64-bit.

Configure MinGW-w64 for 32bit and 64bit Executables

What I do is set up seperate MinGW-w64 toolchains, one for 32-bit and one for 64-bit Windows.

That way there is no confusion and the 2 won't ever accidentally get mixed up if some parameter is forgotten.

You ca get a recent version of both toolchains from http://winlibs.com/

MinGW64 cannot compile 32bit code

It is not multilib enabled. That's why you are not able to compile 32-bit(x86) program. You can get multilib enabled toolchain from following link:

For 64-bit machine: 64-Bit

For 32-bit machine: 32-Bit

Using MinGW-Builds to compile a 32-bit exe on a 64-bit system -- compiles a 32-bit exe but links against 64-bit DLLs

You don't specify the bitness of a DLL when you link against it. You merely specify the name of the DLL. It's up to the loader to find the DLLs with the names that you have specified.

Two of the DLLs that you name, ntdll and msvcrt, are system components. They reside in the system32 directory. Your 32 bit process is subject to the file system redirector. That means that when the loader looks in system32, the redirector silently maps that to syswow64, the 32 bit system directory. That happens transparently so the loader will find 32 bit versions of ntdll and msvcrt.

The error code that you report is 0xC000007B. That is an NTSTATUS error code. Specifically STATUS_INVALID_IMAGE_FORMAT. Which is indeed the error reported when you attempt to load a 64 bit DLL in a 32 bit process. So it seems likely that this is what is happening.

You've done some debugging with Dependency Walker. This is an excellent tool for the job, but in the mode you have used it in (static mode) it does sometimes report false positives. It sounds like you are using the 64 bit version of Dependency Walker. You might get better diagnostics with the 32 bit version.

All that said, what you really need to do is to run your program under the Profile mode of Dependency Walker. That can be found under the Profile menu. When you do this you'll get a stream of diagnostics from the loader, including the name of the DLL that failed to load with error STATUS_INVALID_IMAGE_FORMAT. At that point it should become clear what has gone wrong.



Related Topics



Leave a reply



Submit