Compiling C console applications for Android (on Mac OS X)

I wanted to build some C applications on my Mac, to run directly on the console in Android. It’s possible to do this using the ARM toolchain, but it can be a bit hard to work out how to do it. Having got it working, I’ve written down a simple process that should allow you to get up and running very quickly. (Thanks to Radu Motisan for his 2010 article targeting Windows. It’s out of date now, and for a different OS, but it gave me most of the clues I needed to get it working.)

1. Install theĀ Android SDK for Mac OS X. (I put mine in ~/android-sdk.)

2. Install the Android NDK for Mac OS X. (I put mine in ~/android-ndk-r8d.)

3. Set up your $PATH. In ~/.bash_profile, add these lines:

NDK_ROOT=~/android-ndk-r8d
SDK_ROOT=~/android-sdk
export PATH=$PATH:$SDK_ROOT/sdk/platform-tools
export PATH=$PATH:$NDK_ROOT
export PATH=$PATH:$NDK_ROOT/prebuilt/darwin-x86/bin
export PATH=$PATH:$NDK_ROOT/build/tools

4. Create a new executable script called ndk-comp. Put it in $NDK_ROOT.

#!/bin/sh

ANDROIDSDK='android-14'

PROGDIR=`dirname $0`
PROGDIR=`cd $PROGDIR && pwd`

ARMEABIGCC=$PROGDIR/toolchains/arm-linux-androideabi-4.7/prebuilt/darwin-x86/bin/arm-linux-androideabi-gcc
ARMEABILIB=$PROGDIR/platforms/$ANDROIDSDK/arch-arm/usr/lib
ARMEABIINC=$PROGDIR/platforms/$ANDROIDSDK/arch-arm/usr/include
ARMEABICRT=$PROGDIR/platforms/$ANDROIDSDK/arch-arm/usr/lib/crtbegin_dynamic.o

LINKER=/system/bin/linker

echo "GCC:"$ARMEABIGCC "LIB:"$ARMEABILIB "LINKER":$LINKER "PARAMS:"$@

$ARMEABIGCC $@ -Wl,-rpath-link=$ARMEABILIB,-dynamic-linker=$LINKER -L$ARMEABILIB $ARMEABICRT -I$ARMEABIINC -nostdlib -lc

That’s it!

Now to test it. In a working directory, create a simple C program test.c :

#include <stdio.h>

int main()
{
printf("Hello world!\n");
return 0;
}

Run this command to build it:

ndk-comp -o test test.c

Now connect your Android device, and push the executable to a suitable location (I’m using /data/local/tmp here):

adb push test /data/local/tmp

Now run it:

$ adb shell
# cd /data/local/tmp
# ./test

And you should see:

Hello world!
[1] + Stopped (signal) ./test
This entry was posted in Android, Mac and tagged , , . Bookmark the permalink.

3 Responses to Compiling C console applications for Android (on Mac OS X)

  1. Derzu says:

    Nice post. Just miss to add the flag “-lgcc” to the compiler line.

  2. Seon says:

    I can’t see “Hello world!”…
    I can see just “[1] + Stopped (signal) ./test”.
    What should I do??

  3. Seon says:

    And I tried in rooted device.. what is different?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s