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
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>
#include <unistd.h>

int main() {
 int wait = 5;
#ifdef _WIN32
 return 0;
khermans@khermans-laptop:~/projects$ gcc -std=c99 -Wall -o hello hello.c
khermans@khermans-laptop:~/projects$ time ./hello

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

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


Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Handy Dandy

gnome's picture

Very nice! Thanks for the tip.

Adam Dutko is a Linux Journal Reader Advisory Panelist.
"...thanks for all the fish..."

One Click, Universal Protection: Implementing Centralized Security Policies on Linux Systems

As Linux continues to play an ever increasing role in corporate data centers and institutions, ensuring the integrity and protection of these systems must be a priority. With 60% of the world's websites and an increasing share of organization's mission-critical workloads running on Linux, failing to stop malware and other advanced threats on Linux can increasingly impact an organization's reputation and bottom line.

Learn More

Sponsored by Bit9

Linux Backup and Recovery Webinar

Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.

In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.

Learn More

Sponsored by Storix