如何编译Android动态链编的native c/c++code
推荐于2016-07-31 · 知道合伙人数码行家
huanglenzhi
知道合伙人数码行家
向TA提问 私信TA
知道合伙人数码行家
采纳数:117538
获赞数:517177
长期从事计算机组装,维护,网络组建及管理。对计算机硬件、操作系统安装、典型网络设备具有详细认知。
向TA提问 私信TA
关注
展开全部
编译环境要求:下载Android的源码,并执行完一次完整的编译。以下的所有命令均是在编译后的源码根目录下执行。
1. 编译C code
同样以hello.c为例:
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("hello, world!\n");
return 0;
}
执行以下步骤生成动态链编的binary文件:
生成目标文件: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
#ifndef HELLO_CPP_H
#define HELLO_CPP_H
class Hello
{
public:
Hello();
~Hello();
void printMessage(char* msg);
};
#endif
hello_cpp.cpp
#include <stdio.h>
#include "hello_cpp.h"
Hello::Hello()
{
}
Hello::~Hello()
{
}
void Hello::printMessage(char* msg)
{
printf("C++ example printing message: %s", msg);
}
int main(void)
{
Hello hello_obj;
hello_obj.printMessage("Hello world!\n");
return 0;
}
执行以下命令完成:
编译目标文件: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。
1. 编译C code
同样以hello.c为例:
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("hello, world!\n");
return 0;
}
执行以下步骤生成动态链编的binary文件:
生成目标文件: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
#ifndef HELLO_CPP_H
#define HELLO_CPP_H
class Hello
{
public:
Hello();
~Hello();
void printMessage(char* msg);
};
#endif
hello_cpp.cpp
#include <stdio.h>
#include "hello_cpp.h"
Hello::Hello()
{
}
Hello::~Hello()
{
}
void Hello::printMessage(char* msg)
{
printf("C++ example printing message: %s", msg);
}
int main(void)
{
Hello hello_obj;
hello_obj.printMessage("Hello world!\n");
return 0;
}
执行以下命令完成:
编译目标文件: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。
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询