skeeto / w64devkit

Portable C and C++ Development Kit for x64 (and x86) Windows
The Unlicense
3k stars 211 forks source link

How to build win32 exe #36

Open sovo-code opened 1 year ago

sovo-code commented 1 year ago

First, this is a good project. It helps me much. But, I used to try to build win32 exe by "gcc test.cpp -m32". It can't work. I have seeked many methods, but there is not a useful method. Hopefully there will be a method.

skeeto commented 1 year ago

The 32-bit and 64-bit toolchains are distributed separately, so the -m32 option is unavailable. Download the 32-bit toolchain, w64devkit-i686, and then compile without -m32.

I separate them because targeting 32-bit Windows is a niche case, and simultaneously targeting 32-bit and 64-bit is even more niche. I don't want to burden the common case with a redundant toolchain.

Peter0x44 commented 1 year ago

Perhaps a "multilib" variant should be provided for users who wish to have a compiler that can target both? If that is not too much effort, of course

skeeto commented 1 year ago

Eh, maybe. I'd rather figure out how to reliably place both 32-bit and 64-bit w64devkit in $PATH at the same time. Then instead of -m32 you'd have the tool prefix (i.e. x86_64-w64-mingw32-gcc, i686-w64-mingw32-gcc) for selecting one or the other. Currently the lower priority toolchain gets confused and calls the wrong cc1/etc. despite an explicit sysroot otherwise. It's possible to work around with "gcc -B" but that's undesirable.

At minimum, a multilib w64devkit would still be a 64-bit toolchain, but with that -m32 switch. As a bonus, it should also have i686-w64-mingw32 prefixed tools. Even if I don't distribute a build, I'd be willing to accept a good "variant" patch, but unfortunately it's not a simple matter of flipping the --disable-multilib and --disable-lib32 options. The build still fails (in mingw-w64-headers at the earliest) and it's not obvious why.

smallpotato000 commented 1 year ago

To my surprise, this works in Windows XP (32bit). Just download w64devkit-i686-1.17.0.zip, extract it somewhere, run w64devkit.exe to get a shell. Then create helloworld.c, and compile it with gcc helloworld.c -o helloworld.exe. It produced a Win32 PE file which can also run in Windows XP.

skeeto commented 1 year ago

Yup! Support for 32-bit XP is no accident. I've made changes (edb4ebe, d1545f1, and 8e31eae) to keep it working since I enjoy developing in that environment. Though the builds I distribute require a Pentium 4 or later, so it won't work on hardware more than around 20 years old. GCC generates far better SSE floating point code than it does x87, so I figure it's worth the trade-off. For anyone following along: In a pinch, you can build without this requirement by changing "--with-arch" in variant-i686.patch.

gwald commented 1 year ago

winXP support works great! thanks heaps for this! And the exe's also work great on newer windows also... generated .exe tested on 64bit win7 & win10! and with no extra dll's required!! :+1: :+1: :+1: :+1:

This will be my new gcc for windows :)