【问题标题】:bash: ./main: cannot execute binary file: Exec format error [closed]bash:./main:无法执行二进制文件:执行格式错误[关闭]
【发布时间】:2019-08-11 02:04:37
【问题描述】:

我知道以前有人问过这个问题,但没有一个答案对我有帮助。

我安装了 Ubuntu 18.04 LTS,当我尝试运行我的 C++ 程序时,我收到此错误:

bash: ./main: cannot execute binary file: Exec format error

我知道当尝试在 64 位机器上运行 32 位程序时会发生此错误。但是在终端输出中使用文件 main

main: ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), not stripped

并使用 uname -a 输出

Linux florian 4.15.0-46-generic #49-Ubuntu SMP Wed Feb 6 09:33:07 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

我忽略了什么?

提前致谢

编辑:

我使用

编译程序
g++ -Wall -c -std=c++14 -o main main.cpp

包含文件的文件夹如下所示:

https://www.bilder-upload.eu/bild-cf6106-1553082433.png.html

Game.h 包含在 main.cpp 中,这就是我使用 -c 标志的原因

尝试在没有 -c 标志的情况下编译文件会给我这个终端输出:

    /tmp/ccBqZfJw.o: In function `main':
main.cpp:(.text+0x63): undefined reference to `Sep::Game::Game()'
main.cpp:(.text+0xaf): undefined reference to `Sep::Game::loadConfig(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
main.cpp:(.text+0xec): undefined reference to `Sep::Game::printMap()'
main.cpp:(.text+0x102): undefined reference to `Sep::Game::addWorm(int, int)'
main.cpp:(.text+0x121): undefined reference to `Sep::Game::printMap()'
main.cpp:(.text+0x13c): undefined reference to `Sep::Game::move(int, int, int)'
main.cpp:(.text+0x152): undefined reference to `Sep::Game::addWorm(int, int)'
main.cpp:(.text+0x17b): undefined reference to `Sep::Game::addWorm(int, int)'
main.cpp:(.text+0x19a): undefined reference to `Sep::Game::printMap()'
main.cpp:(.text+0x1b5): undefined reference to `Sep::Game::move(int, int, int)'
main.cpp:(.text+0x1d0): undefined reference to `Sep::Game::move(int, int, int)'
main.cpp:(.text+0x1eb): undefined reference to `Sep::Game::move(int, int, int)'
main.cpp:(.text+0x1f7): undefined reference to `Sep::Game::printMap()'
main.cpp:(.text+0x20d): undefined reference to `Sep::Game::addWorm(int, int)'
main.cpp:(.text+0x233): undefined reference to `Sep::Game::addWorm(int, int)'
main.cpp:(.text+0x25e): undefined reference to `Sep::Game::move(int, int, int)'
main.cpp:(.text+0x26f): undefined reference to `Sep::Game::~Game()'
main.cpp:(.text+0x2b9): undefined reference to `Sep::Game::~Game()'
collect2: error: ld returned 1 exit status

编辑:

在尝试将它与其他文件同时编译后,似乎我们离解决方案越来越近了,但仍然存在一些错误:

命令:

g++ -Wall -std=c++14 -o main main.cpp Field.cpp Game.cpp

输出:

/tmp/ccrCypxF.o: In function `main':
main.cpp:(.text+0x26f): undefined reference to `Sep::Game::~Game()'
main.cpp:(.text+0x2b9): undefined reference to `Sep::Game::~Game()'
/tmp/ccxLZpfi.o: In function `Sep::Game::Game()':
Game.cpp:(.text+0x1f): undefined reference to `vtable for Sep::Game'
/tmp/ccxLZpfi.o: In function `Sep::Game::printMap()':
Game.cpp:(.text+0x104e): undefined reference to `Sep::Field::Field()'
/tmp/ccxLZpfi.o: In function `Sep::Game::move(int, int, int)':
Game.cpp:(.text+0x133b): undefined reference to `Sep::Field::Field()'
collect2: error: ld returned 1 exit status

【问题讨论】:

  • 您是否在同一个机器上编译了该二进制文件?如果不是,您编译的机器是否与您正在运行的机器具有相同的处理器制造商?我唯一能想到的是它使用了它无法理解的特定 Intel/AMD 指令。
  • 看起来你构建了一个目标文件而不是一个可执行文件。你有没有用g++ -c -o main main.cpp 之类的东西编译它? (如果你这样做了,-c 不应该在那里。)
  • “Game.h 包含在 main.cpp 中,这就是我使用 -c 标志的原因” - 这里的逻辑是什么? -c 标志应该在您想要生成目标文件时使用,或者将该目标文件与其他目标文件链接并生成可执行文件(这可能是您需要做的,因为您的文件夹中有多个翻译单元)或将其与其他目标文件捆绑到静态库中。
  • 请发布您的代码,因为那里显然存在错误。这与已经给出答案的原始问题无关(您使用带有 -c 标志的 g++ 创建可重定位的目标文件而不是可执行文件)。请在此处查看有关如何提供最少示例的指南:https://stackoverflow.com/help/mcve

标签: c++ c++14


【解决方案1】:

您应该在同一命令中编译所有文件,以便将它们链接到可执行文件中:

g++ -Wall -std=c++14 -o main main.cpp Field.cpp Game.cpp

【讨论】:

    【解决方案2】:

    您好,欢迎来到这里。

    你应该解释一下你是如何编译和链接你的程序的,因为我担心那里有问题。

    问题是您的文件不是可执行文件。正如文件实用程序所述,它是一个可重定位文件,而不是可执行文件。

    要详细了解可重定位文件和可执行文件之间的区别,您可以在此处查看此答案:What is the difference between executable and relocatable in elf format?

    编辑

    由于OP提供了更多细节,这里是解决方案。

    1) 删除 -c 标志。正如其他人所说,那是完全错误的;

    2) 在编译命令中添加所有 .cpp 文件:

    g++ -Wall -std=c++14 -o main main.cpp Field.cpp Game.cpp

    【讨论】:

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