Ubuntu 编译:C 中文件格式无法识别错误

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

当我在 macOS 上编译时它可以工作,但是在 Ubuntu 中却不能,而是收到此错误:

main.o: file not recognized: File format not recognised
collect2: error: ld returned 1 exit status
Makefile:8: recipe for target 'traffic' failed
make: *** [traffic] Error 1

我知道 main.o 已损坏,但即使在“make clean”之后,无论弹出什么错误。如何修复此问题,以免生成损坏的目标文件。 (按照老师的意愿使用ansi C89)

我的Makefile:

CC = gcc
CFLAGS = -Wall -ansi -pedantic -Werror
OBJ = main.o initialise.o cars.o movement.o newSleep.o random.o terminal.o
EXEC = traffic


$(EXEC) : $(OBJ)
    $(CC) $(CFLAGS) $(OBJ) -o $(EXEC)

main.o : main.c initialise.h cars.h movement.h newSleep.h random.h terminal.h macros.h 
    $(CC) $(CFLAGS) -c main.c

initialise.o : initialise.c initialise.h macros.h 
    $(CC) $(CFLAGS) -c initialise.c

cars.o : cars.c cars.h random.h macros.h 
    $(CC) $(CFLAGS) -c cars.c

movement.o : movement.c movement.h
    $(CC) $(CFLAGS) -c movement.c

newSleep.o : newSleep.c newSleep.h
    $(CC) $(CFLAGS) -c newSleep.c

random.o : random.c random.h 
    $(CC) $(CFLAGS) -c random.c

terminal.o : terminal.c terminal.h
    $(CC) $(CFLAGS) -c terminal.c
    
clean : 
    rm -f $(EXEC) $(OBJ)

输出

make clean; make

rm -f traffic main.o initialise.o cars.o movement.o newSleep.o random.o terminal.o
gcc -Wall -ansi -pedantic -Werror -c main.c
gcc -Wall -ansi -pedantic -Werror -c initialise.c
gcc -Wall -ansi -pedantic -Werror -c cars.c
gcc -Wall -ansi -pedantic -Werror -c movement.c
gcc -Wall -ansi -pedantic -Werror -c newSleep.c
gcc -Wall -ansi -pedantic -Werror -c random.c
gcc -Wall -ansi -pedantic -Werror -c terminal.c
gcc -Wall -ansi -pedantic -Werror main.o initialise.o cars.o movement.o newSleep.o random.o terminal.o -o traffic
main.o: file not recognized: File format not recognised
collect2: error: ld returned 1 exit status
Makefile:8: recipe for target 'traffic' failed
make: *** [traffic] Error 1

输出

file *.o

cars.o:       data
initialise.o: data
main.o:       data
movement.o:   data
newSleep.o:   data
random.o:     data
terminal.o:   data

输出

gcc -dumpmachine

x86_64-linux-gnu

我用的是M2芯片。我在 VMware Horizon Client 上运行 Ubuntu

输出

gcc -v

Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/7/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 7.5.0-3ubuntu1~18.04' --with-bugurl=file:///usr/share/doc/gcc-7/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++ --prefix=/usr --with-gcc-major-version-only --program-suffix=-7 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --enable-bootstrap --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --enable-default-pie --with-system-zlib --with-target-system-zlib --enable-objc-gc=auto --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 7.5.0 (Ubuntu 7.5.0-3ubuntu1~18.04) 

file /bin/sh
的输出我也很困惑

/bin/sh: symbolic link to dash

输出前几行

od -tx1 -c main.o

0000000  00  6d  61  69  6e  2e  63  00  6d  61  69  6e  00  5f  47  4c
         \0   m   a   i   n   .   c  \0   m   a   i   n  \0   _   G   L
0000020  4f  42  41  4c  5f  4f  46  46  53  45  54  5f  54  41  42  4c
          O   B   A   L   _   O   F   F   S   E   T   _   T   A   B   L
0000040  45  5f  00  70  75  74  73  00  61  74  6f  69  00  6d  61  6c
          E   _  \0   p   u   t   s  \0   a   t   o   i  \0   m   a   l
0000060  6c  6f  63  00  70  72  69  6e  74  66  00  69  6e  69  74  69
          l   o   c  \0   p   r   i   n   t   f  \0   i   n   i   t   i
0000100  61  6c  69  7a  65  42  6f  78  00  70  6c  61  63  65  43  61
          a   l   i   z   e   B   o   x  \0   p   l   a   c   e   C   a

find $(dirname $(dirname $(which gcc))) -name collect2
的输出也许这是因为我正在使用我的大学虚拟实验室?

/usr/lib/gcc/x86_64-linux-gnu/7/collect2
find: ‘/usr/lib/vmware/viewagent/DesktopDaemon’: Permission denied
find: ‘/usr/lib/vmware/viewagent/bin’: Permission denied
find: ‘/usr/lib/vmware/viewagent/jre’: Permission denied
find: ‘/usr/share/pam-configs.pbis’: Permission denied

输出

file /bin/dash

/bin/dash: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0, BuildID[sha1]=a783260e3a5fe0afdae77417eea7fbf8d645219e, stripped

输出

file $(which gcc)

/usr/bin/gcc: symbolic link to gcc-7

输出

/usr/bin/gcc*

/usr/bin/ld: error in /usr/bin/gcc-7(.eh_frame); no .eh_frame_hdr table will be created.
/usr/bin/ld: error in /usr/bin/gcc-ar(.eh_frame); no .eh_frame_hdr table will be created.
/usr/bin/ld: error in /usr/bin/gcc-ar-7(.eh_frame); no .eh_frame_hdr table will be created.
/usr/bin/ld: error in /usr/bin/gcc-nm(.eh_frame); no .eh_frame_hdr table will be created.
/usr/bin/ld: error in /usr/bin/gcc-nm-7(.eh_frame); no .eh_frame_hdr table will be created.
/usr/bin/ld: error in /usr/bin/gcc-ranlib(.eh_frame); no .eh_frame_hdr table will be created.
/usr/bin/ld: error in /usr/bin/gcc-ranlib-7(.eh_frame); no .eh_frame_hdr table will be created.
/usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status
c makefile
1个回答
0
投票

抱歉,我已经找到解决方案了。这个解决方案一点也不有趣。 事实证明,我大学的虚拟实验室内置了针对过去事件的恶意软件防护,因此除非代码位于桌面上,否则您无法编译或运行任何代码。

感谢您的帮助,这不是浪费,因为我学到了一些非常有趣的信息。这是一个非常可笑的问题。 我希望如果有人遇到类似的问题,评论的可能解决方案可以在将来帮助某人,即使它没有解决我的问题。

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