출처 :
http://vilimpoc.org/blog/2010/09/26/straightforward-android-native-executables/
I spent some time trying to figure out how to build a native code "Hello, world!" program for Android and noticed that prior to the r4b release of the Android NDK, there is a lot of bad info out there on how to do this. Usually, it involves using some kind of unofficial cross-compiler, plus the entire source tree of the Android OS, and/or whatever other kludgy hacks like linking the executable statically to some kind of ARM libc. This seems bad. With the newest version of the NDK generating a proper gdbserver executable and gdb setup stubs, why anyone would want to do this by hand is beyond me.
There was a script that made sense at one point: agcc.pl (
here), which essentially takes all of the Makefile fragments and command-line switches and wraps them for you, letting you treat the NDK's cross-compilers almost like a normal gcc, but this is probably still more work than it's worth.
The Easy Way
The easiest way to get the NDK to build a native executable for you is just to use
include $(BUILD_EXECUTABLE)
at the end of your Android.mk, and then build like normal.
Anything else will just cause premature baldness.
Read on to see a fully-worked example...
hello-exe.c
2 | int main( int argc, char ** argv) |
4 | printf ( "Hello, world!\n" ); |
Android.mk
1 | LOCAL_PATH := $(call my-dir) |
5 | LOCAL_MODULE := hello-exe |
6 | LOCAL_SRC_FILES := hello-exe.c |
8 | include $(BUILD_EXECUTABLE) |
Example build + install output (it really is that easy)
02 | $ cd ~/android-ndk-r4b/samples/hello-exe |
04 | vilimpoc@funky ~/android-ndk-r4b/samples/hello-exe |
08 | vilimpoc@funky ~/android-ndk-r4b/samples/hello-exe |
09 | $ ~/android-ndk-r4b/ndk-build -B |
10 | Compile thumb : hello-exe <= /home/vilimpoc/android-ndk-r4b/samples/hello-exe/jni/hello-exe.c |
12 | Install : hello-exe => /home/vilimpoc/android-ndk-r4b/samples/hello-exe/libs/armeabi |
14 | vilimpoc@funky ~/android-ndk-r4b/samples/hello-exe |
18 | vilimpoc@funky ~/android-ndk-r4b/samples/hello-exe |
21 | vilimpoc@funky ~/android-ndk-r4b/samples/hello-exe/libs/armeabi |
25 | vilimpoc@funky ~/android-ndk-r4b/samples/hello-exe/libs/armeabi |
27 | List of devices attached |
30 | vilimpoc@funky ~/android-ndk-r4b/samples/hello-exe/libs/armeabi |
36 | vilimpoc@funky ~/android-ndk-r4b/samples/hello-exe/libs/armeabi |
37 | $ adb -e push hello-exe /data/data/hello-exe/hello-exe |
38 | 427 KB/s (0 bytes in 13670.000s) |
40 | vilimpoc@funky ~/android-ndk-r4b/samples/hello-exe/libs/armeabi |
44 | -rw-rw-rw- root root 13670 2010-09-23 12:37 hello-exe |
47 | -rwxrwxrwx root root 13670 2010-09-23 12:37 hello-exe |
53 | vilimpoc@funky ~/android-ndk-r4b/samples/hello-exe/libs/armeabi |
댓글 없음:
댓글 쓰기