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

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

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

我知道这个问题以前已经问过,但是在我的情况下,这些答案都没有帮助。

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

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

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

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-15533082433.png.html

游戏。

尝试在没有-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

编辑:

在AME时尝试使用其他文件对其进行编译后,似乎我们越来越接近解决方案,但是仍然存在一些错误:

命令:

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

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

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

嗨,欢迎在此处。

您应该解释您如何编译和链接程序,因为恐怕那里有问题。

问题是您的文件是不是可执行文件。如文件实用程序所述,它是 recocatable 文件,而不是可执行文件。

要了解重新定位和可执行文件之间的差异的更多信息,您可以在此处查看此答案:以精灵格式可执行文件和可重新定位之间有什么区别?

编辑

由于OP提供了更多详细信息,因此是解决方案。

1)丢弃-c标志。正如其他人所说的那样,那是很明显的;

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

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