1

I want to run a "hello world" program on Android.

It is written in C, and I tried different cross-compilers.

arm-elf-gcc : when I type "./hello" on my phone with adb I get a "Segmentation Fault". However, arm-elf-gdb runs the hello program normally, without a segmentation fault.

arm-linux-gcc : when I type "./hello" on my phone, I get "Illegal instruction"

Any ideas on how I can run my program on my phone?

Community
  • 1
  • 1
afisse
  • 11
  • 1
  • 3

2 Answers2

5

This tutorial seems to explain quite well how to build and debug native C applications for Android: http://betelco.blogspot.com/2010/01/buildingdebugging-android-native-c.html

Wolkenarchitekt
  • 20,170
  • 29
  • 111
  • 174
2

First, make sure you have the NDK:

http://developer.android.com/tools/sdk/ndk/index.html

Here is the easiest way to compile a C binary for your phone:

http://developer.android.com/tools/sdk/ndk/index.html

http://www.kandroid.org/ndk/docs/STANDALONE-TOOLCHAIN.html

Usually $NDK(may be different) =

Linux:

/home/<user>/android-ndk

Mac OS X:

/Users/<user>/android-ndk

In Terminal:

# create tool-chain - one line
$NDK/build/tools/make-standalone-toolchain.sh --platform=android-3 --install-dir=/tmp/my-android-toolchain

# add to terminal PATH variable
export PATH=/tmp/my-android-toolchain/bin:$PATH

# make alias CC be the new gcc binary
export CC=arm-linux-androideabi-gcc

# compile your C code(I tried hello world)
$CC -o foo.o -c foo.c

# push binary to phone
adb push foo.o /data/local/tmp

# execute binary
adb /data/local/tmp/foo.o
Jared Burrows
  • 54,294
  • 25
  • 151
  • 185
  • I tried your answer, and on my desktop, It should be `CC -o foo.o -c foo.c -fPIE -pie` and `adb shell /data/local/tmp/foo.o` Reference: (https://stackoverflow.com/questions/24818902/running-a-native-library-on-android-l-error-only-position-independent-executab) – Han Qiu Mar 20 '18 at 08:11