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!!!

AttachmentSize
hello.txt420 bytes

Comments

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..."
http://littlehat.homelinux.org

White Paper
Linux Management with Red Hat Satellite: Measuring Business Impact and ROI

Linux has become a key foundation for supporting today's rapidly growing IT environments. Linux is being used to deploy business applications and databases, trading on its reputation as a low-cost operating environment. For many IT organizations, Linux is a mainstay for deploying Web servers and has evolved from handling basic file, print, and utility workloads to running mission-critical applications and databases, physically, virtually, and in the cloud. As Linux grows in importance in terms of value to the business, managing Linux environments to high standards of service quality — availability, security, and performance — becomes an essential requirement for business success.

Learn More

Sponsored by Red Hat

White Paper
Private PaaS for the Agile Enterprise

If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.

Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.

Learn More

Sponsored by ActiveState