How to configure gcc to compile for android?
I’ve read in the gcc documentation that it supports building android binaries with the -mandroid and -mbionic switches. First I tried it with the native gcc built from svn. Result:
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
Basically, it failed. Next, I tried to compile the same gcc sources with target=arm-linux-androideabi, (without defining sysroot. ), and it passed the configuration, but failed to build saying pthread.h is not found:
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.
So, does anybody have the instructions for how to use gcc to build android binaries (I don’t want to use code sourcery or android-ndk)? EDIT: my configure options
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
2 Answers 2
I had exactly the same problem as described by dancsi while trying to compile my own cross compilation tool chains for Android (I wanted to build a 32 bit variant of them so I don’t need to buy a 64 bit PC or do my android stuff in a VM).
I am using the tool chain sources from the official AOSP tool chain repo:
repo init -u https://android.googlesource.com/toolchain/manifest
The solution is to use the following 2 extra configure options: —with-headers and —with-libs. I’ve got this solution by reading the following file: [android-toolchain-repo]/build/README:
The other way is to specify the headers and libraries with —with-headers and —with-libs separately. If you configure your tree with a prefix. The headers and libraries will be copied to the install directory specified by the prefix.
For the values of those 2 extra parameters I used [android-src-repo]/prebuilts/ndk/8/platforms/android-14/arch-arm/usr/lib and [android-src-repo]/prebuilts/ndk/8/platforms/android-14/arch-arm/usr/include. [android-src-repo] is referring to my repo checkout of the base AOSP sources (in my case I am on the android-4.2.2_r1 branch of it).
The end-result of the two configure statements I need are the following:
./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
Notice the extra —disable-option-checking argument which was necessary for configure to accept all arguments!
This long list of arguments was constructed by checking the original configure arguments passed for the default 64 bit version of the 4.6 gcc toolchain. This can be checked in the SOURCES files available in [android-src-repo]/prebuilts/gcc/linux-x86/arm/arm-eabi-4.6 and [android-src-repo]/prebuilts/gcc/linux-x86/arm/arm-linux-androideabi-4.6.
In the same files, git tags can be found, so I also checkout those specific version in my [android-toolchain-repo]. For the extra modules needed by gcc, like gmp/mpfr/gdb. I extracted the correct versions of the tar-balls available in [android-toolchain-repo]/gmp|mpfr|gdb. This was the easiest way I could find out, for my original configure statement to be able to pick up those modules.
The same technique as described above also allowed me to compile a 32 bit variant of the latest gcc 4.7 tool chain (currently being used on the MAIN branch of AOSP). The configure options were of course different in this case.
How to compile gcc for android?
Compiling GCC (GNU Compiler Collection) for Android can be a challenging task, especially if you are new to the Android platform and its underlying architecture. GCC is a popular open-source compiler that is widely used to compile many different types of programs, including C, C++, Fortran, and others. However, Android has its own set of limitations and restrictions that can make compiling GCC difficult. In this article, we will explore the steps involved in compiling GCC for Android and provide you with different methods to achieve this goal.
Method 1: Using the Android NDK
To compile GCC for Android, we can use the Android NDK. Here are the steps to do it:
- Download and install the Android NDK from the official website.
- Create a new directory for the GCC source code and download it from the official website.
- Extract the GCC source code and navigate to the directory.
- Create a new directory for the build and navigate to it.
- Run the following command to configure the build:
../configure --prefix=/path/to/installation/directory --target=arm-linux-androideabi --host=x86_64-linux-gnu --enable-languages=c,c++
export PATH=/path/to/installation/directory/bin:$PATH
Now, you can use GCC for Android development!
Here are some explanations for the commands used:
- ../configure : This command configures the build. The —prefix option specifies the installation directory, the —target option specifies the target platform, and the —host option specifies the host platform.
- make : This command builds the software.
- make install : This command installs the software.
- export PATH : This command adds the installation directory to your PATH environment variable.
Note that the above steps are for building GCC for the ARM architecture. If you want to build it for other architectures, you need to change the —target option accordingly.
Also, the above steps only build GCC. If you want to build other libraries or tools, you need to repeat the above steps with the appropriate configuration options.
That’s it! You now know how to compile GCC for Android using the Android NDK.
Method 2: Cross-compiling GCC on a desktop computer
- Download and install the Android NDK on your desktop computer.
- Open a terminal and navigate to the directory where you want to install the GCC toolchain.
- Download the GCC source code from the official website:
wget https://ftp.gnu.org/gnu/gcc/gcc-10.2.0/gcc-10.2.0.tar.gz
../gcc-10.2.0/configure --target=arm-linux-androideabi --prefix=/path/to/installation/directory --with-sysroot=$ANDROID_NDK/sysroot --enable-languages=c,c++ --disable-multilib --disable-bootstrap --with-arch=armv7-a --with-fpu=vfpv3-d16 --with-float=hard
export PATH=/path/to/installation/directory/bin:$PATH
arm-linux-androideabi-gcc --version
That’s it! You have successfully cross-compiled GCC on a desktop computer for Android. You can now use the GCC toolchain to compile C and C++ code for Android.
Method 3: Building GCC from source on an Android device
To compile GCC for Android, you can follow these steps:
- Install Termux from the Google Play Store. This is a terminal emulator and Linux environment for Android.
- Open Termux and type the following command to update the package list:
pkg install git make clang
git clone git://gcc.gnu.org/git/gcc.git
./configure --prefix=$HOME/gcc --disable-multilib --enable-languages=c,c++
export PATH=$HOME/gcc/bin:$PATH
That’s it! You have now built and installed GCC on your Android device.