Stack Exchange
Building 32Bit and 64Bit application uitilizing mingw-w64 and cmake
I need to build a shared library on windows (dll) in 32bit and 64bit.
The build is controlled with cmake, the compiler is mingw64
This is my cmake file:

#64Bit Build (works fine)
ADD_LIBRARY(mylib SHARED mylib.c)
SET_TARGET_PROPERTIES(mylib PROPERTIES PREFIX "")
target_link_options(mylib PUBLIC -Wl,--exclude-all-symbols -s -Wl,--gc-sections -Wl,-Map=output.map )

#32Bit Build (linking issues)
ADD_LIBRARY(mylib32 SHARED mylib.c)
SET_TARGET_PROPERTIES(mylib32 PROPERTIES PREFIX "" COMPILE_FLAGS "-m32" LINK_FLAGS "-m32")
target_link_options(mylib32 PUBLIC --verbose -Wl,--exclude-all-symbols -s -Wl,--gc-sections -Wl,-Map=output.map)

I get a lot of complaints during linking due to 32Bit incomatibility of many linked libraries.
I can reduce the amount of complaints, when I am specifically add 32Bit libraries to the search path like this:
target_link_options(mylib32 PUBLIC --verbose -Wl,--exclude-all-symbols -s -Wl,--gc-sections -Wl,-Map=output.map -L/msys64/mingw32/lib -L/msys64/mingw32/i686-w64-mingw32/lib -L/msys64/mingw32/lib/gcc/i686-w64-mingw32/10.3.0)
But there are still some incompatibilities left: like the dllcrt.a which is always linked from the 64Bit mingw path,
which seems kind of hard coded into gcc/ld.
I even tried to use an empty spec fiel, which gave nor results:
target_link_options(mylib32 PUBLIC -specs=myspe --verbose -Wl,--exclude-all-symbols -s -Wl,--gc-sections -Wl,-Map=output.map -L/msys64/mingw32/lib -L/msys64/mingw32/i686-w64-mingw32/lib -L/msys64/mingw32/lib/gcc/i686-w64-mingw32/10.3.0)
Typical error message I get:
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: i386:x86-64 architecture of input file `C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/lib/dllcrt2.o' is incompatible with i386 output

Do you use the same compiler for both 32bit and 64bit executables?

yes, same compiler,

Why do you not use the 32bit compiler for the 32bit executables?

As far as I know it s very complicated to maintain 2 Build environments with cmake for the same project.
The usage of the "-m32" to generate i386 output seems to be a common usecase, so initially it looked like the much simpler way.

If the compiler had multilib enabled, then -m32 would work, because the 32bit libraries would be included as well. Not sure if anyone still builds multilib compilers for mingw.

Ok, thanks a lot, I have not been aware of the "multilib" keyword.
I installed Mingw via msys2, but apperently there are other distributions which have multilib enabled, at least according to this thread:
https://stackoverflow.com/questions/59685298/compile-for-32-bit-architectures-using-mingw-w64

I just figured out a solution for my case:
the crt* stuff is startup code, which is not needed in the case of libraries.
So I added the -nostartfiles flag, and my library links fine.
I guess in case off an application one can probably link the 32bit startfiles manualy as a workaround.
SET_TARGET_PROPERTIES(amprio_seednkey32 PROPERTIES PREFIX "" COMPILE_FLAGS "-m32 -nostartfiles" LINK_FLAGS "-m32 -nostartfiles")
Anyhow, this all feels like a dirty Workaround, I still wonder what is the best way to link 32Bit and 64Bit using mingw-w64.
