MinGW Under Wine HowtoMy take on a simple lazy way to install and use MinGW under linux On several occasions i've needed to produce a windows and a linux version of a simple utility. If you have multiple virtual machines on your desktop then its probably a non issue, but I have (for many reasons) a separate Windows and Linux machine in my office. Even with the common NFS/SMB file server in the middle it can get a bit tedious to compile and test on machine one, then move chair and do it again on machine 2. I decided the best way to build windows a linux binaries on the same machine. It is possible to roll the MinGW patch set into the gcc source and build a version of gcc with a new target, no thanks! Life is too short. A simpler way is to install MinGW under Wine, then use common bash build scripts to build binaries for both targets.
The following is a step by step guide and some example scripts. Step 1, install and test Wine: Currently im using Fedora Core, or more accurately my machine has been using Fedora core for 4 years and I cant be bothered with pain of reinstalling it. Installing Wine is pretty simple, use "yum install wine". Debian based systems can just "apt-get install wine". Step 2, Download and install MinGW. Assuming the Wine install worked this is really simple. Currently the stable release
of MinGW is 5.4.1, so find and download "MinGW-5.1.4.exe", from a bash prompt make it executable "chmod +x ./MinGW-5.1.4.exe" then
just run the installer under Wine "./MinGW-5.1.4.exe". If Wine is working correctly then you should get the graphical windows
installer, just keep clicking next until you get a default install. [jon ~]% ls -l .wine/drive_c/MinGW/ total 320 drwxrwxr-x 2 jon jon 4096 2009-09-17 15:30 bin -rw-rw-r-- 1 jon jon 17992 2000-12-18 22:47 COPYING -rw-rw-r-- 1 jon jon 26430 2001-01-29 14:32 COPYING.LIB drwxrwxr-x 3 jon jon 4096 2009-09-17 15:30 doc drwxrwxr-x 5 jon jon 12288 2009-09-17 15:30 include drwxrwxr-x 2 jon jon 4096 2009-09-17 15:30 info -rw-rw-r-- 1 jon jon 245 2009-09-17 15:30 installed.ini drwxrwxr-x 3 jon jon 4096 2009-09-17 15:30 lib drwxrwxr-x 3 jon jon 4096 2009-09-17 15:30 libexec drwxrwxr-x 5 jon jon 4096 2009-09-17 15:30 man drwxrwxr-x 4 jon jon 4096 2009-09-17 15:30 mingw32 -rwxrwxr-x 1 jon jon 140095 2009-09-17 15:28 MinGW-5.1.4.exe -rw-rw-r-- 1 jon jon 1353 2009-09-17 15:31 mingw.ini -rw-rw-r-- 1 jon jon 1327 2008-04-26 14:43 mingw.ini.old -rw-rw-r-- 1 jon jon 48 2009-09-17 15:30 MinGW.url -rwxrwxr-x 1 jon jon 67620 2009-09-17 15:30 uninst.exe Step 3, Add some paths. In my case to make life easy I wanted all users to have a path to a copy of MinGW, so I added this "PATH=:/sbin:/.wine/drive_c/MinGW/bin" to the bottom of /etc/bashrc. Now logout for the user, and log back in. Check path has worked with "echo $PATH" then try this and check you have two working compilers: [jon ~]% gcc --version gcc (GCC) 4.1.2 20070925 (Red Hat 4.1.2-27) Copyright (C) 2006 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. [jon ~]% gcc.exe --version gcc.exe (GCC) 3.4.5 (mingw special) Copyright (C) 2004 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. Step 4 This is a simple example program and build script to illustrate how to tie it all together, cut&paste this into a file called compilertest.c Check it compiles as a Linux and Windows executable like this :
#include <stdio.h>
main()
{
printf("Useless test program\n");
}
[jon compilertest]% cc compilertest.c
[jon compilertest]% gcc.exe compilertest.c
[jon compilertest]% ls -l
total 28
-rwxrwxr-x 1 jon jon 15852 2009-09-18 14:54 a.exe
-rwxrwxr-x 1 jon jon 4818 2009-09-18 14:54 a.out
-rw-rw-r-- 1 jon jon 114 2009-09-18 14:53 compilertest.c
[jon compilertest]% file a.out
a.out: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.9, not stripped
[jon compilertest]% file a.exe
a.exe: MS-DOS executable PE for MS Windows (console) Intel 80386 32-bit
[jon compilertest]% ./a.out
Example useless program
[jon compilertest]% ./a.exe
Example useless program
Step 5 Tie it all togther with a bash build script, note the "-D MINGW" argument in gcc.exe line, this sets a definition for a preprocessor variable MINGW. If you have any windows specific code then you can wrap it in #ifdef MINGW #endif.
#!/bin/bash
# Example script called "compile"
#
gcc compilertest.c -o compilertest
if [ -f ./compilertest ]; then
echo "Ok, built linux executable"
fi
if [ -f $HOME/.wine/drive_c/MinGW/bin/gcc.exe ]; then
gcc.exe compilertest.c -o compilertest.exe -D MINGW
if [ -f ./compilertest.exe ]; then
echo "Ok, built Windows executable"
fi
fi
All content on this site is (c) 2010 Jonathan Andrews, This article is marked as free and open. Feel free to copy and or republish it |