【お知らせ】プログラミング記事の投稿はQiitaに移行しました。

Windows x64用のクロスgcc

Windows x64用のクロスgccをビルドしました。MSYSで作業しましたが、他のUNIX系OSでも同じ手順が使えるはずです。

今回はmultilibを使用せずにx64専用とします。

gccが依存する数値計算ライブラリ gmp, mpfr, mpc をビルドします。

$ curl -LO ftp://ftp.gnu.org/gnu/gmp/gmp-5.0.5.tar.xz
$ tar xvf gmp-5.0.5.tar.xz
$ cd gmp-5.0.5
$ ./configure
$ make
$ make install
$ cd ..
$ curl -LO http://www.mpfr.org/mpfr-current/mpfr-3.1.0.tar.xz
$ tar xvf mpfr-3.1.0.tar.xz
$ cd mpfr-3.1.0
$ CPPFLAGS=-I/usr/local/include LDFLAGS=-L/usr/local/lib ./configure
$ make
$ make install
$ cd ..
$ curl -LO http://www.multiprecision.org/mpc/download/mpc-0.9.tar.gz
$ tar xvf mpc-0.9.tar.gz
$ cd mpc-0.9
$ CPPFLAGS=-I/usr/local/include LDFLAGS=-L/usr/local/lib ./configure
$ make
$ make install
$ cd ..

binutils(アセンブラ・リンカなど)をビルドします。

$ curl -LO ftp://ftp.gnu.org/gnu/binutils/binutils-2.22.tar.bz2
$ tar xvf binutils-2.22.tar.bz2
$ mkdir -p x86_64-pc-mingw32/binutils
$ cd x86_64-pc-mingw32/binutils
$ ../../binutils-2.22/configure --target=x86_64-pc-mingw32 --disable-nls
$ make
$ make install
$ cd ../..

gccをコンパイラだけビルドします。gcc付属ライブラリはランタイムがないとビルドできませんが、ランタイムはコンパイラがないとビルドできないためです。

$ curl -LO ftp://ftp.gnu.org/gnu/gcc/gcc-4.7.0/gcc-4.7.0.tar.bz2
$ tar xvf gcc-4.7.0.tar.bz2
$ mkdir -p x86_64-pc-mingw32/gcc
$ cd x86_64-pc-mingw32/gcc
$ CPPFLAGS=-I/usr/local/include LDFLAGS=-L/usr/local/lib ../../gcc-4.7.0/configure --target=x86_64-pc-mingw32 --disable-nls
$ CPPFLAGS=-I/usr/local/include LDFLAGS=-L/usr/local/lib make all-gcc
$ make install-gcc
$ cd ../..

ランタイムのソースをダウンロードして展開します。

$ curl -LO http://sourceforge.net/projects/mingw-w64/files/mingw-w64/mingw-w64-release/mingw-w64-v2.0.3.tar.gz
$ tar xvf mingw-w64-v2.0.3.tar.gz

ヘッダをビルドします。ランタイムをビルドする前にヘッダをインストールしないとエラーになるためです。

※ configureのオプションは--targetではなく--hostです。

$ mkdir -p x86_64-pc-mingw32/headers
$ cd x86_64-pc-mingw32/headers
$ ../../mingw-w64-v2.0.3/mingw-w64-headers/configure --host=x86_64-pc-mingw32
$ make
$ make install
$ cd ..

ランタイムをビルドします。

※ configureのオプションは--targetではなく--hostです。

$ mkdir runtime
$ cd runtime
$ ../../mingw-w64-v2.0.3/configure --host=x86_64-pc-mingw32 --without-headers
$ make
$ make install
$ cd ..

gccのビルドに戻ります。コンパイラはビルド済みのため、付属ライブラリがビルドされます。

$ cd gcc
$ make
$ make install
$ cd ../..

以上でビルドとインストールは完了です。

テスト

$ x86_64-pc-mingw32-gcc hello.c
$ file a.exe
a.exe: PE32+ executable for MS Windows (console) Mono/.Net assembly
$ ./a.exe
hello