Search code examples
c++virtualboxubuntu-18.04

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


I have set up a ubuntu virtual machine on windows using virtual box.

I am trying to run a C++ project. The make file looks like this

CC = g++ -O2 -Wno-deprecated 

tag = -i

ifdef linux
tag = -n
endif

test.out: Record.o Comparison.o ComparisonEngine.o Schema.o File.o DBFile.o y.tab.o lex.yy.o test.o
    $(CC) -o test.out Record.o Comparison.o ComparisonEngine.o Schema.o File.o DBFile.o y.tab.o lex.yy.o test.o -lfl

main: Record.o Comparison.o ComparisonEngine.o Schema.o File.o y.tab.o lex.yy.o main.o
    $(CC) -o main Record.o Comparison.o ComparisonEngine.o Schema.o File.o y.tab.o lex.yy.o main.o -lfl

test.o: test.cc
    $(CC) -g -c test.cc

main.o: main.cc
    $(CC) -g -c main.cc

Comparison.o: Comparison.cc
    $(CC) -g -c Comparison.cc

ComparisonEngine.o: ComparisonEngine.cc
    $(CC) -g -c ComparisonEngine.cc

DBFile.o: DBFile.cc
    $(CC) -g -c DBFile.cc

File.o: File.cc
    $(CC) -g -c File.cc

Record.o: Record.cc
    $(CC) -g -c Record.cc

Schema.o: Schema.cc
    $(CC) -g -c Schema.cc

y.tab.o: Parser.y
    yacc -d Parser.y
    sed $(tag) y.tab.c -e "s/  __attribute__ ((__unused__))$$/# ifndef __cplusplus\n  __attribute__ ((__unused__));\n# endif/" 
    g++ -c y.tab.c

lex.yy.o: Lexer.l
    lex  Lexer.l
    gcc  -c lex.yy.c

clean: 
    rm -f *.o
    rm -f *.out
    rm -f y.tab.c
    rm -f lex.yy.c
    rm -f y.tab.h

After running the make command I get a bunch of warnings like

g++ -O2 -Wno-deprecated  -g -c Record.cc
g++ -O2 -Wno-deprecated  -g -c Comparison.cc
g++ -O2 -Wno-deprecated  -g -c ComparisonEngine.cc
g++ -O2 -Wno-deprecated  -g -c Schema.cc
Schema.cc: In constructor ‘Schema::Schema(const char*, const char*)’:
Schema.cc:46:9: warning: ignoring return value of ‘int fscanf(FILE*, const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
  fscanf (foo, "%s", space);
  ~~~~~~~^~~~~~~~~~~~~~~~~~
Schema.cc:58:10: warning: ignoring return value of ‘int fscanf(FILE*, const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
   fscanf (foo, "%s", space);
   ~~~~~~~^~~~~~~~~~~~~~~~~~
Schema.cc:84:9: warning: ignoring return value of ‘int fscanf(FILE*, const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
  fscanf (foo, "%s", space);
  ~~~~~~~^~~~~~~~~~~~~~~~~~
Schema.cc:91:10: warning: ignoring return value of ‘int fscanf(FILE*, const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
   fscanf (foo, "%s", space);
   ~~~~~~~^~~~~~~~~~~~~~~~~~
Schema.cc:95:11: warning: ignoring return value of ‘int fscanf(FILE*, const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
    fscanf (foo, "%s", space);
    ~~~~~~~^~~~~~~~~~~~~~~~~~
Schema.cc:106:10: warning: ignoring return value of ‘int fscanf(FILE*, const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
   fscanf (foo, "%s", space);
   ~~~~~~~^~~~~~~~~~~~~~~~~~
Schema.cc:114:10: warning: ignoring return value of ‘int fscanf(FILE*, const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
   fscanf (foo, "%s", space);
   ~~~~~~~^~~~~~~~~~~~~~~~~~
Schema.cc:118:10: warning: ignoring return value of ‘int fscanf(FILE*, const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
   fscanf (foo, "%s", space);
   ~~~~~~~^~~~~~~~~~~~~~~~~~
g++ -O2 -Wno-deprecated  -g -c File.cc
File.cc: In member function ‘void File::GetPage(Page*, off_t)’:
File.cc:186:7: warning: ignoring return value of ‘ssize_t read(int, void*, size_t)’, declared with attribute warn_unused_result [-Wunused-result]
  read (myFilDes, bits, PAGE_SIZE);
  ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
File.cc: In member function ‘void File::AddPage(Page*, off_t)’:
File.cc:206:10: warning: ignoring return value of ‘ssize_t write(int, const void*, size_t)’, declared with attribute warn_unused_result [-Wunused-result]
    write (myFilDes, &foo, sizeof (int));
    ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
File.cc:223:8: warning: ignoring return value of ‘ssize_t write(int, const void*, size_t)’, declared with attribute warn_unused_result [-Wunused-result]
  write (myFilDes, bits, PAGE_SIZE);
  ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
File.cc: In member function ‘void File::Open(int, char*)’:
File.cc:258:8: warning: ignoring return value of ‘ssize_t read(int, void*, size_t)’, declared with attribute warn_unused_result [-Wunused-result]
   read (myFilDes, &curLength, sizeof (off_t));
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
File.cc: In member function ‘int File::Close()’:
File.cc:276:8: warning: ignoring return value of ‘ssize_t write(int, const void*, size_t)’, declared with attribute warn_unused_result [-Wunused-result]
  write (myFilDes, &curLength, sizeof (off_t));
  ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
g++ -O2 -Wno-deprecated  -g -c DBFile.cc
yacc -d Parser.y
sed -i y.tab.c -e "s/  __attribute__ ((__unused__))$/# ifndef __cplusplus\n  __attribute__ ((__unused__));\n# endif/" 
g++ -c y.tab.c
y.tab.c: In function ‘int yyparse()’:
y.tab.c:1427:35: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]
       yyerror (YY_("syntax error"));
                                   ^
y.tab.c:1571:35: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]
   yyerror (YY_("memory exhausted"));
                                   ^
lex  Lexer.l
gcc  -c lex.yy.c
g++ -O2 -Wno-deprecated  -g -c test.cc
g++ -O2 -Wno-deprecated  -o test.out Record.o Comparison.o ComparisonEngine.o Schema.o File.o DBFile.o y.tab.o lex.yy.o test.o -lfl

All files compile generating respective .o files except the main.cc. So I compile it using this command

g++ -O2 -Wno-deprecated -g -c -o main.o main.cc

Now the main.o file compiles and main.o is generated

I then run it using ./main.o and get Permission denied

So I get rid of this error by chmod 755 ./* Now running the main.o gives this error

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

Through my research I found out that this error is because of the mismatch in arhitecture and file type.

file main.o outputs x86-64 and uname -m outputs x86_64

I cannot figure out the problem. Any help would be greatly appreciated! Thank you!


Solution

  • You are trying to execute an object file. Object files like main.o are not executable. They only contain part of the code required to form an exectuable.

    You need to run main (or maybe test.out), which is the actual executable.

    The permission warnings were telling you the same thing (that main.o is not executable), but you overwrote that with chmod.

    main should be created by make assuming there are no compilation/linking errors if you use make main or test.out if you use make or make test.out. The linker invocation is given in the main:/test.out: target of the Makefile. If make generates compiler or linker errors, then repeating the same commands manually is not going to fix it. Fix the code or Makefile instead.

    Also note that the warnings/errors are there for a reason. Since C++11, you are not allowed to cast string literals to char*, only to const char*. This was deprecated already in C++03. Circumventing that rule is only going to cause you trouble in the future. Fix your code instead of adding -Wno-deprecated.