5

Possible Duplicate:
How to compile and run a C/C++ program on the Android system

I recently got a project to compile a c program to be used on Android. I already had an makefile for it, but the problem was how to simply compile it to be used on Android, using a linux emulator on my Windows PC. I've searched the net for a good tutorial and couldn't find a simple one.

I hope it'll help someone

Community
  • 1
  • 1
Dekel
  • 192
  • 4
  • 12
  • How to compile and run a C/C++ program on the Android system http://stackoverflow.com/questions/8698798/how-to-compile-and-run-a-c-c-program-on-the-android-system – b3h3m0th Sep 27 '12 at 12:15
  • You have chosen a hard way. Note that `ndk-build` can create an executable in one simple step. See **test-libstdc++** NDK sample. – Alex Cohn Sep 27 '12 at 12:41
  • Hard? not for me.. That's the way i'm used to for compiling c programs. This way u don't have to learn how to use any new library or environment. Notice that i had to use an existing makefile for the project – Dekel Sep 27 '12 at 12:59

1 Answers1

9

That's the first time I write a tutorial or a guide here, and English isn't my native language, so i'm sorry about the quality of this tutorial ahead :)

Goal: To compile a c program, using gcc, for being used in an Android app.

The things you need:

  1. Linux or linux emulator (such as VMWare).

  2. Latest NDK from google: http://developer.android.com/tools/sdk/ndk/index.html#Downloads

All the next instructions should be done by using some Linux shell. I've used "Terminal".

  1. create a shortcut for your NDK download dir, something like: "NDK=$HOME/android-ndk-r8b"

  2. declare SYSROOT- which is a root directory gcc is using for compilation. something like: "SYSROOT=$NDK/platforms/android-8/arch-arm".

  3. Link SYSROOT and the binaries needed for compilation, by typing something like: "cd $NDK/build/tools" "sh make-standalone-toolchain.sh --arch=arm --ndk-dir=$HOME/android-ndk-r8b --install-dir=$HOME/android-toolchain --platform=android-8"

  4. Expand PATH, which will be used for invoking gcc. something like: "export PATH=$HOME/android-toolchain/bin:$PATH"

  5. Create a shortcut for the Android gcc, by something like: export CC=arm-linux-androideabi-gcc

I've used android-8 as my minimum needed NDK version. You can set it to every NDK- supported SDK version, available in the NDK.

Now you can compile for arm on linux by simply invoking "$CC" instead of "gcc" for normal linux c compiling.

Dekel
  • 192
  • 4
  • 12