int main( int argc, char *argv["> )
{
name.getname();
name.sayhello();
return 0;
android平台上带有标准c库,我们可以写个c程序来试试看能不能在上面运行。。。
首先下载并安装交叉编译工具gnu/arm linux gcc:
安装时 直接解压就行了,要设置好path环境变量。
简单的c代码:
test.c
#include
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
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++库。
编缉推荐阅读以下文章
">android 2.0常用功能截图
">实现一个用于显示当前时间的google android 窗口小部件(appwidget)
">android和ophone模拟器界面对比
">在android(ophone)模拟器中加载和使用sdcard卡
">android教程之实现动作感应技术
">android入门第六篇之listview (一)
">android入门第五篇之tablelayout (二)
">android入门第四篇之tablelayout (一)
">android入门第三篇之relativelayout、framelayout
">android入门第二篇之linearlayout、absolutelayout
![]()
如果喜欢在android平台上测试c/c++程序及库请收藏或告诉您的好朋友.
阅读(129) | 评论(0) | 转发(0) |