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

发布时间:2021-09-24 22:11:41

一.编写helloworld.c Android.mk

??? [root@fontlose jni]# cat hello.c



#include
int main()
{
printf("Hello World!
");
return 0;

}


??? [root@fontlose jni]# cat Android.mk



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


二.编译
??? 1) 使用NDK r5 ????????? 目录结构? ─ hello

??????????????????????????????? ├── jni

??????????????????? ?? ?? ??? ??? ?├── Android.mk


??????????????????? ?? ?? ??? ??? ?└── hello.c



[root@fontlose nativity]# cd hello
[root@fontlose hello]# export NDK_PROJECT_PATH=`pwd`
[root@fontlose hello]# ndk-build
Install : helloworld => libs/armeabi/helloworld

[root@fontlose hello]# adb push libs/armeabi/helloworld /data
44 KB/s (2176 bytes in 0.047s)

[root@fontlose hello]# adb shell
/ # cd /data
/data # ls -l
-rwxrwxrwx root root 2176 2011-08-07 03:01 helloworld
/data # ./helloworld
Hello World!


??? 2) 使用通用toolchain ??????? 在hello.c目录下


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


?????? [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/



============================================
PLATFORM_VERSION_CODENAME=REL
PLATFORM_VERSION=2.3.2
TARGET_PRODUCT=generic
TARGET_BUILD_VARIANT=eng
TARGET_SIMULATOR=
TARGET_BUILD_TYPE=release
TARGET_BUILD_APPS=
TARGET_ARCH=arm
HOST_ARCH=x86
HOST_OS=linux
HOST_BUILD_TYPE=release
BUILD_ID=GRH78C
============================================
make: 进入目录“/opt/FriendlyARM/mini6410/android/Android-2.3”
target thumb C: helloworld <= development/hello/hello.c
target Executable: helloworld (out/target/product/generic/obj/EXECUTABLES/helloworld_intermediates/LINKED/helloworld)
target Non-prelinked: helloworld (out/target/product/generic/symbols/system/bin/helloworld)
target Strip: helloworld (out/target/product/generic/obj/EXECUTABLES/helloworld_intermediates/helloworld)
Install: out/target/product/generic/system/bin/helloworld
make: 离开目录“/opt/FriendlyARM/mini6410/android/Android-2.3”?

????? 5.编译完成执行文件在out/target/product/generic/symbols/system/bin/helloworld

??????? 下载至设备


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

??? 4) 使用源码自带Toolchain ?????? 在这之前要把上一步中的helloworld模块clean:


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


[root@fontlose Android-2.3]# make helloworld showcommands?????? 利用上面输出的编译命令,简化来手工编译helloworld程序,先手工删除上次编译得到的helloworld程序:


? [root@fontlose Android-2.3]# rm out/target/product/generic/system/bin/helloworld?????? gcc编译,生成目标文件


?????? [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?????? 生成可执行文件:


[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


[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


[root@fontlose Android-2.3]# adb push out/target/product/generic/symbols/system/bin/helloworld /data
145 KB/s (7608 bytes in 0.051s)
[root@fontlose Android-2.3]# adb shell
/ # cd /data/
/data # ls -l
-rwxrwxrwx root root 7608 2011-08-07 04:14 helloworld
/data # ./helloworld
Hello World!

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





相关文档

  • git上传项目到码云的步骤及报错Push rejected: Push to origin/master was rejected
  • 康佳云电视有什么软件可以看电视直播
  • Pycharm安装 - 永久破解
  • 成本会计主管员的岗位职责说明
  • 个性签名情侣版_幸福爱情签名
  • opencv c++函数 imgproc模块 8 Sobel 导数
  • 台式机无线网卡老是断怎么办
  • 小学安全自查报告书
  • 《春送僧》原文和翻译、《春送僧》赏析与评价 唐代贯休
  • concurrenthashmap Java7和Java8超详细笔记(第二版)
  • Redis能缓存的5种数据类型(存取)
  • 芦笋和洋葱能一起吃吗?芦笋和洋葱怎么炒好吃?
  • 钻孔抽芯检测规范钻孔抽芯检测技术在建筑工程中的实践论文
  • 轨道衡称重传感器原理 称重传感器的原理及应用
  • 描绘景色的优美句子
  • 第一个通过HCIEv3.0的咱的学员
  • PLSQL DEVELOPER没有工具栏了,设置处理
  • 第18届“明天小小科学家”奖励活动颁奖典礼在京举行
  • 怎么把梨花开
  • 著名的英语故事
  • 笔记90--WindowManager.LayoutParams2.1
  • 结婚怎么向父母说
  • 学生母亲节演讲稿
  • 日常减肥最快的方法
  • 六年级写人作文:我们班的“双面人”
  • 朝花夕拾读后感推荐3篇
  • 幼儿园小朋友的发言稿3篇
  • 高职美术教育问题以及对策
  • 夏新k2音响怎么换电池
  • 一起来捉妖怎样捉妖
  • 猜你喜欢

    电脑版