尝试执行文件时出现"sh: ./<file> not found"错误

"sh: ./<file> not found" error when trying to execute a file

本文关键字:lt file gt 错误 found not 文件 执行 sh      更新时间:2023-10-16

我遇到了一个我所遇到过的最奇怪的问题。我正在交叉编译ARM CPU与Linux板上的应用程序。我正在使用buildroot,一切都很顺利,直到我试图在目标上运行应用程序:我得到了-sh: ./hw: not found。例如:

$ cat /tmp/test.cpp 
#include <cstdio>
#include <vector>
int main(int argc, char** argv){
        printf("Hello Kitty!n");
        return 0;
}
$ ./arm-linux-g++ -march=armv7-a /tmp/test.cpp -o /tftpboot/hw

加载可执行文件到目标;然后在目标上发出:

# ./hw
-sh: ./hw: Permission denied
# chmod +x ./hw
# ./hw
-sh: ./hw: not found
# ls -l ./hw
-rwxr-xr-x    1 root     root          6103 Jan  1 03:40 ./hw

还有更多:在使用发行版编译器(如arm-linux-gnueabi-g++ -march=armv7-a /tmp/test.cpp -o /tftpboot/hw)构建时,应用程序运行良好!

我通过readelf -a -W /tftpboot/hw比较了可执行文件,但没有注意到太大的差异。我把两个输出都粘贴在这里。我唯一注意到的是Version5 EABI, soft-float ABIVersion5 EABI。我试图通过传递-mfloat-abi=softfp-mfloat-abi=soft来消除差异,但编译器似乎忽略了它。不过,我想这并不重要,因为编译器甚至不会发出警告。

我还认为,如果可执行文件在某些方面不兼容,可能sh会输出此错误。但是在我的主机PC上,我看到了另一个错误,例如:

$ sh /tftpboot/hw
/tftpboot/hw: 1: /tftpboot/hw: Syntax error: word unexpected (expecting ")")

sh打印这个奇怪的错误,因为它试图运行你的程序作为一个shell脚本!

你的错误./hw: not found可能是由于没有找到动态链接器(又名ELF解释器)引起的。试着用-static编译它作为一个静态程序,或者用你的动态加载器:# /lib/ld-linux.so.2 ./hw或类似的东西运行它。

如果问题是动态加载器在你的工具链和运行时环境中的命名不同,你可以修复它:

  • 在运行时环境中:带有符号链接。在工具链中:使用-Wl,--dynamic-linker=/lib/ld-linux.so.2