Android下使用C语言的四种编译方式

一、编写helloworld.c Android.mk

<span style="font-size:18px;"><span style="font-size:24px;">#include <stdio.h>  
int main()  
{  
     printf("Hello World!\n");  
     return 0; 
}  

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

二.编译


1、使用NDK r5


ndk-build 命令的使用。
2、使用通用toolchain

arm-none-linux-gnueabi-gcc -static hello.c -o helloworld

adb push helloworld  /data
3、在源码环境中编译

在源码development 目录下创建hello目录,将helloworld.c Android.mk拷贝至hello目录下,然后mm即可。

4、使用源码自带Toolchain
在这之前要把上一步中的helloworld模块clean:

make clean-helloworld

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

make  helloworld  showcommands

(target Strip的作用是什么?)
        Android并没有采用glibc作为C库,而是采用了Google自己开发的Bionic Libc,通用的Toolchain用来编译和移植Android 的Linux内核是可行的,因为内核并不需要C库,但是开发Android的应用程序时,其他Toolchain编译的应用程序只能采用静态编译的方式才能运行,使用静态方式编译出来的执行文件都比较大。