GCC for Windows on Mac OS X
Two toolchains based on GCC are popular on Windows:
Cygwin,
a whole development environment which includes also many other
programs and a shell; and MinGW,
a complete toolchain for the development of native Windows applications.
We'll describe here how we built MinGW as a cross-compiler for C and C++.
It is likely that other GCC compilers can be added without problem.
- Download the required elements of MinGW
- Go to the
download page
of MinGW, and download the following files from the Current set:
- src gcc-core
- src gcc-g++
- bin mingw-runtime
- bin w32-api
- src binutils
- Launch Terminal
- The Terminal application is in the Utilities folder. In the Finder,
type Command-Shift-U to open it.
- Make a directory for MinGW
- We'll store the MinGW toolchain in a disk image, which we find
convenient since it makes its deployment on other Mac computers very
easy. But depending on your needs, you may disagree; you'll have to
adjust the directory names accordingly. We create a directory with
the same path as where the image will be mounted:
mkdir /Volumes/MinGW
- Change directory to the download folder
- In the Terminal application, change directory to the folder where
the downloaded elements are stored, typically on the Desktop:
cd ~/Desktop
- Unarchive and build MinGW
- Type the following commands (you can copy them from this page and
paste them to the Terminal window):
# expand archives in temp directory
mkdir /tmp/xgccbuild
mkdir /tmp/xgccbuild/usr
cp binutils-*tar* gcc-*tar* /tmp/xgccbuild
cp mingw-run*tar* w32api-*tar* /tmp/xgccbuild/usr
cd /tmp/xgccbuild
gunzip *.gz
tar xf b*
tar xf gcc-c*
tar xf gcc-g*
rm *.tar
cd usr
gunzip *.gz
tar xf m*
tar xf w*
rm *.tar
cd ..
# copy MinGW headers and libraries to final place
cp -Rf usr/* /Volumes/MinGW
# rename directories for easier access
mv b* binutils
mv gcc-* gcc
# configure and build binutils
mkdir build
cd build
../binutils/configure --target=mingw32 --prefix=/Volumes/MinGW \
--with-gcc --with-gnu-as --with-gnu-ld \
--disable-shared --disable-nls
make CFLAGS="-O2 -D__USE_CRTIMP -fno-exceptions" LDFLAGS=-s
make install
cd ..
rm -Rf build
# add a symbolic link to include in mingw32
ln -s /Volumes/MinGW/include /Volumes/MinGW/mingw32/include
# configure and build gcc
mkdir build
cd build
../gcc/configure --with-gcc --with-gnu-ld --with-gnu-as \
--target=mingw32 --prefix=/Volumes/MinGW \
--enable-threads --disable-nls \
--enable-languages=c,c++ --disable-win32-registry \
--disable-shared --without-x \
--with-sysroot=/tmp/xgccbuild
make
export PATH=$PATH:/Volumes/MinGW/bin
make install
cd ..
rm -Rf build
- Test compiler
- Write a small program such as the one below and save it as test.c
#include <stdio.h>
int main(void)
{
printf("Hello, Windows!\n");
return 0;
}
- Compile it with the following command:
/Volumes/MinGW/bin/mingw32-gcc -o test.exe test.c
- Transfer test.exe to a Windows computer and execute it. Do the same
with a C++ program named test.cpp:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello++, Windows!\n";
return 0;
}
- The C++ compiler is invoked with
/Volumes/MinGW/bin/mingw32-g++ -o test.exe test.cpp
- Make a disk image
- Create a .dmg image from /Volumes/MinGW, remove MinGW directory and mount image:
cd
hdiutil create -srcfolder /Volumes/MinGW MinGW.dmg
rm -Rf /Volumes/MinGW
open MinGW.dmg
|