Build Windows executables on Linux
If you have to build a binary ( .exe, .dll, … ) from source code for LINUX and WINDOWS, you need at least one build environment for each operating system.
In today’s world, this is not a big deal, because we can have as many virtual machines as we want thanks to VMWARE ( or VirtualBox ).
But this could become a complex environment and might also increase license costs to name just a few problems.
Many of us use Linux as our primary operating system. So the question is: “Can we use Linux to compile and link Windows executables?”
The concept of targeting a different platform than the compiler is running on is not new, and is known as cross-compilation.
Cross-compiling Windows binaries on Linux may have many benefits to it.
- Reduced operating system complexity.
On cross-platform projects that are also built on Linux, we can get one less operating system to maintain.
- Access to Unix build tools.
Build tools such as make, autoconf, automake and Unix utilities as grep, sed, and cat, to mention a few, become available for use in Windows builds as well. Even though projects such as MSYS port a few of these utilities to Windows, the performance is generally lower, and the versions are older and less supported than the native Unix counterparts. Also, if you already have a build environment set up under Linux, you don’t have to set it up again on Windows, but just use the existing one.
- Lower license costs.
As we know, Windows costs in terms of license fees. Building on Linux, developers do not need to have a Windows installation on their machines, but maybe just a central Windows installation for testing purposes.
On a Linux build environment, a gcc that compiles native binaries is usually installed in “/usr/bin”.
Native headers and libraries are in turn found in “/usr/include” and “/usr/lib”, respectively.
We can see that all these directories are rooted in “/usr”.
Any number of cross-compiler environments can be installed on the same system, as long as they are rooted in different directories.
To compile and link a Windows executable on Linux do the following
(1) Go to the MinGW-w64 download page.
For 64Bit, open “Toolchains targetting Win64″ , followed by “Automated Builds” and download a recent version to /tmp
For 32Bit, open “Toolchains targetting Win32″ , followed by “Automated Builds” and download a recent version to /tmp
(2) Create 2 directories mkdir /opt/mingw32 and mkdir /opt/mingw64
(3) Unpack the .b2z files to the according directories
For 64Bit tar xvf mingw-w64-bin_x86_64-linux_20131228.tar.bz2 -C /opt/mingw64
For 32Bit tar xvf mingw-w32-bin_x86_64-linux_20131227.tar.bz2 -C /opt/mingw32
(4) Create a new hello.c file in /tmp and paste the following code into it
#include <stdio.h>
int main()
{
printf("Hello World!\n");
return 0;
}
(5) Next, you can build the Windows binaries using the following commands
For 64Bit /opt/mingw64/bin/x86_64-w64-mingw32-gcc /tmp/hello.c -o /tmp/hello-w64.exe
For 32Bit /opt/mingw32/bin/i686-w64-mingw32-gcc /tmp/hello.c -o /tmp/hello-w32.exe
You now have to Windows binaries that can be downloaded to a Windows environment.
Well, this is just a simple sample and for more complex projects you probably have to do a little more work, but it should give you the idea, how cross-compiling can be implemented.