1

I know this Question has been asked before, but none of the answers helped in my case.

I have Ubuntu 18.04 LTS installed and when I try to run my C++ program, I get this Error:

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

I know this error occurs, when a 32-bit program is tried to be run on a 64-bit machine. But using file main in the terminal outputs

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

and using uname -a outputs

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

What do I overlook?

Thanks in advance

EDIT:

I compile the program using

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

This is how the folder with the files looks like:

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

The Game.h is included in the main.cpp, thats why i use the -c flag

Trying to compile the file without the -c flag gives me this terminal output:

    /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

EDIT:

After trying to compile it withe the other files at the ame time it seems like we're getting closer to a solution, but there are still some errors:

Command:

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

Output:

/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
  • Did you compile that binary on the same box? If not does the box you compiled on have the same processor manufacturer as the one you're running on? Only thing I can think of is it's using a specific Intel/AMD instruction it can't understand. – Nicholas Smith Mar 20 '19 at 11:40
  • 3
    Looks like you built an object file instead of an exectuable. Did you perhaps compile it with something like `g++ -c -o main main.cpp`? (If you did, the `-c` should not be there.) – molbdnilo Mar 20 '19 at 11:42
  • 1
    *"Game.h is included in the main.cpp, thats why i use the -c flag"* - what is the logic here? `-c` flag should be used when you want to produce object file, either to link that object file with other object files and make an executable (that is what you probably need to do since you have several translation units in your folder) or to bundle it with other object files into static library. – user7860670 Mar 20 '19 at 11:57
  • Please post your code as there's obviously an error there. This has nothing to do with the original question, for which the answer was already given (you were using g++ with the -c flag creating a relocatable object file rather than an executable). Please have a look at the guide on how to provide minimal examples here: [https://stackoverflow.com/help/mcve](https://stackoverflow.com/help/mcve) – Sergio Monteleone Mar 20 '19 at 15:23

2 Answers2

1

Hi and welcome here on SO.

You should explain how you compile and link your program, as I'm afraid there's something wrong there.

The problem is your file is not an executable. As the file utility states, it is a relocatable file, not an executable.

To learn more about the difference between relocatables and executables you can have a look at this answer here on SO: What is the difference between executable and relocatable in elf format?

EDIT

Since the OP has provided more details, here is the solution.

1) Drop the -c flag. As others have stated, that is plain wrong there;

2) Add all the .cpp files in your compile command:

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

Sergio Monteleone
  • 2,776
  • 9
  • 21
1

You should compile all files in the same command, so that they can be linked into an executable:

g++ -Wall -std=c++14 -o main main.cpp Field.cpp Game.cpp
VLL
  • 9,634
  • 1
  • 29
  • 54