Skip to content

Instantly share code, notes, and snippets.

@jeroen
Last active March 15, 2023 12:04
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save jeroen/b3876b065512299d80f1 to your computer and use it in GitHub Desktop.
Save jeroen/b3876b065512299d80f1 to your computer and use it in GitHub Desktop.
mingw-w64 cross compile
# We use Ubuntu 14.04 to build a native gcc for win32 with multilib support
#
# Based on:
# http://sourceforge.net/p/mingw-w64/wiki2/Native%20Win64%20compiler/
# http://sourceforge.net/p/mingw-w64/code/HEAD/tree/stable/v3.x/mingw-w64-doc/howto-build/mingw-w64-howto-build.txt?format=raw
#
# Cross compiling notes:
# - The minor version of gcc must match that of our cross compiler (4.8 in this case)
# - Important parameters: http://gcc.gnu.org/onlinedocs/gccint/Configure-Terms.html
#
# Use ubuntu cross compiler
sudo apt-get install make gcc-mingw-w64-x86-64 gcc-mingw-w64-i686 mingw-w64
# Setup dir structure
BUILDROOT=~/mingw
SRC=$BUILDROOT/sources
DEST=$BUILDROOT/dest
mkdir -p $SRC
mkdir -p $DEST
# Get sources
cd $SRC
wget http://ftp.gnu.org/gnu/binutils/binutils-2.24.tar.bz2
tar xjf binutils-2.24.tar.bz2
wget http://downloads.sourceforge.net/project/mingw-w64/mingw-w64/mingw-w64-release/mingw-w64-v3.1.0.tar.bz2
tar xjf mingw-w64-v3.1.0.tar.bz2
wget http://ftp.gnu.org/gnu/gcc/gcc-4.8.3/gcc-4.8.3.tar.bz2
tar xjf gcc-4.8.3.tar.bz2
# Get gcc dependencies
cd gcc-4.8.3
./contrib/download_prerequisites
# Make binutils
mkdir -p $BUILDROOT/binutils
cd $BUILDROOT/binutils
$SRC/binutils-2.24/configure --prefix=$DEST --with-sysroot=$DEST --target=i686-w64-mingw32 --enable-targets=i686-w64-mingw32,x86_64-w64-mingw32 #--enable-64-bit-bfd
make
make install
# Add path (required for building gcc later)
export PATH="$PATH:$DEST/bin"
# Build mingw headers
# Assumes we are building with x86_64-w64-mingw32 cross compiler!
mkdir -p $BUILDROOT/headers
cd $BUILDROOT/headers
$SRC/mingw-w64-v3.1.0/mingw-w64-headers/configure --prefix=$DEST/i686-w64-mingw32 --host=i686-w64-mingw32 --build=x86_64-w64-mingw32 #--build=i686-w64-mingw32
make
make install
# Symlink for gcc
ln -s $DEST/i686-w64-mingw32 $DEST/mingw
# Multilib symlink. Not sure if this is necessary for i686.
# ln -s $DEST/i686-w64-mingw32/lib $DEST/i686-w64-mingw32/lib64
# Building GCC
# Not sure about --disable-shared
mkdir -p $BUILDROOT/gcc
cd $BUILDROOT/gcc
$SRC/gcc-4.8.3/configure --target=i686-w64-mingw32 --prefix=$DEST --with-sysroot=$DEST --enable-targets=all --disable-shared --enable-languages=c,c++,fortran
make all-gcc
make install-gcc
# Building CRT (Mingw-w64 itself)
mkdir -p $BUILDROOT/crt
cd $BUILDROOT/crt
$SRC/mingw-w64-v3.1.0/configure --host=i686-w64-mingw32 --prefix=$DEST/i686-w64-mingw32 --with-sysroot=$DEST --enable-lib32 --enable-lib64
make
make install
# Finishing gcc
cd $BUILDROOT/gcc
make
make install
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment