解决:/system/bin/sh: ./hello: No such file or directory

android中执行C可执行文件时,出现/system/bin/sh: ./hello: No such file or directory 错误。

1. 问题:

msm8909w:/data # ./hello
/system/bin/sh: ./hello: No such file or directory

2. 解决方法 1:
使用静态链接库:在最后加上-static

arm-linux-gnueabihf-gcc hello.c -o hello -static

3. 解决方法 2:
将linux的C动态链接库复制到android系统中的/lib目录下。因为/lib里面有很多链接,所以不能直接复制文件,要压缩后再复制,再解压,保证链接也一起被复制。
1). 在所用的arm-linux交叉编译工具中找到ld-linux-armhf.so.3所在的/lib目录
我目录是:
home/ubuntu/Public/gcc-linaro-4.9.4-2017.01-x86_64_arm-linux-gnueabihf/arm-linux-gnueabihf/libc/lib
解决:/system/bin/sh: ./hello: No such file or directory_第1张图片
2). 将其压缩。试了压缩成lib.tar,在安装系统中解压不了。用.tar.bz2格式可以解压:

 tar jcvf lib.tar.bz2 lib

3). 重新挂载文件系统为可读写

mount -o rw,remount /

4).将lib.tar.bz2用android系统的根目录

adb push lib.tar.bz2 /

5). 解压

tar jxvf lib.tar.bz2

解决:/system/bin/sh: ./hello: No such file or directory_第2张图片

6). 查看链接是否有

ls -l 

解决:/system/bin/sh: ./hello: No such file or directory_第3张图片

7). 一切准备好了,执行hello

msm8909w:/ # cd data
msm8909w:/data # ./hello
Hello World!
msm8909w:/data #

3. debug过程:
查看hello动态链接器路径:
输入命令

msm8909w:/data # file hello
hello: ELF shared object, 32-bit LSB arm, dynamic (/lib/ld-linux-armhf.so.3), BuildID=e8f27306e68a08e256aaff15db40859d94b65e53, not stripped

android系统的动态链接器与hello程序的动态链接器的路径不一样(安卓和普通linux的根文件目录不一样):
android系统的动态链接器路径:msm8909w:/system/lib
普通linux的动态链接器路径:/lib

参考:https://www.jianshu.com/p/ac9911eb7958?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

你可能感兴趣的:(学习,linux,ubuntu,shell,android)