【问题标题】:-bash: ./a.out: cannot execute binary file: Exec format error-bash:./a.out:无法执行二进制文件:执行格式错误
【发布时间】:2018-07-17 09:59:47
【问题描述】:

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

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

z.cpp:

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

并使用g++ z.cpp 编译。尝试调用 ./a.out 时,我在 Q 描述中收到错误,即:

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

在编译不太不同的 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++'

【问题讨论】:

  • 当您尝试在 64 位系统中执行 32 位应用程序时会出现该错误,但这似乎对您没有帮助.. 我也想知道 @987654321 @site 会比 SO 为您提供更多帮助。
  • @CIsForCookies:你能粘贴file a.out的输出吗?

标签: c++ bash compiler-errors g++


【解决方案1】:

发现问题...

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 很好。

【讨论】:

    猜你喜欢
    • 2021-09-25
    • 1970-01-01
    • 1970-01-01
    • 2019-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多