bash: ./main: cannot execute binary file: Exec format error [closed] - c++

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
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

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

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

Related

"undefined reference to *" Error while compiling file that uses the Open Dynamics Engine v0.13.1

I am a beginner to organizing C++ projects and I have been trying to start an Open Dynamics Engine project for a week now but I keep getting several errors. First, what I have done:
Installed ode-0.13.1 with automake as directed in the manual.
Created a file test.cpp with the code from the bouncing ball HOWTO.
Wrote a makefile to compile.
But when I run my makefile, I get the following errors:
gcc -Wall -g -DdDOUBLE -o test test.o -L/home/username/ode-0.13.1/ode/src/libode.la -L/home/username/ode-0.13.1/drawstuff/src/libdrawstuff.la -L/usr/X11R6/lib -L/usr/lib -L/usr/local/lib -I. -I/home/username/ode-0.13.1/include/ -I/usr/X11R6/include -I/usr/include -lm -lode -lGL -lGLU
/usr/local/lib/libode.a(collision_trimesh_box.o): In function dCalcVectorLength3':
/home/username/ode-0.13.1/ode/src/../../include/ode/odemath.h:148: undefined reference tosqrt'
/home/username/ode-0.13.1/ode/src/../../include/ode/odemath.h:148: undefined reference to sqrt'
/home/username/ode-0.13.1/ode/src/../../include/ode/odemath.h:148: undefined reference tosqrt'
/usr/local/lib/libode.a(util.o): In function dxStepBody(dxBody*, double)':
/home/username/ode-0.13.1/ode/src/util.cpp:618: undefined reference tosincos'
/home/username/ode-0.13.1/ode/src/util.cpp:630: undefined reference to sincos'
/home/username/ode-0.13.1/ode/src/util.cpp:628: undefined reference tosqrt'
/home/username/ode-0.13.1/ode/src/util.cpp:590: undefined reference to `sqrt'
... (and this keeps going for pages)
/usr/local/lib/libode.a(OPC_AABBTree.o): In function Opcode::AABBTree::Build(Opcode::AABBTreeBuilder*)':
/home/username/ode-0.13.1/OPCODE/OPC_AABBTree.cpp:413: undefined reference tooperator new[](unsigned long)'
/home/username/ode-0.13.1/OPCODE/OPC_AABBTree.cpp:427: undefined reference to operator new[](unsigned long)'
/usr/local/lib/libode.a(OPC_AABBTree.o): In functionOpcode::AABBTreeNode::~AABBTreeNode()':
/home/username/ode-0.13.1/OPCODE/OPC_AABBTree.cpp:83: undefined reference to operator delete[](void*, unsigned long)'
/usr/local/lib/libode.a(OPC_Collider.o): In functionOpcode::Collider::~Collider()':
/home/username/ode-0.13.1/OPCODE/OPC_Collider.cpp:54: undefined reference to operator delete(void*, unsigned long)'
/usr/local/lib/libode.a(OPC_Collider.o):(.data.rel.ro._ZTIN6Opcode8ColliderE[_ZTIN6Opcode8ColliderE]+0x0): undefined reference tovtable for __cxxabiv1::__class_type_info'
/usr/local/lib/libode.a(OPC_Collider.o):(.data.rel.ro._ZTVN6Opcode8ColliderE[_ZTVN6Opcode8ColliderE]+0x20): undefined reference to `__cxa_pure_virtual'
collect2: error: ld returned 1 exit status
makefile:14: recipe for target 'test' failed
make: *** [test] Error 1
These errors seem like the library has not been set up correctly, but I'm pretty sure I've properly installed and my -L's are set to the correct paths.
Note 1: the demos included with the ode library work.
Note 2: I've tried (and failed) to install other versions of ode previously. I did my best with make uninstall and make clean to clean these failed installs, but I think they may be affecting new installs because when I run sudo make install for the first time there is an awful lot of Nothing to do here... messages.

Compile the first mathgl sample error

a test file named ml.cc, I have already installed the mathgl headers to /usr/local/include and libmgl.a to /usr/local/lib
#include <mgl2/mgl.h>
int main()
{
mglGraph gr;
gr.FPlot("sin(pi*x)");
gr.WriteFrame("test.png");
return 0;
}
"g++ -c ml.cc" can work,but "g++ ml.cc" does not work,the error is
/tmp/ccPzPcZt.o: In function `mglGraph::mglGraph(int, int, int)':
ml.cc:(.text._ZN8mglGraphC2Eiii[_ZN8mglGraphC5Eiii]+0x3b): undefined reference to `mgl_create_graph_gl'
ml.cc:(.text._ZN8mglGraphC2Eiii[_ZN8mglGraphC5Eiii]+0x54): undefined reference to `mgl_create_graph'
/tmp/ccPzPcZt.o: In function `mglGraph::~mglGraph()':
ml.cc:(.text._ZN8mglGraphD2Ev[_ZN8mglGraphD5Ev]+0x28): undefined reference to `mgl_use_graph'
ml.cc:(.text._ZN8mglGraphD2Ev[_ZN8mglGraphD5Ev]+0x42): undefined reference to `mgl_delete_graph'
/tmp/ccPzPcZt.o: In function `mglGraph::SetFontSize(double)':
ml.cc:(.text._ZN8mglGraph11SetFontSizeEd[_ZN8mglGraph11SetFontSizeEd]+0x2a): undefined reference to `mgl_set_font_size'
/tmp/ccPzPcZt.o: In function `mglGraph::WriteFrame(char const*, char const*)':
ml.cc:(.text._ZN8mglGraph10WriteFrameEPKcS1_[_ZN8mglGraph10WriteFrameEPKcS1_]+0x2b): undefined reference to `mgl_write_frame'
/tmp/ccPzPcZt.o: In function `mglGraph::FPlot(char const*, char const*, char const*)':
ml.cc:(.text._ZN8mglGraph5FPlotEPKcS1_S1_[_ZN8mglGraph5FPlotEPKcS1_S1_]+0x30): undefined reference to `mgl_fplot'
collect2: error: ld returned 1 exit status
"g++ -L /usr/local/lib/ -l mgl ml.o" is the same error
I encoutered exactly the same problem and managed to solve it.
If you did the installation properly as you said. Then you simply need to add -lmgl AT THE END of the line! Like this:
g++ ml.o -lmgl
As luke already had mentioned, you have a linking error, thus compiling is not affected. Here is an explanation for this behaviour:
undefined reference to symbol even when nm indicates that this symbol is present in the shared library
Hope this solved your problem.
Peter
You have a linking error. To solve this you need to link against mgl. It looks like you are trying to, but its not working.
First, I would drop the space between the l and the mgl. so
g++ -L /usr/local/lib/ -lmgl ml.o
If that doesn't work, check to see if there are any other libraries you need to link against. See if g++ is throwing errors that it cannot find mgl.
Hope that helps.

How can do I link libevent that has been configured to have a different prefix?

I am trying to link libevent using g++ but am having trouble since I set libevent's install directory with the --prefix flag when configuring. To install libevent I downloaded the latest source, extracted it and ran the following commands in the directory
./configure --prefix=/home/tom/local --disable-shared && make
make install
After running these commands libevent successfully installs to the /home/tom/local folder. Now to test that I can use libevent I have downloaded the sample rot13 server with libevent that can be found towards the bottom of: http://www.wangafu.net/~nickm/libevent-book/01_intro.html
To compile I run the following command:
g++ -I=/home/tom/local/include rot13server.cpp -L/home/tom/local/lib
But I get the following compilation errors:
/tmp/cctwJY4k.o: In function `alloc_fd_state(event_base*, int)':
libevent.cc:(.text+0x9b): undefined reference to `event_new'
libevent.cc:(.text+0xec): undefined reference to `event_new'
libevent.cc:(.text+0x11a): undefined reference to `event_free'
/tmp/cctwJY4k.o: In function `free_fd_state(fd_state*)':
libevent.cc:(.text+0x1b6): undefined reference to `event_free'
libevent.cc:(.text+0x1c9): undefined reference to `event_free'
/tmp/cctwJY4k.o: In function `do_read(int, short, void*)':
libevent.cc:(.text+0x310): undefined reference to `event_add'
/tmp/cctwJY4k.o: In function `do_write(int, short, void*)':
libevent.cc:(.text+0x4da): undefined reference to `event_del'
/tmp/cctwJY4k.o: In function `do_accept(int, short, void*)':
libevent.cc:(.text+0x564): undefined reference to `evutil_make_socket_nonblocking'
libevent.cc:(.text+0x5da): undefined reference to `event_add'
/tmp/cctwJY4k.o: In function `run()':
libevent.cc:(.text+0x5f3): undefined reference to `event_base_new'
libevent.cc:(.text+0x63f): undefined reference to `evutil_make_socket_nonblocking'
libevent.cc:(.text+0x6d2): undefined reference to `event_new'
libevent.cc:(.text+0x6e7): undefined reference to `event_add'
libevent.cc:(.text+0x6f3): undefined reference to `event_base_dispatch'
collect2: error: ld returned 1 exit status
It seems like the compiler is finding the include files but not the object files.
I also tried configuring libevent without the --disable-shared option and then exported /home/tom/local/lib to the LD_LIBRARY_PATH but I still get the same error with the compile command:
g++ -I=/home/tom/local/include rot13server.cpp
Can anyone tell me what I am doing wrong?
Thanks
Can anyone tell me what I am doing wrong?
Just about everything.
The -I=/home/tom/local/include tells GCC to look in =/home/tom/local/include directory, which is unlikely to exist.
You want:
g++ -I/home/tom/local/include rot13server.cpp -c
g++ -L/home/tom/local/lib rot13server.o -levent -o rot13server

how to compile and run this program, pthread issue?

I would like to compile the following program that makes alignment of fasta sequences, the program is available in:
http://compbio.cs.princeton.edu/mems/
I have downloaded a zip file called sparseMEM and downloaded the g++ in Ubuntu Linux, in the directory created there is a Makefile archive, so I tried to compile with make. The problem is that I got the following error:
g++ -lpthread -O3 -DSIXTYFOURBITS mummer.o qsufsort.o sparseSA.o fasta.o -o mummer
mummer.o: In function `main':
mummer.cpp:(.text.startup+0x480): undefined reference to `pthread_create'
mummer.cpp:(.text.startup+0x4a8): undefined reference to `pthread_join'
sparseSA.o: In function `sparseSA::MEM(std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, std::vector<match_t, std::allocator<match_t> >&, int, bool, int)':
sparseSA.cpp:(.text+0x3bbc): undefined reference to `pthread_create'
sparseSA.cpp:(.text+0x3be0): undefined reference to `pthread_join'
collect2: ld returned 1 exit status
make: *** [mummer] Error 1
how I can make it run?
also if somebody knows how to test this program it would be great
thanks
For Linux you need -pthread instead of -lpthread.

gnuplot-iostream not linking to boost

Hi I'm trying to use gnuplot-iostream, at the minute I'm just trying to get the code here working. When I try to combile I get errors in the linker saying:
In function `stream<int, boost::iostreams::file_descriptor_flags>':
/usr/include/boost/iostreams/stream.hpp:130: undefined reference to `boost::iostreams::file_descriptor_sink::file_descriptor_sink(int, boost::iostreams::file_descriptor_flags)'
In function `boost::iostreams::file_descriptor_sink boost::iostreams::detail::wrap<boost::iostreams::file_descriptor_sink>(boost::iostreams::file_descriptor_sink const&, boost::disable_if<boost::iostreams::is_std_io<boost::iostreams::file_descriptor_sink>, void>::type*)':
/usr/include/boost/iostreams/detail/wrap_unwrap.hpp:53: undefined reference to `boost::iostreams::file_descriptor_sink::file_descriptor_sink(boost::iostreams::file_descriptor_sink const&)'
In function `concept_adapter':
/usr/include/boost/iostreams/detail/adapter/concept_adapter.hpp:67: undefined reference to `boost::iostreams::file_descriptor_sink::file_descriptor_sink(boost::iostreams::file_descriptor_sink const&)'
/usr/include/boost/iostreams/detail/adapter/concept_adapter.hpp:38: undefined reference to `boost::iostreams::file_descriptor_sink::file_descriptor_sink(boost::iostreams::file_descriptor_sink const&)'
In function `long boost::iostreams::detail::write_device_impl<boost::iostreams::output>::write<boost::iostreams::file_descriptor_sink>(boost::iostreams::file_descriptor_sink&, boost::iostreams::char_type_of<boost::iostreams::file_descriptor_sink>::type const*, long)':
/usr/include/boost/iostreams/write.hpp:121: undefined reference to `boost::iostreams::file_descriptor::write(char const*, long)'
In function `void boost::iostreams::detail::close_impl<boost::iostreams::closable_tag>::close<boost::iostreams::file_descriptor_sink>(boost::iostreams::file_descriptor_sink&, std::_Ios_Openmode)':
/usr/include/boost/iostreams/close.hpp:224: undefined reference to `boost::iostreams::file_descriptor::close()'
In function `std::fpos<__mbstate_t> boost::iostreams::detail::seek_device_impl<boost::iostreams::any_tag>::seek<boost::iostreams::file_descriptor_sink>(boost::iostreams::file_descriptor_sink&, long, std::_Ios_Seekdir, std::_Ios_Openmode)':
/usr/include/boost/iostreams/seek.hpp:137: undefined reference to `boost::iostreams::file_descriptor::seek(long, std::_Ios_Seekdir)'
collect2: ld returned 1 exit status
Note I have boost installed and have previously compiled programs that use iostream.
Any help massively appreciated. Thanks
Ruling out the obvious... are you compiling with g++ -lboost_iostreams? If you can share some information about how the compiler and linker are being invoked it will make it easier to answer your question.
Run into exact problem when tried to use g++ with gnuplot-iostream library.
g++ prog.cpp -L/usr/lib -lboost_filesystem -lboost_system -lboost_iostreams
Resolved the issue for me - the code were compiled successfully with no errors.