bash:./main.o无法执行二进制文件Exec格式错误

问题描述 投票:0回答:1

我已经使用虚拟框在Windows上设置了ubuntu虚拟机。

我正在尝试运行C ++项目。生成文件如下所示:>

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

运行make命令后,我收到一堆警告,例如

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

.o以外,所有文件都会编译生成相应的main.cc文件。所以我用这个命令编译]

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

现在main.o文件将编译并生成main.o

然后我使用./main.o运行它并获得权限被拒绝

所以我通过chmod 755 ./*消除了此错误现在运行main.o会出现此错误

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

通过研究,我发现此错误是由于架构和文件类型不匹配造成的。

[file main.o输出x86-64

uname -m输出x86_64] >>

我无法找出问题所在。任何帮助将不胜感激!谢谢!

我已使用虚拟盒在Windows上设置了ubuntu虚拟机。我正在尝试运行C ++项目。生成文件如下所示CC = g ++ -O2 -Wno-deprecated tag = -i ifdef linux tag = -n ...

c++ virtualbox ubuntu-18.04
1个回答
2
投票

您正在尝试执行目标文件。诸如main.o之类的目标文件不可执行。它们仅包含组成可执行文件所需的部分代码。

您需要运行main(或者可能是test.out),它是实际的可执行文件。

© www.soinside.com 2019 - 2024. All rights reserved.