android运行C程序以及C程序在android下的编译

来源:互联网 发布:淘宝商城男士洗面奶 编辑:程序博客网 时间:2024/05/22 15:21

在android的设计中,谷歌设计了一套专门为嵌入式设备使用的bionic C库,以替换原有的GUN Libc,这个精简的bionic库据说只有200多K,所以如果只想使用这个精简的C库像在linux下一样 开发C程序,基本是不可能的。当然如果只想让其在shell中运行还是可以做到的。

因为编译完的目标程序是在android下运行,就要使用交叉编译的工具,在下面地址下载:

http://www.codesourcery.com/gnu_toolchains/arm/download.html

下载完之后,bin目录下的arm-none-linux-gnueabi-gcc就是交叉编译器了

?
#include <stdio.h>
int main() {
    printf("nihao a\n");
    printf("你好 啊\n");
    return 1;
}

输入一下命令:

./arm-none-linux-gnueabi-gcc hello.c -o hello -static

-static选项在这里是必须的,否则会出现”not found”的错误。

然后就可以把编译好的hello传到手机上运行了。不过这里有个前提条件,要求android机器必须是root过的,好像简单的z4root还不行,必须使用更彻底的root方法,关于如何root,这里就不再赘述了,可以参考相关root的帖子。

adb push hello /dev/sample/

这里要上传的目录必须是root用户所有的。

然后就是运行程序,可以在adb shell里测试

adb shell

cd /dev/sample/

chmod 777 hello

./hello

或者在手机上安装超级终端,在终端里运行

./hello

./hello

原创粉丝点击