首页 > 解决方案 > -bash:./a.out:无法执行二进制文件:执行格式错误

问题描述

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

我在我的 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 代码时:

质量控制:

#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++ 编译器中,因为在q.c编译时运行良好的 C 代码 () 在编译时gcc无法运行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++'

标签: c++bashcompiler-errorsg++

解决方案


发现问题...

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


推荐阅读