• Android Native C 之 Helloworld的四种编译方式_转载


    一.编写helloworld.c Android.mk

        [root@fontlose jni]# cat hello.c 

    1. #include <stdio.h>  
    2. int main()  
    3. {  
    4.     printf("Hello World!\n");  
    5.     return 0;  
    6.   
    7. }  

        [root@fontlose jni]# cat Android.mk

    1. LOCAL_PATH:= $(call my-dir)  
    2. include $(CLEAR_VARS)  
    3. LOCAL_SRC_FILES:=hello.c  
    4. LOCAL_MODULE := helloworld  
    5. LOCAL_MODULE_TAGS := optional  
    6. include $(BUILD_EXECUTABLE)  

    二.编译

        1) 使用NDK r5

              目录结构  ─ hello
                                    ├── jni
                                       ├── Android.mk

                                       └── hello.c

     

    1.  [root@fontlose nativity]# cd hello  
    2.  [root@fontlose hello]# export NDK_PROJECT_PATH=`pwd`  
    3.  [root@fontlose hello]# ndk-build  
    4.  Install        : helloworld => libs/armeabi/helloworld  
    5.   
    6.  [root@fontlose hello]# adb push libs/armeabi/helloworld  /data  
    7.  44 KB/s (2176 bytes in 0.047s)  
    8.   
    9.  [root@fontlose hello]# adb shell  
    10.  / # cd  /data  
    11.  /data # ls -l  
    12.  -rwxrwxrwx root     root         2176 2011-08-07 03:01 helloworld  
    13. /data # ./helloworld  
    14.  Hello World!  

     

        2) 使用通用toolchain

            在hello.c目录下

    1.        [root@fontlose jni]# arm-none-linux-gnueabi-gcc -static hello.c -o helloworld  
    2.        [root@fontlose jni]# adb push helloworld  /data  
    3.        1718 KB/s (581219 bytes in 0.330s)  
    4.        [root@fontlose jni]# adb shell  
    5.        / # cd /data  
    6.        /data # ls -l           
    7.       -rwxrwxrwx root     root       581219 2011-08-07 03:28 helloworld  
    8.        /data # ./helloworld  
    9.        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目录

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

           2.将helloworld.c Android.mk拷贝至hello目录下
           3. [root@fontlose Android-2.3]# . build/envsetup.sh

           4. [root@fontlose Android-2.3]# mmm development/hello/

     

    1. ============================================  
    2. PLATFORM_VERSION_CODENAME=REL  
    3. PLATFORM_VERSION=2.3.2  
    4. TARGET_PRODUCT=generic  
    5. TARGET_BUILD_VARIANT=eng  
    6. TARGET_SIMULATOR=  
    7. TARGET_BUILD_TYPE=release  
    8. TARGET_BUILD_APPS=  
    9. TARGET_ARCH=arm  
    10. HOST_ARCH=x86  
    11. HOST_OS=linux  
    12. HOST_BUILD_TYPE=release  
    13. BUILD_ID=GRH78C  
    14. ============================================  
    15. make: 进入目录“/opt/FriendlyARM/mini6410/android/Android-2.3”  
    16. target thumb C: helloworld <= development/hello/hello.c  
    17. target Executable: helloworld (out/target/product/generic/obj/EXECUTABLES/helloworld_intermediates/LINKED/helloworld)  
    18. target Non-prelinked: helloworld (out/target/product/generic/symbols/system/bin/helloworld)  
    19. target Strip: helloworld (out/target/product/generic/obj/EXECUTABLES/helloworld_intermediates/helloworld)  
    20. Install: out/target/product/generic/system/bin/helloworld  
    21. make: 离开目录“/opt/FriendlyARM/mini6410/android/Android-2.3”   

     

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

    1.        [root@fontlose Android-2.3]# adb push out/target/product/generic/symbols/system/bin/helloworld  /data  
    2.        135 KB/s (7608 bytes in 0.054s)  
    3.        [root@fontlose Android-2.3]# adb shell  
    4.        / # cd /data  
    5.        /data # ls -l      
    6.        -rwxrwxrwx root     root         7608 2011-08-07 03:49 helloworld       
    7.        /data # ./helloworld     
    8.        Hello World!  

        4) 使用源码自带Toolchain

           在这之前要把上一步中的helloworld模块clean:

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

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

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

           利用上面输出的编译命令,简化来手工编译helloworld程序,先手工删除上次编译得到的helloworld程序:

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

           gcc编译,生成目标文件

    1.        [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  

           生成可执行文件:

    1. [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

    1. [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  
    1. [root@fontlose Android-2.3]# adb push out/target/product/generic/symbols/system/bin/helloworld  /data  
    2. 145 KB/s (7608 bytes in 0.051s)  
    3. [root@fontlose Android-2.3]# adb shell  
    4. / # cd /data/  
    5. /data # ls -l  
    6. -rwxrwxrwx root     root         7608 2011-08-07 04:14 helloworld  
    7. /data # ./helloworld  
    8. Hello World!  

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

  • 相关阅读:
    JS基础
    NodeJs实现他人项目实例
    Node.js在任意目录下使用express命令‘不是内部或外部命令’解决方法
    HTTP基本知识
    RESTful API
    基本概念和方法1
    Node.js--安装express以及创建第一个express项目(windows)
    Node-debug方法
    css3动画划过有一个框
    translate 动画不同时间飞入文字
  • 原文地址:https://www.cnblogs.com/c6000/p/2720616.html
Copyright © 2020-2023  润新知