title: bash: ./main.o cannot execute binary file Exec format error
body: <p>I have set up a ubuntu virtual machine on windows using virtual box.</p>
  
  <p>I am trying to run a C++ project. The make file looks like this</p>
  
  <pre><code>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
  </code></pre>
  
  <p>After running the make command I get a bunch of warnings like</p>
  
  <pre><code>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, &amp;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, &amp;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, &amp;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
  </code></pre>
  
  <p>All files compile generating respective <code>.o</code> files except the <code>main.cc</code>. So I compile it using this command</p>
  
  <pre><code>g++ -O2 -Wno-deprecated -g -c -o main.o main.cc
  </code></pre>
  
  <p>Now the <code>main.o</code> file compiles and main.o is generated</p>
  
  <p>I then run it using <code>./main.o</code> and get <strong>Permission denied</strong></p>
  
  <p>So I get rid of this error by <code>chmod 755 ./*</code>
  Now running the <code>main.o</code> gives this error</p>
  
  <pre><code>bash: ./main.o cannot execute binary file Exec format error
  </code></pre>
  
  <p>Through my research I found out that this error is because of the mismatch in arhitecture and file type.</p>
  
  <p><code>file main.o</code> outputs <strong>x86-64</strong> and <code>uname -m</code> outputs <strong>x86_64</strong></p>
  
  <p>I cannot figure out the problem.
  Any help would be greatly appreciated! Thank you!</p>