Boston Linux & Unix (BLU) Home | Calendar | Mail Lists | List Archives | Desktop SIG | Hardware Hacking SIG
Wiki | Flickr | PicasaWeb | Video | Maps & Directions | Installfests | Keysignings
Linux Cafe | Meeting Notes | Blog | Linux Links | Bling | About BLU

BLU Discuss list archive


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

HOWTO: Hack and test cross-platform code from Linux (mingw/wine)



I frequently need to write common C code that runs on Linux, UNIX, and
Windows -- from the same source.  Sometimes it is beneficial for me to
cross-compile from Linux for Windows in testing.  Here's a very simple
example that will get you hacking win32 code on Linux :-)

khermans at 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 at khermans-laptop:~/projects$ wine --version
wine-0.9.30
khermans at khermans-laptop:~/projects$ emacs -nw hello.c
khermans at khermans-laptop:~/projects$ cat hello.c
/*
 * Author: Kristian Hermansen <kristian.hermansen at gmail.com>
 * For demonstration purposes of cross-compilation...
 */

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

/* platform-secific 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 at khermans-laptop:~/projects$ gcc -std=c99 -Wall -o hello hello.c
khermans at khermans-laptop:~/projects$ time ./hello
hello!
goodbye!

real    0m5.005s
user    0m0.000s
sys     0m0.004s
khermans at khermans-laptop:~/projects$ i586-mingw32msvc-gcc -std=c99
-Wall -o hello.exe hello.c
khermans at khermans-laptop:~/projects$ time wine hello.exe
hello!
goodbye!

real    0m5.295s
user    0m0.004s
sys     0m0.012s
khermans at 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
-- 
Kristian Hermansen

-- 
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.





BLU is a member of BostonUserGroups
BLU is a member of BostonUserGroups
We also thank MIT for the use of their facilities.

Valid HTML 4.01! Valid CSS!



Boston Linux & Unix / webmaster@blu.org