免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2003 | 回复: 0
打印 上一主题 下一主题

<转载>如何编译Android动态链编的native c/c++代码 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-11-17 21:55 |只看该作者 |倒序浏览

                如何编译动态链编的native c/c++代码
                                               
                                               
                                                                                                                                                                                                                        在另外一个帖子
Native Hello World on Android
中,neil已经介绍了如何编译一个静态的native程序在Android中运行,本文主要说明如何编译动态链编的native程序。
编译环境要求:下载Android的源码,并执行完一次完整的编译。以下的所有命令均是在编译后的源码根目录下执行。
1. 编译C code
同样以hello.c为例:复制内容到剪贴板代码:#include
#include
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
#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。
               
               
               
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/48729/showart_1421210.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP