Exploit mingw and wine for cross-platform portability testing

I frequently need to write portable C code that runs on Linux, UNIX, and Windows. Sometimes it is beneficial to cross-compile from Linux for Windows during testing. Here is a very simple example that will get you hacking win32 code on Linux by utilizing mingw and wine :-)


khermans@khermans-laptop:~/projects$ aptitude search mingw
i   mingw32                         - Minimalist GNU win32 (cross) compiler
i   mingw32-binutils                - Minimalist GNU win32 (cross) binutils
i   mingw32-runtime                 - Minimalist GNU win32 (cross) runtime
khermans@khermans-laptop:~/projects$ wine --version
wine-0.9.30
khermans@khermans-laptop:~/projects$ emacs -nw hello.c
khermans@khermans-laptop:~/projects$ cat hello.c
/*
 * Author: Kristian Hermansen <kristian.hermansen@gmail.com>
 * For demonstration purposes of cross-compilation...
 */

/* general includes */
#include <stdio.h>

/* platform-specific includes  */
#ifdef _WIN32
#include <windows.h>
#else
#include <unistd.h>
#endif

int main() {
 int wait = 5;
 printf("hello!\n");
#ifdef _WIN32
 Sleep(wait*1000);
#else
 sleep(wait);
#endif
 printf("goodbye!\n");
 return 0;
}
khermans@khermans-laptop:~/projects$ gcc -std=c99 -Wall -o hello hello.c
khermans@khermans-laptop:~/projects$ time ./hello
hello!
goodbye!

real    0m5.005s
user    0m0.000s
sys     0m0.004s
khermans@khermans-laptop:~/projects$ i586-mingw32msvc-gcc -std=c99 -Wall -o hello.exe hello.c
khermans@khermans-laptop:~/projects$ file hello.exe
hello.exe: MS-DOS executable PE for MS Windows (console) Intel 80386 32-bit
khermans@khermans-laptop:~/projects$ time wine hello.exe
hello!
goodbye!

real    0m5.295s
user    0m0.004s
sys     0m0.012s
khermans@khermans-laptop:~/projects$ ls -lsh
total 212K
 12K -rwxr-xr-x 1 khermans khermans 9.1K 2007-04-08 12:54 hello
4.0K -rw-r--r-- 1 khermans khermans  418 2007-04-08 12:53 hello.c
196K -rwxr-xr-x 1 khermans khermans 194K 2007-04-08 12:54 hello.exe

So there you have it. There is no need to boot into Windows for testing. Simply utilize mingw and wine. Now, go forth and start hacking!!!

hello.txt420 bytes