我在gcc文档中读到它支持使用-mandroid和-mbionic开关构建android二进制文件。首先,我尝试使用从svn构建的本机gcc。结果:
dancsi@dancsi-VirtualBox:~$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/local/libexec/gcc/i686-pc-linux-gnu/4.7.0/lto-wrapper
Target: i686-pc-linux-gnu
Configured with: ../source/configure --enable-threads --disable-nls
Thread model: posix
gcc version 4.7.0 20110611 (experimental) (GCC)
dancsi@dancsi-VirtualBox:~$ g++ test.cpp -mandroid -o test.out
dancsi@dancsi-VirtualBox:~$ file test.out
test.out: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.15, not stripped
基本上,它失败了。接下来,我尝试使用target = arm-linux-androideabi编译相同的gcc源代码(没有定义sysroot,...),并且它传递了配置,但是没有找到说找不到pthread.h:
In file included from ../.././gcc/gthr-default.h:1:0,
from ../../../combined/libgcc/../gcc/gthr.h:160,
from ../../../combined/libgcc/../gcc/unwind-dw2.c:37:
../../../combined/libgcc/../gcc/gthr-posix.h:41:21: fatal error: pthread.h: No such file or directory
compilation terminated.
那么,有没有人有如何使用gcc构建android二进制文件的说明(我不想使用代码源代码或android-ndk)?
编辑: 我的配置选项
configure --target=arm-linux-android --host=i686-pc-linux-gnu --build=i686-pc-linux-gnu --disable-libssp --disable-libgomp --disable-nls --enable-languages=c,c++,java
答案 0 :(得分:3)
在尝试为Android编译自己的交叉编译工具链时,我遇到与 dancsi 所描述的完全相同的问题(我想构建一个 32位变体所以我不需要购买64位PC或在虚拟机中运行我的机器人。)
我正在使用官方AOSP工具链回购中的工具链来源:
repo init -u https://android.googlesource.com/toolchain/manifest
解决方案是使用以下2个额外配置选项: - with-headers 和 - with-libs 。我通过阅读以下文件得到了这个解决方案:[android-toolchain-repo] / build / README:
另一种方法是使用--with-headers和指定头文件和库 - with-libs分开。如果使用前缀配置树。标题 和库将被复制到前缀指定的安装目录。
对于我使用的那两个额外参数的值[android-src-repo] / prebuilts / ndk / 8 / platforms / android-14 / arch-arm / usr / lib和[android-src-repo] / prebuilts / NDK / 8 /平台/机器人-14 /弓形臂的/ usr /包括。 [android-src-repo]是指我的基础AOSP源的repo checkout(在我的情况下,我在它的android-4.2.2_r1分支上)。
我需要的两个配置语句的最终结果如下:
./configure --target=arm-eabi --host=i686-linux-gnu --build=i686-linux-gnu --disable-gold \
--with-gcc-version=4.6 --with-binutils-version=2.21 --with-gmp-version=4.2.4 --with-mpfr-version=2.4.1 \
--with-gdb-version=7.3.x --with-gold-version=2.21 --prefix=/tmp/toolchain --disable-multilib \
--with-libs=/home/boeboe/android-src/prebuilts/ndk/8/platforms/android-14/arch-arm/usr/lib \
--with-headers=/home/boeboe/android-src/prebuilts/ndk/8/platforms/android-14/arch-arm/usr/include \
--program-transform-name='s&^&arm-eabi-&' --with-arch=armv5te --disable-option-checking
./configure --target=arm-linux-androideabi --host=i686-linux-gnu --build=i686-linux-gnu --enable-gold \
--with-gcc-version=4.6 --with-binutils-version=2.21 --with-gmp-version=4.2.4 --with-mpfr-version=2.4.1 \
--with-gdb-version=7.3.x --with-gold-version=2.21 --prefix=/tmp/toolchain \
--with-libs=/home/boeboe/android-src/prebuilts/ndk/8/platforms/android-14/arch-arm/usr/lib \
--with-headers=/home/boeboe/android-src/prebuilts/ndk/8/platforms/android-14/arch-arm/usr/include \
--program-transform-name='s&^&arm-linux-androideabi-&' --disable-option-checking
注意额外的 - disable-option-checking 参数,这是配置接受所有参数所必需的!
这个长参数列表是通过检查为4.6 gcc工具链的默认64位版本传递的原始configure参数构建的。这可以在[android-src-repo] /prebuilts/gcc/linux-x86/arm/arm-eabi-4.6和[android-src-repo] / prebuilts / gcc / linux-x86 /中的SOURCES文件中查看。臂/臂的linux-androideabi-4.6。
在相同的文件中,可以找到git标签,所以我也在我的[android-toolchain-repo]中查看这些特定的版本。对于gcc所需的额外模块,比如gmp / mpfr / gdb ...,我提取了[android-toolchain-repo] / gmp | mpfr | gdb中可用的tar-ball的正确版本。这是我能找到的最简单的方法,因为我的原始配置语句能够获取这些模块。
与上述相同的技术也允许我编译最新gcc 4.7工具链的32位变体(目前在AOSP的MAIN分支上使用)。在这种情况下,配置选项当然是不同的。
答案 1 :(得分:1)
In file included from ../.././gcc/gthr-default.h:1:0,
from /mnt/network/source/gcc/libgcc/../gcc/gthr.h:160,
from /mnt/network/source/gcc/libgcc/../gcc/unwind-dw2.c:37:
/mnt/network/source/gcc/libgcc/../gcc/gthr-posix.h:41:21: fatal error: pthread.h: No such file or directory
compilation terminated.
make[2]: *** [unwind-dw2.o] Error 1
make[1]: *** [all-target-libgcc] Error 2
make: *** [all] Error 2
我收到了与你相同的错误,但我没有为Android编译。我正在尝试构建w64-mingw32编译器。
AR=x86_64-w64-mingw32-ar RANLIB=x86_64-w64-mingw32-ranlib
../gcc-4.5.1/configure --prefix=/tools \
--with-local-prefix=/tools --enable-clocale=gnu \
--enable-shared --enable-threads=posix \
--enable-__cxa_atexit --enable-languages=c,c++ \
--disable-libstdcxx-pch --disable-multilib \
--disable-bootstrap --disable-libgomp \
--without-ppl --without-cloog --build=$MACHTYPE --host=$MACHTYPE --target=x86_64-w64-mingw32
我的回答是让你知道它不仅仅是Android,麻烦更为通用,只是为ARM架构而构建。 我认为这是一个c ++问题,因为我单独构建一个c编译器没有任何问题。