如何编译Android动态链编的native c/c++code
时间:2008-11-20 来源:bigluo
编译环境要求:下载Android的源码,并执行完一次完整的编译。以下的所有命令均是在编译后的源码根目录下执行。
1. 编译C code
同样以hello.c为例:
#include <stdio.h> |
- 生成目标文件: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/libstdc++/include -I bionic/libc/kernel/common -I bionic/libc/kernel/arch-arm -include system/core/include/arch/linux-arm/AndroidConfig.h -c -o hello.o hello.c
- 生成可执行程序: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 hello -Lout/target/product/generic/obj/lib -Wl,-rpath-link=out/target/product/generic/obj/lib -lc -lstdc++ out/target/product/generic/obj/lib/crtbegin_dynamic.o 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
用命令file查看生成的hello文件属性:
hello: ELF 32-bit LSB executable, ARM, version 1 (SYSV), dynamically linked (uses shared libs), stripped
可以证明此时的hello是一个动态链编的文件。
2. 编译native c++ 代码
以hello_cpp为例:
hello_cpp.h |
- 编译目标文件:prebuilt/linux-x86/toolchain/arm-eabi-4.2.1/bin/arm-eabi-g++ -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 -include system/core/include/arch/linux-arm/AndroidConfig.h -fno-exceptions -fno-rtti -c -o hello_cpp.o hello_cpp.cpp
- 编译可执行程序:prebuilt/linux-x86/toolchain/arm-eabi-4.2.1/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 hello_cpp -Lout/target/product/generic/obj/lib -Wl,-rpath-link=out/target/product/generic/obj/lib -lc -lstdc++ out/target/product/generic/obj/lib/crtbegin_dynamic.o hello_cpp.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
同样用file查看hello_cpp的文件属性:
hello_cpp: ELF 32-bit LSB executable, ARM, version 1 (SYSV), dynamically linked (uses shared libs), stripped
但是很不幸的是,android自带的toolchain不支持C++标准库的开发,即所有的std namespace下的类均无法使用,包括基本的string。