您的位置:首页 > 运维架构 > Linux

android手机编译可运行的linux程序

2017-01-25 09:40 429 查看
测试平台:三星S4,内核3.4.5(华为p8上也测试通过,内核3.10.61)
Ubuntu版本:14.04

1. 搭建交叉编译环境
1.1 下载交叉编译工具:
arm-none-linux-gnueabi,下载地址:
http://www.codesourcery.com/sgpp/lite/arm/portal/package7851/public/arm-none-linux-gnueabi/arm-2010.09-50-arm-none-linux-gnueabi-i686-pc-linux-gnu.tar.bz2
1.2 安装交叉编译工具:
将压缩包复制到/opt/toolchains/,运行解压命令tar -jxvf,解压得到arm-2010.09文件夹
1.3 配置环境变量
1)  su nano ~/.bashrc
2)  在文件末尾添加如下的一行
export PATH=$PATH:/opt/toolchains/arm-2010.09-50/bin
3)  保存文件并退出
4)  运行命令 source ~/.bashrc
5)  当然可以用命令行验证一下是否成功  arm-none-linux-gnueabi-gcc  -v。
注:配置好环境变量后,运行命令如果提示无法找到该文件,那么需要: apt-get install lib32z1,给系统装上32位软件的兼容库。

2. 编译C程序
新建hello.c
#include <stdio.h>

int main()
{
printf("hello world android linux grograming\n");
return;
}

编译:arm-none-linux-gnueabi-gcc -static -o hello hello.c
注:必须加上-static,否则运行时会提示./hello: No such file or directory。也就是在编译时将函数都静态编译到程序中了,运行时不用再动态连接,如果不加此选项,在android平台上就不让运行。

3. 运行
adb连接手机
1) adb push hello /sdcard/
2) adb shell
3)./hello
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息