android 下之hello world c 手动编译

android c 编译使用m,mm,mmm

先source /build/envsetup.sh

然后是export TARGET_PRODUCT=omap3evm

mm.在当前目录下面,需要有Android.mk

mmm dir :需要在dir目录下面有Android.mk目录

make modules:cd /home/rowboat/rowboat_android1/&& make hello

编完之后,即修改了Android系统以后,可以使用 $ make snod 重新生成system.img

nvoke ". build/envsetup.sh" from your shell to add the following functions to your environment:
- croot:   Changes directory to the top of the tree.
- m:       Makes from the top of the tree.
- mm:      Builds all of the modules in the current directory.
- mmm:     Builds all of the modules in the supplied directories.
- cgrep:   Greps on all local C/C++ files.
- jgrep:   Greps on all local Java files.
- resgrep: Greps on all local res/*.xml files.
- godir:   Go to the directory containing a file.

Android SDK相当强大和全面了,但有时你的应用程序可能需要更多的功能,需要在androidlinux基础层面上运行应用程序。今天研究了如何在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

编译好之后,可用filereadelf查看可执行文件。

   # 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.solibm.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调用造成栈堆下溢。



Android Toolchain与Bionic LibcAndroid所用的Toolchain(即交叉编译工具链)可从下面的网址下载:http://android.kernel.org/pub/android-toolchain-20081019.tar.bz2。如果下载了完整的Android项目的源代码,则可以在“/prebuilt/linux-x86/toolchain/arm-eabi-4.2.1/bin”目录下找到交叉编译工具,比如Android所用的arm-eabi-gcc-4.2.1。Android并没有采用glibc作为C库,而是采用了Google自己开发的Bionic Libc,它的官方Toolchain也是基于Bionic Libc而并非glibc的。这使得使用或移植其他Toolchain来用于Android要比较麻烦:在Google公布用于Android的官方Toolchain之前,多数的Android爱好者使用的Toolchain是在http://www.codesourcery.com/gnu_toolchains/arm/download.html 下载的一个通用的Toolchain,它用来编译和移植Android 的Linux内核是可行的,因为内核并不需要C库,但是开发Android的应用程序时,直接采用或者移植其他的Toolchain都比较麻烦,其他Toolchain编译的应用程序只能采用静态编译的方式才能运行于Android模拟器中,这显然是实际开发中所不能接受的方式。目前尚没有看到说明成功移植其他交叉编译器来编译Android应用程序的资料。与glibc相比,Bionic Libc有如下一些特点:- 采用BSD License,而不是glibc的GPL License;- 大小只有大约200k,比glibc差不多小一半,且比glibc更快;- 实现了一个更小、更快的pthread;- 提供了一些Android所需要的重要函数,如”getprop”, “LOGI”等;- 不完全支持POSIX标准,比如C++ exceptions,wide chars等;- 不提供libthread_db 和 libm的实现 另外,Android中所用的其他一些二进制工具也比较特殊:- 加载动态库时使用的是/system/bin/linker而不是常用的/lib/ld.so;- prelink工具不是常用的prelink而是apriori,其源代码位于” /build/tools/apriori”- strip工具也没有采用常用的strip,即“/prebuilt/linux-x86/toolchain/arm-eabi-4.2.1/bin”目录下的arm-eabi-strip,而是位于/out/host/linux-x86/bin/的soslim工具。

上一篇中(即《Android编译环境(1) - 编译Native C的helloworld模块》),我们试用了通过标准的Android.mk文件来编译C模块,下面我们来试试如何直接运用gcc命令行来编译,从而了解Android编译环境的细节。

  Android编译环境提供了"showcommands"选项来显示编译命令行,我们可以通过打开这个选项来查看一些编译时的细节。当然,在这之前要把上一篇中的helloworld模块clean: 上面的"make clean-$(LOCAL_MODULE)"是Android编译环境提供的make clean的方式。

  接下来使用showcommands选项重新编译helloworld: # make helloworld showcommands build/core/product_config.mk:229: WARNING: adding test OTA key target thumb C: helloworld <= development/hello/hello.c prebuilt/linux-x86/toolchain/arm-eabi-4.2.1/bin/ar m-eabi-gcc-I system/core/include-I hardware/libhardware/include-I hardware/ril/include-I dalvik/libnativehelper/include-I frameworks/base/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-I development/hello-I out/target/product/generic/obj/EXECUTABLES/hellowo rld_intermediates-c-fno-exceptions -Wno-multichar -march=armv5te -mtune=xscale -msoft-float -fpic -mthumb-interwork -ffunction-sections -funwind-tables -fstack-protector -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 -DANDROID -fmessage-length=0 -W -Wall -Wno-unused -DSK_RELEASE -DNDEBUG -O2 -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/hellowo rld_intermediates/hello.o development/hello/hello.c target Executable: helloworld (out/target/product/generic/obj/EXECUTABLES/hellow orld_intermediates/LINKED/helloworld) prebuilt/linux-x86/toolchain/arm-eabi-4.2.1/bin/ar m-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/hellowo rld_intermediates/LINKED/helloworld -Lout/target/product/generic/obj/lib -Wl,-rpath-link=out/target/product/generic/obj/lib -lc -lstdc++ -lmout/target/product/generic/obj/lib/crtbegin_dynami c.oout/target/product/generic/obj/EXECUTABLES/hellowo rld_intermediates/hello.o-Wl,--no-undefined prebuilt/linux-x86/toolchain/arm-eabi-4.2.1/bin/.. /lib/gcc/arm-eabi/4.2.1/interwork/libgcc.a out/target/product/generic/obj/lib/crtend_android. o target Non-prelinked: helloworld (out/target/product/generic/symbols/system/bin/hel loworld) out/host/linux-x86/bin/acp -fpt out/target/product/generic/obj/EXECUTABLES/hellowo rld_intermediates/LINKED/helloworld out/target/product/generic/symbols/system/bin/hell oworld target Strip: helloworld (out/target/product/generic/obj/EXECUTABLES/hellow orld_intermediates/helloworld) out/host/linux-x86/bin/soslim --strip --shady --quiet out/target/product/generic/symbols/system/bin/hell oworld --outfile out/target/product/generic/obj/EXECUTABLES/hellowo rld_intermediates/helloworld Install: out/target/product/generic/system/bin/helloworld out/host/linux-x86/bin/acp -fpt out/target/product/generic/obj/EXECUTABLES/hellowo rld_intermediates/helloworld out/target/product/generic/system/bin/helloworld 从上面的命令行可以看到,Android编译环境所用的交叉编译工具链是prebuilt/linux-x86/toolchain/arm-eabi-4.2.1/bin/ar m-eabi-gcc,-I和-L参数指定了所用的C库头文件和动态库文件路径分别是bionic/libc/include和out/target/product/generic/obj/lib,其他还包括很多编译选项以及-D所定义的预编译宏。

  我们可以利用上面的编译命令,稍加简化来手工编译helloworld程序。先手工删除上次编译得到的helloworld程序: 再用gcc编译,生成目标文件: # prebuilt/linux-x86/toolchain/arm-eabi-4.2.1/bin/ar m-eabi-gcc -I bionic/libc/arch-arm/include -I bionic/libc/include -I bionic/libc/kernel/common-I bionic/libc/kernel/arch-arm -c-fno-exceptions -Wno-multichar -march=armv5te -mtune=xscale -msoft-float -fpic -mthumb-interwork -ffunction-sections -funwind-tables -fstack-protector -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 -DANDROID -fmessage-length=0 -W -Wall -Wno-unused -DSK_RELEASE -DNDEBUG -O2 -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/hellowo rld_intermediates/hello.o development/hello/hello.c 与Android.mk编译参数比较,上面主要减少了不必要的-I参数。

  接下来生成可执行文件: # prebuilt/linux-x86/toolchain/arm-eabi-4.2.1/bin/ar m-eabi-gcc -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/hellowo rld_intermediates/LINKED/helloworld -Lout/target/product/generic/obj/lib -Wl,-rpath-link=out/target/product/generic/obj/lib -lc -lmout/target/product/generic/obj/EXECUTABLES/hellowo rld_intermediates/hello.o out/target/product/generic/obj/lib/crtbegin_dynami c.o -Wl,--no-undefined ./prebuilt/linux-x86/toolchain/arm-eabi-4.2.1/bin/ ../lib/gcc/arm-eabi/4.2.1/interwork/libgcc.a out/target/product/generic/obj/lib/crtend_android. o 这里值得留意的是参数"-Wl,-dynamic-linker,/system/bin/linker",它指定了Android专用的动态链接器/system/bin/linker,而不是通常所用的ld.so。

  生成的可执行程序可用file和readelf命令来查看一下: # file out/target/product/generic/obj/EXECUTABLES/hellowo rld_intermediates/LINKED/helloworld out/target/product/generic/obj/EXECUTABLES/hellowo rld_intermediates/LINKED/helloworld: ELF 32-bit LSB executable, ARM, version 1 (SYSV), dynamically linked (uses shared libs), not stripped #readelf -d out/target/product/generic/obj/EXECUTABLES/hellowo rld_intermediates/LINKED/helloworld grep NEEDED 0x00000001 (NEEDED)Shared library: [libc.so] 0x00000001 (NEEDED)Shared library: [libm.so] 这是ARM格式的动态链接可执行文件,运行时需要libc.so和libm.so。"not stripped"表示它还没被STRIP。嵌入式系统中为节省空间通常将编译完成的可执行文件或动态库进行STRIP,即去掉其中多余的符号表信息。在前面"make helloworld showcommands"命令的最后我们也可以看到,Android编译环境中使用了out/host/linux-x86/bin/soslim工具进行STRIP
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值