2

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!

Stuxen
  • 708
  • 7
  • 21
  • *"I then run it using `./main.o`"* - that's just object code, not an executable. When you said, *"So I compile it using this command:"* - do you know what that command *does* ? – WhozCraig Jan 23 '20 at 04:28

1 Answers1

4

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.

walnut
  • 21,629
  • 4
  • 23
  • 59
  • Thank you so much for clearing out a lot of things. It was really helpful. I have added the errors I get after the running the make file if you'd like to comment on that. I have never really done a c++ project before. This code is actually given by my professor as a starter code. And I have to do a project on top of this. So trying to setup the project now. – Stuxen Jan 23 '20 at 04:40
  • 1
    @DeepBodra There are no errors, only warnings. The build was sucessful and the executable `test.out` was built. You can run that. If you want to build `main` instead, you need to call `make main`. I fixed my answer. Whether you are supposed to built and execute `main` or `test.out`, I don't know. – walnut Jan 23 '20 at 04:44
  • 2
    @DeepBodra Based on the warning messages the compiler is giving, the code you got from your professor is bad and is not going to work with newer C++ versions. All of these warnings should have been fixed and using `fscanf` (a C library function) in C++ which has its own input/output library is also not a good sign. – walnut Jan 23 '20 at 04:45
  • Thank you so much. I'll try fixing the code instead. I love this community! By the way, is it possible to switch to a older version of the g++ compiler to get around this problem? – Stuxen Jan 23 '20 at 04:52
  • 1
    @DeepBodra You can always add the `-std=c++98`/`-std=c++03` flag to the compiler if it complains that the code uses something that was removed from the language. But that is obviously not a nice solution, given that it makes it impossible to use newer language features. – walnut Jan 23 '20 at 04:55