android2.2下编译c模块

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 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值