1. 程式人生 > 其它 >解決:/system/bin/sh: ./hello: No such file or directory

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

技術標籤:學習linuxubuntushellandroid

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
在這裡插入圖片描述
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

在這裡插入圖片描述

6). 檢視連結是否有

ls -l 

在這裡插入圖片描述

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