您的位置:首页 > 移动开发 > Android开发

Android Native C 之 Helloworld的四种编译方式

2011-11-23 18:11 645 查看
一.编写helloworld.c Android.mk

[root@fontlose jni]# cat hello.c

view plaincopy to
clipboardprint?

#include <stdio.h>
int main()
{
printf("Hello World!\n");
return 0;

}

view plaincopy to
clipboardprint?

LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES:=hello.c
LOCAL_MODULE := helloworld
LOCAL_MODULE_TAGS := optional
include $(BUILD_EXECUTABLE)

LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES:=hello.c
LOCAL_MODULE := helloworld
LOCAL_MODULE_TAGS := optional
include $(BUILD_EXECUTABLE)

二.编译

1) 使用NDK r5

目录结构 ─ hello

├── jni

├── Android.mk

└── hello.c

view plaincopy to
clipboardprint?

[root@fontlose nativity]# cd hello
[root@fontlose hello]# export NDK_PROJECT_PATH=`pwd`
[root@fontlose hello]# ndk-build
Install : helloworld => libs/armeabi/helloworld

[root@fontlose hello]# adb push libs/armeabi/helloworld /data

44 KB/s (2176 bytes in 0.047s)

[root@fontlose hello]# adb shell
/ # cd /data
/data # ls -l
-rwxrwxrwx root root 2176 2011-08-07 03:01 helloworld

/data # ./helloworld
Hello World!

view plaincopy to
clipboardprint?

[root@fontlose jni]# arm-none-linux-gnueabi-gcc -static hello.c -o helloworld

[root@fontlose jni]# adb push helloworld  /data
1718 KB/s (581219 bytes in 0.330s)
[root@fontlose jni]# adb shell
/ # cd /data
/data # ls -l
-rwxrwxrwx root     root       581219 2011-08-07 03:28 helloworld

/data # ./helloworld
Hello World!

[root@fontlose jni]# arm-none-linux-gnueabi-gcc -static hello.c -o helloworld
[root@fontlose jni]# adb push helloworld  /data
1718 KB/s (581219 bytes in 0.330s)
[root@fontlose jni]# adb shell
/ # cd /data
/data # ls -l
-rwxrwxrwx root     root       581219 2011-08-07 03:28 helloworld
/data # ./helloworld
Hello World!


Windows: http://www.codesourcery.com/gnu_toolchains/arm/portal/package3400/public/arm-none-linux-gnueabi/arm-2008q3-41-arm-none-linux-gnueabi.exe
Linux: http://www.codesourcery.com/gnu_toolchains/arm/portal/package3399/public/arm-none-linux-gnueabi/arm-2008q3-41-arm-none-linux-gnueabi.bin

3) 在源码环境中编译

1. 在源码development 目录下创建hello目录

view plaincopy to
clipboardprint?

[root@fontlose Android-2.3]# mkdir $Android_Src/development/hello

view plaincopy to
clipboardprint?

============================================

PLATFORM_VERSION_CODENAME=REL
PLATFORM_VERSION=2.3.2
TARGET_PRODUCT=generic
TARGET_BUILD_VARIANT=eng
TARGET_SIMULATOR=
TARGET_BUILD_TYPE=release
TARGET_BUILD_APPS=
TARGET_ARCH=arm
HOST_ARCH=x86
HOST_OS=linux
HOST_BUILD_TYPE=release
BUILD_ID=GRH78C
============================================
make: 进入目录“/opt/FriendlyARM/mini6410/android/Android-2.3”

target thumb C: helloworld <= development/hello/hello.c
target Executable: helloworld (out/target/product/generic/obj/EXECUTABLES/helloworld_intermediates/LINKED/helloworld)

target Non-prelinked: helloworld (out/target/product/generic/symbols/system/bin/helloworld)

target Strip: helloworld (out/target/product/generic/obj/EXECUTABLES/helloworld_intermediates/helloworld)

Install: out/target/product/generic/system/bin/helloworld
make: 离开目录“/opt/FriendlyARM/mini6410/android/Android-2.3”

============================================
PLATFORM_VERSION_CODENAME=REL
PLATFORM_VERSION=2.3.2
TARGET_PRODUCT=generic
TARGET_BUILD_VARIANT=eng
TARGET_SIMULATOR=
TARGET_BUILD_TYPE=release
TARGET_BUILD_APPS=
TARGET_ARCH=arm
HOST_ARCH=x86
HOST_OS=linux
HOST_BUILD_TYPE=release
BUILD_ID=GRH78C
============================================
make: 进入目录“/opt/FriendlyARM/mini6410/android/Android-2.3”
target thumb C: helloworld <= development/hello/hello.c
target Executable: helloworld (out/target/product/generic/obj/EXECUTABLES/helloworld_intermediates/LINKED/helloworld)
target Non-prelinked: helloworld (out/target/product/generic/symbols/system/bin/helloworld)
target Strip: helloworld (out/target/product/generic/obj/EXECUTABLES/helloworld_intermediates/helloworld)
Install: out/target/product/generic/system/bin/helloworld
make: 离开目录“/opt/FriendlyARM/mini6410/android/Android-2.3”


5.编译完成执行文件在out/target/product/generic/symbols/system/bin/helloworld

下载至设备

view plaincopy to
clipboardprint?

[root@fontlose Android-2.3]# adb push out/target/product/generic/symbols/system/bin/helloworld /data

135 KB/s (7608 bytes in 0.054s)
[root@fontlose Android-2.3]# adb shell
/ # cd /data
/data # ls -l
-rwxrwxrwx root root 7608 2011-08-07 03:49 helloworld

/data # ./helloworld
Hello World!

view plaincopy to
clipboardprint?

[root@fontlose Android-2.3]# make clean-helloworld

[root@fontlose Android-2.3]# make clean-helloworld
使用showcommands选项重新编译helloworld,查看具体命令,从输出命令行可以看到,Android编译环境所用的交叉编译工具链是prebuilt/linux-x86/toolchain/arm-eabi-4.4.3/bin/arm-eabi-gcc。

view plaincopy to
clipboardprint?

[root@fontlose Android-2.3]# make helloworld showcommands

view plaincopy to
clipboardprint?

[root@fontlose Android-2.3]#  rm out/target/product/generic/system/bin/helloworld

[root@fontlose Android-2.3]#  rm out/target/product/generic/system/bin/helloworld
gcc编译,生成目标文件

view plaincopy to
clipboardprint?

[root@fontlose Android-2.3]# prebuilt/linux-x86/toolchain/arm-eabi-4.4.3/bin/arm-eabi-gcc -I development/hello -I out/target/product/generic/obj/EXECUTABLES/helloworld_intermediates -I dalvik/libnativehelper/include/nativehelper -I system/core/include -I hardware/libhardware/include -I hardware/libhardware_legacy/include -I hardware/ril/include -I dalvik/libnativehelper/include -I frameworks/base/include -I frameworks/base/opengl/include -I frameworks/base/native/include -I external/skia/include -I out/target/product/generic/obj/include -I bionic/libc/arch-arm/include -I bionic/libc/include -I bionic/libstdc++/include -I bionic/libc/kernel/common -I bionic/libc/kernel/arch-arm -I bionic/libm/include -I bionic/libm/include/arch/arm -I bionic/libthread_db/include -c -fno-exceptions -Wno-multichar -msoft-float -fpic -ffunction-sections -funwind-tables -fstack-protector -Wa,--noexecstack -Werror=format-security -fno-short-enums -march=armv5te -mtune=xscale -D__ARM_ARCH_5__ -D__ARM_ARCH_5T__ -D__ARM_ARCH_5E__ -D__ARM_ARCH_5TE__ -include system/core/include/arch/linux-arm/AndroidConfig.h -I system/core/include/arch/linux-arm/ -Wno-psabi -mthumb-interwork -DANDROID -fmessage-length=0 -W -Wall -Wno-unused -Winit-self -Wpointer-arith -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -DNDEBUG -g -Wstrict-aliasing=2 -finline-functions -fno-inline-functions-called-once -fgcse-after-reload -frerun-cse-after-loop -frename-registers -DNDEBUG -UDEBUG -mthumb -Os -fomit-frame-pointer -fno-strict-aliasing -finline-limit=64 -MD -o out/target/product/generic/obj/EXECUTABLES/helloworld_intermediates/hello.o development/hello/hello.c

view plaincopy to
clipboardprint?

[root@fontlose Android-2.3]# prebuilt/linux-x86/toolchain/arm-eabi-4.4.3/bin/arm-eabi-g++ -nostdlib -Bdynamic -Wl,-T,build/core/armelf.x -Wl,-dynamic-linker,/system/bin/linker -Wl,--gc-sections -Wl,-z,nocopyreloc -o out/target/product/generic/obj/EXECUTABLES/helloworld_intermediates/LINKED/helloworld -Lout/target/product/generic/obj/lib -Wl,-rpath-link=out/target/product/generic/obj/lib -lc -lstdc++ -lm  out/target/product/generic/obj/lib/crtbegin_dynamic.o         out/target/product/generic/obj/EXECUTABLES/helloworld_intermediates/hello.o        -Wl,-z,noexecstack    -Wl,--no-undefined  prebuilt/linux-x86/toolchain/arm-eabi-4.4.3/bin/../lib/gcc/arm-eabi/4.4.3/libgcc.a out/target/product/generic/obj/lib/crtend_android.o

[root@fontlose Android-2.3]# prebuilt/linux-x86/toolchain/arm-eabi-4.4.3/bin/arm-eabi-g++ -nostdlib -Bdynamic -Wl,-T,build/core/armelf.x -Wl,-dynamic-linker,/system/bin/linker -Wl,--gc-sections -Wl,-z,nocopyreloc -o out/target/product/generic/obj/EXECUTABLES/helloworld_intermediates/LINKED/helloworld -Lout/target/product/generic/obj/lib -Wl,-rpath-link=out/target/product/generic/obj/lib -lc -lstdc++ -lm  out/target/product/generic/obj/lib/crtbegin_dynamic.o         out/target/product/generic/obj/EXECUTABLES/helloworld_intermediates/hello.o        -Wl,-z,noexecstack    -Wl,--no-undefined  prebuilt/linux-x86/toolchain/arm-eabi-4.4.3/bin/../lib/gcc/arm-eabi/4.4.3/libgcc.a out/target/product/generic/obj/lib/crtend_android.o
使用了out/host/linux-x86/bin/soslim工具进行STRIP

view plaincopy to
clipboardprint?

[root@fontlose Android-2.3]# out/host/linux-x86/bin/soslim --strip --shady --quiet out/target/product/generic/symbols/system/bin/helloworld --outfile out/target/product/generic/obj/EXECUTABLES/helloworld_intermediates/helloworld

view plaincopy to
clipboardprint?

[root@fontlose Android-2.3]# adb push out/target/product/generic/symbols/system/bin/helloworld  /data

145 KB/s (7608 bytes in 0.051s)
[root@fontlose Android-2.3]# adb shell
/ # cd /data/
/data # ls -l
-rwxrwxrwx root     root         7608 2011-08-07 04:14 helloworld

/data # ./helloworld
Hello World!

[root@fontlose Android-2.3]# adb push out/target/product/generic/symbols/system/bin/helloworld  /data
145 KB/s (7608 bytes in 0.051s)
[root@fontlose Android-2.3]# adb shell
/ # cd /data/
/data # ls -l
-rwxrwxrwx root     root         7608 2011-08-07 04:14 helloworld
/data # ./helloworld
Hello World!

Android并没有采用glibc作为C库,而是采用了Google自己开发的Bionic Libc,通用的Toolchain用来编译和移植Android 的Linux内核是可行的,因为内核并不需要C库,但是开发Android的应用程序时,其他Toolchain编译的应用程序只能采用静态编译的方式才能运行,使用静态方式编译出来的执行文件都比较大。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: