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

摘要:
LOCAL_PATH:=$include$LOCAL_SRC_FILES:=hello。cLOCAL_MODULE:=helloworld_MODULE_TAGS:=选项包括$II。编译1)使用NDKr5目录结构-helloг-jniг-Android。mkг-你好。查看平面复制?窗户:http://www.codesourcery.com/gnu_toolchains/arm/portal/package3400/public/arm-none-linux-gnueabi/arm-2008q3-41-arm-none-linux-gnueabi.exeLinux:http://www.codesourcery.com/gnu_toolchains/arm/portal/package3399/public/arm-无linux gnueabi/arm-2008q3-41-arm-None-linux-gnueabi。bin3)在源代码环境中编译1.在源代码开发目录中创建hello目录[plain]viewplaincopyprint?

一.编写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编译的应用程序只能采用静态编译的方式才能运行,使用静态方式编译出来的执行文件都比较大。

免责声明:文章转载自《Android Native C 之 Helloworld的四种编译方式_转载》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇elasticsearch之python备份Qt Charts的简单安装与使用下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

keil软件相关问题汇总

目录 一.keilc51——F12无法定位到定义处。 二、关于STM37F7的pack包不能安装报错SVD问题。 三、关于"Insufficient RAM for Flash Algorithms"出错原因及解决方案。 四、KEIL每次都要编译全部文件并且每个文件编译三次 五、flash算法存放文件夹 六、出现头文件嵌套无法打开.h文件和go to de...

Linux下NFS配置及远程挂载

NFS配置及远程挂载 NFS简介  NFS是Network File System的简写,即网络文件系统。NFS最早是由 Sun Microsystems 公司开发,并于1984年推出。NFS 是一种可分散式的网络文件系统,可以通过网络(一个局域网)使不同的机器、不同的操作系统,能够共享目录和文件,使客户端能通过网络访问并分享文件到位于服务端的磁盘中。NF...

操作笔记:linux下安装mysql

1,检查linux下是否安装了mysql shell指令如下: [root@iZ945sgm0ugZ ~]# rpm -qa|grep -i mysql 如果有的话:做出挨个删除(eg:rpm -ev mysql-connector-odbc-5.2.5-6.el7.x86_64) [root@iZ945sgm0ugZ ~]# rpm -qa|grep -...

RedHat yum安装配置

以前用fedora配置yum很方便,可是来到RHLE就不是那么顺手了,如果不做如下设置,经常出现不能通过yum安装软件的情况,下面是详细配置(网上找的) 首先,rhel5系统安装的时候其实已经有yum了,只是因为如果用官方的网站更新的话除非你是用钱买的rhel5.否则它会提示注册之类的。所以只要把 yum的更新地址改成开源的就行了。而限定yum更新地址的...

linux c 检测网络状态

转自:http://stackoverflow.com/questions/808560/how-to-detect-the-physical-connected-state-of-a-network-cable-connector You want to look at the nodes in /sys/class/net/ I experimente...

linux命令之添加删除磁盘分区

之前已经写过df和fdisk的区别了,df可以显示当前已经挂载的磁盘分区,df -T可以额外显示文件系统类型 fdisk -l可以显示出所有挂载未挂载的分区,但不显示文件类型 在我的虚拟机上有一块分配了40G的硬盘但未使用,使用fdisk -l 对于40G的磁盘就是只显示了 Disk /dev/sdb 40G,并未有对应的Device, 接下来就是对其分区...