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.