Exploit mingw and wine for cross-platform portability testing

November 28th, 2007 by khermans

Your rating: None

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!!!
__________________________
Kristian Erik Hermansen is a Linux Journal Reader Advisory Panelist. Track Me.

AttachmentSize
hello.txt420 bytes


Special Magazine Offer -- 2 Free Trial Issues!
Receive 2 free trial issues of Linux Journal as well as instant online access to current and past issues. There's NO RISK and NO OBLIGATION to buy. CLICK HERE for offer

Linux Journal: delivering readers the advice and inspiration they need to get the most out of their Linux systems since 1994.

Sorry, offer available in the US only. International orders, click here.

Comment viewing options

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

Handy Dandy

On November 28th, 2007 gnome says:

Very nice! Thanks for the tip.

__________________________

Adam Dutko is a Linux Journal Reader Advisory Panelist.
"...thanks for all the fish..."
http://littlehat.homelinux.org:8000

Featured Videos

The X Window System is a magnificent platform for many uses, but using it to run an application over a slow network is nearly impossible. This is an introduction to NX, a technology that makes remote applications fly even over commodity internet.

Linux Journal Gadget Guy, Shawn Powers, reviews the Flip Video Ultra, a small portable video camera, and shows us how easy it is to edit the video with Kino.

Thanks to our sponsor: Silicon Mechanics

From the Magazine

September 2008, #173

Feeling a bit like a Thermian? Never give up, never surrender! Someday, you could go from underdog to top dog. Just take a look at a few of the underdogs we highlight in this issue: Mutt, djbdns, Nginix, Gentoo, Xara and the program voted mostly likely to fail just a few years back—Firefox. If Firefox not radical enough for you, check out Chef Marcel's column for some more alternatives. Having trouble mapping your program data to your relational database? If so, Rueven Lerner shows you some tricks in his At The Forge column.

Need to run GUI applications on your server in the next state? In his Paranoid Penguin column, Mick Bauer shows you how to do it securely. Kyle Rankin keeps hacking and slashing and shows you a few split screen secrets you may not be familiar with. Finally, we all know what happens next February, but only Doc knows what happens afterward.

Read this issue