3

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

4

1 回答 1

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.

于 2012-09-27T12:11:27.207 回答