How to make the Android NDK compile Objective-C! Many thanks to Jackie Gleason (@LifeIsTooShort) for his M3 Conference (@M3Conf) presentation, Adding Objective-C Support to the Android NDK, upon which most of this tutorial is based.
DOWNLOAD AND PATCH THE ANDROID NDK TOOLCHAIN SOURCES
Using the terminal, create a directory for your toolchain sources:
mkdir -p ~/SDKs/Android/toolchain-src
cd ~/SDKs/Android/toolchain-src
IMPORTANT: The build-gcc script expects a specific layout for the source directory. Create the above directory anywhere you like, but be sure to create the subdirectories below exactly as described.
Next, fetch the sources:
git clone https://android.googlesource.com/toolchain/build.git
git clone https://android.googlesource.com/toolchain/gmp.git
git clone https://android.googlesource.com/toolchain/gdb.git
git clone https://android.googlesource.com/toolchain/mpc.git
git clone https://android.googlesource.com/toolchain/mpfr.git
mkdir binutils
cd binutils/
curl -O ftp://sourceware.org//pub/binutils/snapshots/binutils-2.22.51.tar.bz2
tar xjfv binutils-2.22.51.tar.bz2
cd ../
mkdir gcc
cd gcc/
curl -O ftp://ftp.gnu.org//gnu/gcc/gcc-4.6.1/gcc-4.6.1.tar.bz2
tar xjfv gcc-4.6.1.tar.bz2
cd ../
Patch the toolchain makefile:
git clone git://gist.github.com/1378127.git build-patch
cd build/
patch < ../build-patch/Makefile.in.diff
cd ../
Patch GCC:
cd gcc/
curl http://gcc.gnu.org/bugzilla/attachment.cgi?id=24879 > gcc-4.6.1.patch
cd gcc-4.6.1/
patch -p1 < ../gcc-4.6.1.patch
DOWNLOAD AND PATCH THE ANDROID NDK
Using the terminal, fetch the NDK:
cd ~/SDKs/Android/
curl -O http://dl.google.com/android/ndk/android-ndk-r6-darwin-x86.tar.bz2
tar xjfv android-ndk-r6-darwin-x86.tar.bz2
Next, patch the build-gcc script:
git clone git://gist.github.com/1378122.git android-ndk-build-gcc-patch
cd android-ndk-r6/build/tools/
patch < ../../../android-ndk-build-gcc-patch/build-gcc.sh.diff
BUILD THE ANDROID NDK TOOLCHAIN
Using the terminal, get to the NDK build/tools directory:
cd ~/SDKs/Android/android-ndk-r6/build/tools/
Next, rebuild GCC on Mac OS X:
./build-gcc.sh --gmp-version=4.2.4 --mpfr-version=2.4.1 --binutils-version=2.22.51 --try-64 ~/SDKs/Android/toolchain-src ~/SDKs/Android/android-ndk-r6 arm-linux-androideabi-4.6.1
Or rebuild GCC on Linux:
./build-gcc.sh --gmp-version=4.2.4 --mpfr-version=2.4.1 --mpc-version=0.8.1 --binutils-version=2.22.51 --try-64 ~/SDKs/Android/toolchain-src ~/SDKs/Android/android-ndk-r6 arm-linux-androideabi-4.6.1
ACKNOWLEDGEMENTS
Adding Objective-C Support to the Android NDK. Gleason, Jackie. M3 Conference. 19 Nov. 2011.
Building an Android NDK with Recent GCC and Binutils. Hommey, Mike. Glandium. 1 Aug. 2011.
Compiling GCC 4.6.1 on Mac OSX Lion. Solarian Programmer. 20 Sep. 2011.
Android NDK GCC 4.2.1 with Objective C 2.0 Support. phausler@gmail.com. 19 Jan. 2010.

5 Comments
Great tutorial! Thanks!!
I had one question: When compiling c++ code that includes std-headers like , or the compiler (I built for OS X) says it can’t find those headers. (No such file or directory) What is missing for it to be able to find those?
Havard,
You’re welcome! Do you have Xcode installed on your Mac? And what syntax are you using for the includes? Your examples were stripped by WordPress, probably because they resemble HTML tags.
Hi, Following your proocess…i executed everything…but in the end I am getting these logs. Could you please look at it and tell me the error. “ERROR: Unknown option ‘–mpc-version’.”
[root@localhost tools]# ./build-gcc.sh –gmp-version=4.2.4 –mpfr-version=2.4.1 –mpc-version=0.8.1 –binutils-version=2.22.51 –try-64 ~/SDKs/Android/toolchain-src ~/SDKs/Android/android-ndk-r6 arm-linux-androideabi-4.6.1 expr: warning: unportable BRE:
^\\([^\\-].*\\)$': using^’ as the first character of the basic regular expression is not portable; it is being ignored expr: warning: unportable BRE:^\\(--[^=]*\\)=.*$': using^’ as the first character of the basic regular expression is not portable; it is being ignored expr: warning: unportable BRE:^--[^=]*=\\(.*\\)$': using^’ as the first character of the basic regular expression is not portable; it is being ignored expr: warning: unportable BRE:^\\([^\\-].*\\)$': using^’ as the first character of the basic regular expression is not portable; it is being ignored expr: warning: unportable BRE:^\\(--[^=]*\\)=.*$': using^’ as the first character of the basic regular expression is not portable; it is being ignored expr: warning: unportable BRE:^--[^=]*=\\(.*\\)$': using^’ as the first character of the basic regular expression is not portable; it is being ignored expr: warning: unportable BRE:^\\([^\\-].*\\)$': using^’ as the first character of the basic regular expression is not portable; it is being ignored expr: warning: unportable BRE:^\\(--[^=]*\\)=.*$': using^’ as the first character of the basic regular expression is not portable; it is being ignored expr: warning: unportable BRE:^--[^=]*=\\(.*\\)$': using^’ as the first character of the basic regular expression is not portable; it is being ignored ERROR: Unknown option ‘–mpc-version’. See –help for usage.I figured out the error, it was not having an option registered in the build.sh file for MPC…
but after that , the error which I received was this :
configure:4138: error: in `/tmp/ndk-root/build/toolchain/libbfd-binutils-2.22.51′: configure:4142: error: C compiler cannot create executables
Kindly reply to this Michael !! your help would be much appreciated
Ankur, please pardon my very slow reply. What operating system & version are you using? What architecture (ARM or Intel? 32 or 64-bit)?