This post was originally published on SeptemberÂ 26, 2010. The original can be found here.
Sometimes you just have to compile Windows programs from the comfort of your Linux install. This is a relatively simple process that basically requires you to only install the following (Ubuntu) packages:
To compile 32-bit programs
- mingw32 (swap out for gcc-mingw32 if you need 64-bit support)
Additionally for 64-bit programs (*PLEASE SEE NOTE)
Once you have those packages you just need to swap out â€œgccâ€ in your normal compile commands with either â€œi586-mingw32msvc-gccâ€ (for 32-bit) or â€œamd64-mingw32msvc-gccâ€ (for 64-bit). So for example if we take the following hello world program in C
int main(int argc, char** argv)
we can compile it to a 32-bit Windows program by using something similar to the following command (assuming the code is contained within a file called main.c)
i586-mingw32msvc-gcc -Wall â€œmain.câ€ -o â€œProgram.exeâ€
You can even compile Win32 GUI programs as well. Take the following code as an example
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
char *msg = â€œThe message boxâ€™s message!â€;
MessageBox(NULL, msg, â€œMsgBox Titleâ€, MB_OK | MB_ICONINFORMATION);
this time Iâ€™ll compile it into a 64-bit Windows application using
amd64-mingw32msvc-gcc -Wall -mwindows â€œmain.câ€ -o â€œProgram.exeâ€
You can even test to make sure it worked properly by running the program through wine like
You might need to install some extra packages to get Wine to run 64-bit applications but in general this will work.
Thatâ€™s pretty much it. You might have a couple of other issues (like linking against Windows libraries instead of the Linux ones) but overall this is a very simple drop-in replacement for your regular gcc command.
*NOTE: There is currently a problem with the Lucid packages for the 64-bit compilers. As a work around you can get the packages from this PPA instead.
Originally posted on my personal website here.