让文件无效?

时间:2013-11-25 18:53:33

标签: gcc compiler-construction makefile executable

找到解决方案。见下文:

我正在尝试让我的makefile将三个c程序编译成一个可执行文件,但是我收到以下错误:

cachesim.o: could not read symbols: File in wrong format

是的,我每次使用时都会使用make clean。 make文件如下

CC     = gcc
CFLAGS = -Wall -m32 -O -g

all:  cachesim cache trace_file_parser 
gcc -o cachesim cachesim.o cache.o trace_file_parser.o

cachesim:       cachesim.c
        $(CC) -c -o cachesim.o cachesim.c $(CFLAGS)

cache:          cache.c
        $(CC) -c -o cache.o cache.c $(CFLAGS)

trace_file_parser:  trace_file_parser.c
        $(CC) -c -o trace_file_parser.o trace_file_parser.c $(CFLAGS)

clean:
rm -f *.o

我无法弄清楚这是为什么......

我每次都在使用make clean。

尝试编译:

[katiea@mumble-15] (34)$ make clean
rm -f *.o
[katiea@mumble-15] (35)$ ls
cache.c   cache.h     cachesim.c~      gcc_trace  Makefile~     trace_file_parser.c
cache.c~  cachesim.c  cache_structs.h  Makefile   strgen_trace  trace_file_parser.h
[katiea@mumble-15] (36)$ make
gcc -c -o cachesim.o cachesim.c -Wall -m32 -O -g
gcc -c -o cache.o cache.c -Wall -m32 -O -g
gcc -c -o trace_file_parser.o trace_file_parser.c -Wall -m32 -O -g
gcc -o cachesim cachesim.o cache.o trace_file_parser.o
cachesim.o: could not read symbols: File in wrong format
collect2: ld returned 1 exit status
make: *** [all] Error 1

CC     = gcc

CFLAGS = -Wall -m32 -O -g

all:  cachesim.c cache.c trace_file_parser.c
$(CC) -o cachesim cachesim.c cache.c trace_file_parser.c $(CFLAGS)

cachesim:       cachesim.c
        $(CC) -c -o cachesim.o cachesim.c $(CFLAGS)

cache:          cache.c
        $(CC) -c -o cache.o cache.c $(CFLAGS)

trace_file_parser:  trace_file_parser.c
        $(CC) -c -o trace_file_parser.o trace_file_parser.c $(CFLAGS)

clean:
rm -f *.o

1 个答案:

答案 0 :(得分:6)

请阅读makefile的介绍。这看起来像是我的家庭作业。

makefile的最基本原则之一是目标应该是您正在构建的实际文件。这些规则都是假的:

cachesim:       cachesim.c
         $(CC) -c -o cachesim.o cachesim.c $(CFLAGS)

(等),因为目标是cachesim,但配方(命令行)构建文件cachesim.o

你的makefile可以像这样轻松编写(利用make的内置规则):

CC      = gcc
CFLAGS  = -Wall -m32 -O -g
LDFLAGS = -m32 -O -g

cachesim: cachesim.o cache.o trace_file_parser.o

clean:
        rm -f *.o

这就是你所需要的一切。

至于你的错误,在我看来,文件cachesim.o必须采用某种奇怪的格式,也许是在你正确设置makefile之前从后面。

如果再次运行make clean然后make,是否会出现同样的错误?如果是这样,请显示编译和链接行。

ETA:如果要创建32位程序,请使用链接行上的-m32标志以及编译行。