在android平台上测试C/C++程序及库

  int main( int argc, char *argv[] )
  {
  name.getname();
  name.sayhello();
  return 0;

  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;
  }

  上面的应用程序在编译时必须加上-static选项,也就是在编译时将函数都静态编译到程序中了,运行时不用再动态连接。如果不加此选项,在android平台上就不让运行。

  经过测试,将自己写的库放到/system/lib目录下,然后写个主程序来动态连接,也是无法运行。

  看来此平台做了限制,不让C/C++的程序运行时动态连接到这些C/C++库。

原文地址:https://www.cnblogs.com/chuncn/p/1414400.html