首 页 | 新 闻 | Symbian | Android| Windows Mobile | J2ME | 下载中心 | 游戏策划招聘与求职 | 购书指南 | 视频教程
您现在的位置: 开发视界 >> Android >> Android 入门 >> 正文
在android平台上测试C/C++程序及库
作者:佚名    文章来源:xiongcb.mentor100.com    更新时间:2008-8-29 11:44:09

android平台上带有标准C库,我们可以写个C程序来试试看能不能在上面运行。。。
首先下载并安装交叉编译工具GNU/ARM Linux gcc
http://www.codesourcery.com/gnu_toolchains/arm/download.html
安装时 直接解压就行了,要设置好PATH环境变量。

简单的C代码:
test.c

#include <stdio.h>
int main()
{
 int i,j;
 for(i=0;i<=10;i++)
 {
  for(j=0;j<=i;j++)
      printf(”*”);
  printf(”\n”);
  }
   return 0;
}

用刚下载的交叉编译工具编译源代码:
# arm-none-linux-gnueabi-gcc test.c -o test -static
-static选项在这里是必须的,不然android平台就不运行此程序。

这也说明了此平台上的C/C++库是不能被C/C++程序动态连接的 。

进入tools目录,用adb工具下载到android平台,放到/data/data目录。
 # ./adb push test /data/data
进入/data/data目录运行程序。
# cd /data/data
# ./test
*
**
***
****
*****
******
*******
********
*********
**********
***********

ok,It’s done !

C++程序一样的方法,只不过编译器换成:arm-none-linux-gnueabi-g++

附C++示例代码:
//

// HelloAndroid.cpp

//

//

#include <iostream>

using std::cin;

using std::cout;

using std::endl;

class MyName

{

  public:

    void getname( void );

    void sayhello( void );

  private:

    char name[ 255 ];

};

void MyName::getname( void )

{

  cout << “What is your name? “;

  cin >> name;

}

void MyName::sayhello( void )

{

  cout << “Welcome “ << name << ” to the world of Android << endl;

}

MyName name;

int main( int argc, char *argv[] )

{

  name.getname();

  name.sayhello();

  return 0;

}

参考网址:
http://www.anddev.org/native_c_-und-quothello_world-und-quot_working_in_emulator-t61.html

上面的应用程序在编译时必须加上-static选项,也就是在编译时将函数都静态编译到程序中了,运行时不用再动态连接。如果不加此选项,在android平台上就不让运行。
经过测试,将自己写的库放到/system/lib目录下,然后写个主程序来动态连接,也是无法运行。
看来此平台做了限制,不让C/C++的程序运行时动态连接到这些C/C++库。

相关文章:
没有相关文章
 

站点地图 | 加入收藏 | 联系站长 | 广告服务 |
QQ:280529124  Tel:0592-8271361 辽ICP备05021703号