export CROSS_HOST="$(echo $MACHTYPE)"
export CROSS_TARGET="arm-jkphone-linux-gnueabi"
export CROSS_ROOT="$(pwd)/cross-tools"
我把手机的sd卡分成两个区,第一个是fat32(android似乎不支持把sdcard全部分成ext3……),第二个是ext3,手动在fat32区的根目录下建立tools目录并把ext3区挂上去。
在host机器上,为了避免工具链的各种路径问题,也在根目录下建立了/sdcard/tools
源代码版本:
binutils-2.21.tar.bz2
gcc-4.6.1.tar.bz2
gmp-5.0.2.tar.bz2
mpfr-2.4.2.tar.bz2
mpc-0.8.2.tar.gz
glibc-2.13.tar.xz
glibc-ports-2.13.tar.bz2
linux-2.6.32.21.tar.bz2
重要问题:
gcc pass1完成后,由于--disable-shared,不会安装libgcc_eh,后面的glibc就会编译失败,因此需要进行如下操作:
ln -sv libgcc.a $($CROSS_TARGET-gcc -print-libgcc-file-name | sed "s@libgcc@&_eh@"
SPECS=`dirname $($CROSS_TARGET-gcc -print-libgcc-file-name)`/specs $CROSS_TARGET-gcc -dumpspecs | sed -e 's@/lib\(64\)\?/ld@/sdcard/tools&@g' -e "/^\*cpp:$/{n;s,$, -isystem /sdcard/tools/include,}" > $SPECS
jiakai:/tmp$ echo 'int main() {}' > dummy.c jiakai:/tmp$ arm-jkphone-linux-gnueabi-gcc dummy.c jiakai:/tmp$ readelf -l a.out | grep interpreter [Requesting program interpreter: /sdcard/tools/lib/ld-linux.so.3]注意readelf的输出,解释器必须是/sdcard/tools/lib/ld-linux.so.3
下面帖configure的参数吧:
cross toolchain:
binutils:
../binutils-2.21/configure --prefix=$CROSS_ROOT --host=$CROSS_HOST --target=$CROSS_TARGET --with-lib-path=/sdcard/tools/lib --disable-nls --disable-werror --with-sysroot=/sdcard/tools
gcc pass1:../gcc-4.6.1/configure --prefix=$CROSS_ROOT --host=$CROSS_HOST --target=$CROSS_TARGET --disable-multilib --with-local-prefix=/sdcard/tools --without-headers --disable-nls --disable-shared --disable-threads --enable-languages=c --with-sysroot=/sdcard/tools --disable-decimal-float --disable-libmudflap --disable-libssp --disable-libgomp --with-build-sysroot=/ --disable-libquadmath
glibc:BUILD_CC=gcc CC=$CROSS_TARGET-gcc AR=$CROSS_TARGET-ar RANLIB=$CROSS_TARGET-ranlib ../glibc-2.13/configure --prefix=/sdcard/tools --host=$CROSS_TARGET --build=$CROSS_HOST --disable-profile --enable-add-ons --with-tls --enable-kernel=2.6.32 --with-binutils=$CROSS_ROOT/bin --with-headers=/sdcard/tools/include libc_cv_forced_unwind=yes libc_cv_c_cleanup=yes
gcc pass2:../gcc-4.6.1/configure --prefix=$CROSS_ROOT --host=$CROSS_HOST --target=$CROSS_TARGET --with-sysroot=/sdcard/tools --with-headers=/sdcard/tools/include --with-libs=/sdcard/tools/lib --disable-nls --enable-languages=c,c++ --enable-c99 --enable-long-long --enable-threads=posix --enable-__cxa_atexit --disable-libstdcxx-pch --disable-multilib
交叉编译工具链完成后,设置以下环境变量:
export CC="${CROSS_TARGET}-gcc" export CXX="${CROSS_TARGET}-g++" export AR="${CROSS_TARGET}-ar" export AS="${CROSS_TARGET}-as" export RANLIB="${CROSS_TARGET}-ranlib" export LD="${CROSS_TARGET}-ld" export STRIP="${CROSS_TARGET}-strip"
target toolchain:
binutils:
../binutils-2.21/configure --prefix=/sdcard/tools --build=$CROSS_HOST --host=$CROSS_TARGET --target=$CROSS_TARGET --enable-sharedgcc:
../gcc-4.6.1/configure --prefix=/sdcard/tools --build=$CROSS_HOST --host=$CROSS_TARGET --target=$CROSS_TARGET --with-local-prefix=/sdcard/tools --enable-long-long --enable-c99 --enable-shared --enable-threads=posix --enable-__cxa_atexit --enable-languages=c,c++
当然在目标工具链上也要类似地调整gcc的specs。
然后终于在android上编译C++版hello world成功了!
参考资料:
clfs book, lfs book (http://www.linuxfromscratch.org/)
http://hi.baidu.com/aflyhorse1990/blog/item/e1ad0a502aaf94758435247e.html