Chinaunix

标题: <转载>如何编译Android动态链编的native c/c++代码 [打印本页]

作者: stanleylei    时间: 2008-11-17 21:55
标题: <转载>如何编译Android动态链编的native c/c++代码

                如何编译动态链编的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文件:

用命令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;
}执行以下命令完成:

同样用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




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2