title: How to build a cross-compiler for i386-apple-darwin target from x86_64-apple-darwin host?
body: <p><strong>Problem overview</strong></p>
  
  <p>I am trying to learn how to build a cross-compiler, specifically a version of GNU <code>gcc</code> 4.7.2 on a 64-bit Intel OS X host that targets <code>i386-apple-darwin</code> (32-bit Intel). However, I keep getting stuck on the same error:</p>
  
  <pre><code>...
  checking for i386-apple-darwin-gcc... /Users/alexpreynolds/Developer/third-party/build-gcc/./gcc/xgcc -B/Users/alexpreynolds/Developer/third-party/build-gcc/./gcc/ -B/Users/alexpreynolds/Developer/third-party/i386-apple-gcc/i386-apple-darwin/bin/ -B/Users/alexpreynolds/Developer/third-party/i386-apple-gcc/i386-apple-darwin/lib/ -isystem /Users/alexpreynolds/Developer/third-party/i386-apple-gcc/i386-apple-darwin/include -isystem /Users/alexpreynolds/Developer/third-party/i386-apple-gcc/i386-apple-darwin/sys-include
  checking for suffix of object files... configure: error: in `/Users/alexpreynolds/Developer/third-party/build-gcc/i386-apple-darwin/libgcc':
  configure: error: cannot compute suffix of object files: cannot compile
  See `config.log' for more details.
  make[1]: *** [configure-target-libgcc] Error 1
  make: *** [all] Error 2
  </code></pre>
  
  <p><strong>Build process</strong></p>
  
  <p>I first build a version of GNU <code>binutils</code> which targets <code>i386-apple-darwin</code>, which is accessible to the GNU <code>gcc</code> build folder:</p>
  
  <pre><code>$ cd third-party
  $ wget http://ftp.gnu.org/gnu/binutils/binutils-2.23.2.tar.bz2
  $ tar xjvf binutils-2.23.2.tar.bz2
  $ mkdir i386-apple-darwin
  $ mkdir build-binutils &amp;&amp; cd build-binutils
  $ ../binutils-2.23.2/configure --target=i386-apple-darwin --prefix="/Users/alexpreynolds/Developer/third-party/i386-apple-gcc"
  $ make all &amp;&amp; make install
  ...
  </code></pre>
  
  <p>Then I try to build a targeted version of <code>gcc</code> as follows:</p>
  
  <pre><code>$ cd ..
  $ pwd
  /Users/alexpreynolds/Developer/third-party
  $ wget http://ftp.gnu.org/pub/gnu/gcc/gcc-4.7.2/gcc-4.7.2.tar.bz2
  $ tar xvjf gcc-4.7.2.tar.bz2
  $ cd gcc-4.7.2
  $ ./contrib/download_prerequisites
  ... /* this grabs GMP, MPC and MPFR dependencies */
  $ cd ..
  $ mkdir build-gcc &amp;&amp; cd build-gcc
  $ ../gcc-4.7.2/configure --target=i386-apple-darwin --prefix="/Users/alexpreynolds/Developer/Bedops/bedops/third-party/i386-apple-gcc"
  $ make
  ...
  </code></pre>
  
  <p>After about 30 minutes of compiling stuff, it gets stuck with the error message shown above.</p>
  
  <p><strong>Error details</strong></p>
  
  <p>To track down the source of the error, here is a snippet of <code>build-gcc/i386-apple-darwin/libgcc/config.log</code>:</p>
  
  <pre><code>configure:3344: /Users/alexpreynolds/Developer/third-party/build-gcc/./gcc/xgcc -B/Users/alexpreynolds/Developer/third-party/build-gcc/./gcc/ -B/Users/alexpreynolds/Developer/third-party/i386-apple-gcc/i386-apple-darwin/bin/ -B/Users/alexpreynolds/Developer/third-party/i386-apple-gcc/i386-apple-darwin/lib/ -isystem /Users/alexpreynolds/Developer/third-party/i386-apple-gcc/i386-apple-darwin/include -isystem /Users/alexpreynolds/Developer/third-party/i386-apple-gcc/i386-apple-darwin/sys-include    -V &gt;&amp;5
  xgcc: error: unrecognized command line option '-V'
  xgcc: fatal error: no input files
  compilation terminated.
  configure:3355: $? = 1
  configure:3344: /Users/alexpreynolds/Developer/third-party/build-gcc/./gcc/xgcc -B/Users/alexpreynolds/Developer/third-party/build-gcc/./gcc/ -B/Users/alexpreynolds/Developer/third-party/i386-apple-gcc/i386-apple-darwin/bin/ -B/Users/alexpreynolds/Developer/third-party/i386-apple-gcc/i386-apple-darwin/lib/ -isystem /Users/alexpreynolds/Developer/third-party/i386-apple-gcc/i386-apple-darwin/include -isystem /Users/alexpreynolds/Developer/third-party/i386-apple-gcc/i386-apple-darwin/sys-include    -qversion &gt;&amp;5
  xgcc: error: unrecognized command line option '-qversion'
  xgcc: fatal error: no input files
  compilation terminated.
  configure:3355: $? = 1
  configure:3371: /Users/alexpreynolds/Developer/third-party/build-gcc/./gcc/xgcc -B/Users/alexpreynolds/Developer/third-party/build-gcc/./gcc/ -B/Users/alexpreynolds/Developer/third-party/i386-apple-gcc/i386-apple-darwin/bin/ -B/Users/alexpreynolds/Developer/third-party/i386-apple-gcc/i386-apple-darwin/lib/ -isystem /Users/alexpreynolds/Developer/third-party/i386-apple-gcc/i386-apple-darwin/include -isystem /Users/alexpreynolds/Developer/third-party/i386-apple-gcc/i386-apple-darwin/sys-include    -o conftest -g -O2   conftest.c  &gt;&amp;5
  Assembler messages:
  Fatal error: invalid listing option `r'
  configure:3374: $? = 1
  configure:3562: checking for suffix of object files
  configure:3584: /Users/alexpreynolds/Developer/third-party/build-gcc/./gcc/xgcc -B/Users/alexpreynolds/Developer/third-party/build-gcc/./gcc/ -B/Users/alexpreynolds/Developer/third-party/i386-apple-gcc/i386-apple-darwin/bin/ -B/Users/alexpreynolds/Developer/third-party/i386-apple-gcc/i386-apple-darwin/lib/ -isystem /Users/alexpreynolds/Developer/third-party/i386-apple-gcc/i386-apple-darwin/include -isystem /Users/alexpreynolds/Developer/third-party/i386-apple-gcc/i386-apple-darwin/sys-include    -c -g -O2  conftest.c &gt;&amp;5
  Assembler messages:
  Fatal error: invalid listing option `r'
  configure:3588: $? = 1
  configure: failed program was:
  | /* confdefs.h */
  | #define PACKAGE_NAME "GNU C Runtime Library"
  | #define PACKAGE_TARNAME "libgcc"
  | #define PACKAGE_VERSION "1.0"
  | #define PACKAGE_STRING "GNU C Runtime Library 1.0"
  | #define PACKAGE_BUGREPORT ""
  | #define PACKAGE_URL "http://www.gnu.org/software/libgcc/"
  | /* end confdefs.h.  */
  |
  | int
  | main ()
  | {
  |
  |   ;
  |   return 0;
  | }
  configure:3602: error: in `/Users/alexpreynolds/Developer/third-party/build-gcc/i386-apple-darwin/libgcc':
  configure:3605: error: cannot compute suffix of object files: cannot compile
  See `config.log' for more details.
  </code></pre>
  
  <p><strong>The compiler I'm bootstrapping from</strong></p>
  
  <pre><code>$ gcc -v
  Using built-in specs.
  COLLECT_GCC=gcc
  COLLECT_LTO_WRAPPER=/opt/local/libexec/gcc/x86_64-apple-darwin12/4.7.2/lto-wrapper
  Target: x86_64-apple-darwin12
  Configured with: ../gcc-4.7.2/configure --prefix=/opt/local --build=x86_64-apple-darwin12 --enable-languages=c,c++,objc,obj-c++,lto,fortran,java --libdir=/opt/local/lib/gcc47 --includedir=/opt/local/include/gcc47 --infodir=/opt/local/share/info --mandir=/opt/local/share/man --datarootdir=/opt/local/share/gcc-4.7 --with-libiconv-prefix=/opt/local --with-local-prefix=/opt/local --with-system-zlib --disable-nls --program-suffix=-mp-4.7 --with-gxx-include-dir=/opt/local/include/gcc47/c++/ --with-gmp=/opt/local --with-mpfr=/opt/local --with-mpc=/opt/local --with-ppl=/opt/local --with-cloog=/opt/local --enable-cloog-backend=isl --disable-cloog-version-check --enable-stage1-checking --enable-lto --enable-libstdcxx-time --with-as=/opt/local/bin/as --with-ld=/opt/local/bin/ld --with-ar=/opt/local/bin/ar --with-bugurl=https://trac.macports.org/newticket --disable-ppl-version-check --with-pkgversion='MacPorts gcc47 4.7.2_2+universal'
  Thread model: posix
  gcc version 4.7.2 (MacPorts gcc47 4.7.2_2+universal) 
  </code></pre>
  
  <p><strong>Question</strong></p>
  
  <p>Is there a step I missed in setting up the build environment of <code>coreutils</code> or <code>gcc</code> such that this error results? </p>
  
  <p>What else can I troubleshoot in trying to resolve this error? My Google searches on this error message are not bearing much fruit.</p>
  
  <hr>
  
  <p><strong>Changes</strong></p>
  
  <p>I changed the build script as follows:</p>
  
  <pre><code>#!/bin/bash -x
  
  WORK_DIR=$PWD
  INSTALL_DIR=$WORK_DIR/gcc-4.7.2-darwin
  MIN_OSX_VERSION=10.5
  
  if [ -d "$INSTALL_DIR" ]; then
      echo "Directory $INSTALL_DIR already exists";
      exit 1
  fi
  
  BINUTILS=binutils-2.23.2
  TARBALL_BINUTILS=$BINUTILS.tar.bz2
  URL_BINUTILS=http://ftp.gnu.org/gnu/binutils/$TARBALL_BINUTILS
  SOURCE_BINUTILS_DIR=$WORK_DIR/$BINUTILS
  BUILD_BINUTILS_DIR=$WORK_DIR/build-$BINUTILS
  
  GCC=gcc-4.7.2
  TARBALL_GCC=$GCC.tar.bz2
  URL_GCC=http://ftp.gnu.org/pub/gnu/gcc/$GCC/$TARBALL_GCC
  SOURCE_GCC_DIR=$WORK_DIR/$GCC
  BUILD_GCC_DIR=$WORK_DIR/build-$GCC
  
  curl -O $URL_BINUTILS
  mkdir -p $SOURCE_BINUTILS_DIR
  tar -xjvf $TARBALL_BINUTILS
  rm $TARBALL_BINUTILS
  mkdir -p $BUILD_BINUTILS_DIR
  cd $BUILD_BINUTILS_DIR
  $SOURCE_BINUTILS_DIR/configure --prefix=$INSTALL_DIR
  make -j $(sysctl -n hw.availcpu) all CFLAGS='-Wno-error' &amp;&amp; make install
  
  find $INSTALL_DIR -name '*strip*' -delete
  
  cd $WORK_DIR
  
  curl -O $URL_GCC
  mkdir -p $SOURCE_GCC_DIR
  tar -xvjf $TARBALL_GCC
  rm $TARBALL_GCC
  cd $SOURCE_GCC_DIR
  sed -i '' -e 's/wget/curl -O/g' $SOURCE_GCC_DIR/contrib/download_prerequisites
  $SOURCE_GCC_DIR/contrib/download_prerequisites
  cd $WORK_DIR
  mkdir -p $BUILD_GCC_DIR
  cd $BUILD_GCC_DIR
  $SOURCE_GCC_DIR/configure --prefix=$INSTALL_DIR --enable-languages=c,c++
  make -j $(sysctl -n hw.availcpu) CFLAGS="-mmacosx-version-min=$MIN_OSX_VERSION" &amp;&amp; make install
  
  find $INSTALL_DIR -name '*.a' -print0 | xargs -0 -n1 ranlib
  
  rm -Rf $BUILD_GCC_DIR
  rm -Rf $BUILD_BINUTILS_DIR
  rm -Rf $SOURCE_GCC_DIR
  rm -Rf $SOURCE_BINUTILS_DIR
  </code></pre>
  
  <p>The <code>ranlib</code> step was necessary due to numerous errors during the process of building a library.</p>
  
  <p>To build an <code>i386</code> binary, I had to do the following with my <code>Makefile</code>:</p>
  
  <pre><code>ARCH                 = i386
  ...
  GCCDIR               = ${PARTY3}/gcc-4.7.2-darwin
  GCCBINDIR            = ${GCCDIR}/bin
  GCCLIBDIR            = ${GCCDIR}/lib
  GCCINCDIR            = ${GCCDIR}/include
  ...
  LIBCPP               = libgcc_eh.a
  LIBCXX               = libstdc++.a
  ...
  INCLUDES             = -I${GCCINCDIR} ...
  ...
  CC                   = ${GCCBINDIR}/gcc
  CXX                  = ${GCCBINDIR}/g++
  ...
  GCCCPPLIBPATH        = ${GCCLIBDIR}/gcc/x86_64-apple-darwin12.3.0/4.7.2/${ARCH}/${LIBCPP}
  GCCCXXLIBPATH        = ${GCCLIBDIR}/${ARCH}/${LIBCXX}
  ...
  FLAGS                = ${SFLAGS} ${NONSTATICFLAGS} ${OBJS} -L${GCCLIBDIR} ...
  ...
  run: dependencies
      mkdir -p ${BIN} &amp;&amp; ${CXX} -o ${BIN}/${PROG}_${ARCH} ${FLAGS} -static-libgcc -mmacosx-version-min=${MIN_OSX_VERSION} -arch ${ARCH} ... ${GCCCPPLIBPATH} ${GCCCXXLIBPATH} ${SOURCE1}
  </code></pre>
  
  <p>Summary of <code>Makefile</code> changes:</p>
  
  <ol>
  <li><p>I had to add the <code>-static-libgcc</code> flag and leave out <code>-static-libstdc++</code>, which had no effect (except to cause the build to fail, until I ran <code>ranlib</code> on the static libraries, which fixed the build failure but had no effect on <code>-static-libstdc++</code>).</p></li>
  <li><p>I added <code>${GCCCPPLIBPATH}</code> and <code>${GCCCXXLIBPATH}</code> to the <code>run</code> target, which each point to the static C anc C++ libraries <code>libgcc_eh.a</code> and <code>libstdc++.a</code>, respectively.</p></li>
  </ol>
  
  <p>This allowed me to compile a working <code>i386</code> (32-bit) binary, but the same GCC installation failed to build a working <code>x86_64</code> (64-bit) binary.</p>