c++ - -bash:./a.out:无法执行二进制文件:执行格式错误
问题描述
我在这个错误上发现了一些未解决的问题,但没有一个是相关的。
我在我的 VM 上编写了最简单的 C++ 代码(Ubuntu 14.04.3 LTS,sudo 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++'
解决方案
发现问题...
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
。
推荐阅读
- google-api - 无法按 ID 获取特定的 YouTube 订阅
- ruby-on-rails - ActiveAdmin 降价编辑器
- centos7 - 努力在 CentOS7 上用 yum 安装 Puppetserver
- python - 使用 flask_socketio 将视频流式传输到 Flask 应用程序
- oracle - Shell 脚本中的 Oracle 匿名块调用
- android - 从服务器接收到一个极长的令牌,如何使它多行,这样我就不必拖动滚动条来复制它?
- android - 在辅助功能服务中的另一个应用程序上显示 Toast
- r - 构建遍历列的函数
- asp.net - 在 aspnet 核心中使用策略覆盖 AuthorizeAttribute
- php - Symfony:项目创建