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

时间:2019-03-20 11:30:54

标签: c++ c++14

我知道之前曾有人问过这个问题,但对我而言,答案都没有帮助。

我已安装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

2 个答案:

答案 0 :(得分:1)

嗨,欢迎来到这里。

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

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

要了解有关可重定位的文件和可执行文件之间的区别的更多信息,可以在SO上查看以下答案: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

答案 1 :(得分:1)

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

g++ -Wall -std=c++14 -o main main.cpp Field.cpp Game.cpp
相关问题