-bash:/a.out:无法执行二进制文件:Exec格式错误

-bash: ./a.out: cannot execute binary file: Exec format error

本文关键字:二进制文件 Exec 错误 执行 格式 out -bash      更新时间:2023-10-16

我在这个错误上发现了一些悬而未决的问题,但没有一个是相关的。

我在我的VM上写了最简单的C++代码(Ubuntu 14.04.3 LTSsudo virt-what输出为vmware):

z.cpp:

#include <iostream>
int main(){
std::cout << "hello world" << std::endl;
return 0;
}

并用CCD_ 3编译。当尝试调用./a.out时,我在Q描述中得到错误,即:

-bash:/a.out:无法执行二进制文件:Exec格式错误

编译一个不那么不同的C代码时:

q.c:

#include <stdio.h>
int main(){
puts("hello world");
return 0;
}

有了gcc q.c,我没有问题,./a.out的输出与预期的"hello world"一样


这是我的dpkg --list | grep compiler:

ii  g++                                          4:4.8.2-1ubuntu6                                    i386         GNU C++ compiler
ii  g++-4.8                                      4.8.4-2ubuntu1~14.04                                i386         GNU C++ compiler
ii  gcc                                          4:4.8.2-1ubuntu6                                    i386         GNU C compiler
ii  gcc-4.8                                      4.8.4-2ubuntu1~14.04                                i386         GNU C compiler
ii  hardening-includes                           2.5ubuntu2.1                                        all          Makefile for enabling compiler flags for security hardening
ii  libllvm3.5:i386                              1:3.5-4ubuntu2~trusty2                              i386         Modular compiler and toolchain technologies, runtime library
ii  libxkbcommon0:i386                           0.4.1-0ubuntu1                                      i386         library interface to the XKB compiler - shared library

问题显然存在于g++编译器中,因为用gcc编译时运行良好的C代码(q.c(,用g++编译时无法运行。然而,我不知道编译器中到底有什么可能是错误的


file a.out = a.out: ELF 32-bit MSB executable, PowerPC or cisco 4500, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.10, not stripped


已经回答了,但为了问题的完整性,这里是最后一个产生差异的拼图(尽管我第一次发布Q时没有想过检查这个(:

alias g++='/opt/Cross_Tools/powerpc-linux-gnu/bin/powerpc-linux-gnu-g++'

发现问题。。。

g++命令实际上是在制作一个32位的应用程序(从file a.out的输出中可以看出(。原因是我有一个我不知道的别名:

alias g++='/opt/Cross_Tools/powerpc-linux-gnu/bin/powerpc-linux-gnu-g++'

这使得我的g++ z.cpp命令不使用实际的/usr/bin/g++,而是使用交叉编译器。当用make z编译时,a.out是好的。