Android SDK相当强大和全面了,但有时你的应用程序可能需要更多的功能,需要在android的linux基础层面上运行应用程序。今天研究了如何在android环境下编译c模块,提供以下2种方法:

一、使用Android.mk自动编译:

1、在$(yourAndroid)/development目录下,创建一个hello目录。

#mkdir $(yourAndroid)/development/hello

其中$(yourandroid)指android的源代码目录;

2、在hello目录中,编写hello.c,内容如下:

#include <stdio.h>
int main()
{
printf("hello world/n");
exit(0);

//return 0;


}
3、在hello目录中,编写Android.mk, 内容如下:

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

LOCAL_SRC_FILES指定源文件,LOCAL_MODULE指定要编译的模块名,include $(BUILD_EXECUTABLE)指定编译成可执行文件,改为BUILD_SHARED_LIBRARY为动态链接库,这些可参考$(yourAndroid)/build/core/config.mk

4、回到Android源代码顶层目录,进行编译,cd $(your_andoird) && make helloworld

5、#adb push helloworld /data

二、手动编译:

1、编译成目标文件:

#$(yourAndroid)/prebuilt/linux-x86/toolchain/arm-eabi-4.2.1/bin/arm-eabi-gcc -I bionic/libc/arch-arm/include/ -I bionic/libc/include -I bionic/libc/kernel/common -I bionic/libc/kernel/arch-arm -c helloworld.c -o hello.o

2、生成可执行代码:

#$(yourAndroid)/prebuilt/linux-x86/toolchain/arm-eabi-4.2.1/bin/arm-eabi-gcc -nostdlib -Bdynamic -Wl,-T,build/core/armelf.x -Wl,-dynamic-linker,/system/bin/linker -Wl,--gc-sections -Wl,-z,nocopyreloc -o helloworld -Lout/target/product/generic/obj/lib -Wl,-rpath-link=out/target/product/generic/obj/lib -lc hello.o -entry=main

3、上传文件:

#adb push helloworld /data

编译好之后,可用file和readelf查看可执行文件。

# file helloworld

helloworld: ELF 32-bit LSB executable, ARM, version 1 (SYSV), dynamically linked (uses shared libs), not stripped

#readelf -d helloworld

Dynamic section at offset 0x1000 contains 12 entries:
Tag Type Name/Value
0x00000001 (NEEDED) Shared library: [libc.so]
0x00000004 (HASH) 0x80e8
0x00000005 (STRTAB) 0x8214
0x00000006 (SYMTAB) 0x8134
0x0000000a (STRSZ) 118 (bytes)
0x0000000b (SYMENT) 16 (bytes)
0x00000015 (DEBUG) 0x0
0x00000003 (PLTGOT) 0x9088
0x00000002 (PLTRELSZ) 16 (bytes)
0x00000014 (PLTREL) REL
0x00000017 (JMPREL) 0x828c
0x00000000 (NULL) 0x0
这是ARM格式的动态链接可执行文件,运行时需要libc.so和libm.so。"not stripped"表示它还没被STRIP。嵌入式系统中为节省空间通常将编译完成的可执行文件或动态库进行STRIP,即去掉其中多余的符号表信息。在前面"make helloworld showcommands"命令的最后我们也可以看到,Android编译环境中使用了out/host/linux-x86/bin/soslim工具进行STRIP。

备注: 在adb shell 终端下运行 ./helloworld如果出现

[1] + Stopped (signal) ./helloworld,

源文件中用exit(0) 替代return 0即可,出现的原因是这个位置的return调用造成栈堆下溢。

参考文摘:

1、http://blog.csdn.net/evilcode/archive/2010/07/14/5733390.aspx
2、http://wenku.baidu.com/view/76c618d5360cba1aa811dabd.html

转载:http://blog.csdn.net/liushaogeng/archive/2010/09/01/5855925.aspx

更多相关文章

  1. Android(安卓)Studio错误org.gradle.api.resources.ResourceExce
  2. systemtap在android、pandaboard上的移植
  3. 《第一行代码》阅读笔记(三十四)——Material Design
  4. Android自检工具
  5. 关于android生成debug和release版
  6. 修改编译Nexus5x android7.0.1(N)版本内核(AOSP)
  7. Bitmap保存图片到指定路径
  8. Ubuntu 16.04 LTS 编译 Android(安卓)7.1
  9. Android输出logs到文件

随机推荐

  1. 你知道 Android(安卓)的 MessageQueue.Id
  2. Android开发错误Unable to execute dex:
  3. Android(安卓)Studio 无法引用org.apache
  4. 制作ota升级包之error:Could not create t
  5. Android(安卓)API 28 访问服务器失败 提
  6. android 中文 api (64) —— Scroller
  7. Android(安卓)加载.gif格式图片
  8. Android配置----Eclipse+BlueStacks调试A
  9. 关于Android(安卓)API,你所不知道的
  10. android dialog