Cross-compilation in Linux

Sometimes it is necessary to create Windows application from Linux. I will briefly introduce the method for it in the article.

The basic principle is simple and common for all cross-compilations (e.g., creating Symbian applications in Linux/Windows or other desktop OS):
  1. Get and install a toolchain for target platform (compiler, linker and other tools);
  2. Compile all required frameworks using this toolchain (for example, GStreamer, Qt, wxWidgets etc)
  3. Compile your own project with this toolchain and precompiled frameworks.
For creating Windows applications from Linux you can use MingWG: Minimalist GNU for Windows. The installation is pretty easy:

$ sudo apt-get install mingw32


As an example, let's compile a simple Windows application with a message box (msgbox.c file):


#include <windows.h>
 
INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine, int nCmdShow)
{
  MessageBoxW(0, L"Hello from Linux!\n", L"MinGW app", MB_ICONEXCLAMATION);

  return 0;
}


MingGW includes all headers and libraries to compile such kind of the applications. If you want to use something serious (like Qt-based applications), you have to precompile Qt from sources using MinGW.

Compilation is pretty easy too for that case:

$ i586-mingw32msvc-cc msgbox.c -o msgbox.exe -Wl,-subsystem,windows

MinGW contains two kind of compilers - for x86 and for x86_64 platforms, so use the required compiler. Additionally, we set up the subsystem flag (MinGW compile applications as console-based by default).

That's all. Upload the the output application to the Windows OS and enjoy.

Comments

Popular posts from this blog

DIY: Business cards in LaTeX

Python vs JS vs PHP for embedded systems

Shellcode detection using libemu