bash:./ main:无法执行二进制文件:exec格式错误[关闭]

问题描述 投票:1回答:2

我之前已经问过这个问题,但在我的案例中没有一个答案有帮助。

我安装了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
c++ c++14
2个回答
1
投票

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

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

1
投票

嗨,欢迎来到SO。

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

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

要了解有关可重定位项和可执行文件之间区别的更多信息,可以在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

© www.soinside.com 2019 - 2024. All rights reserved.