2013-11-25 139 views
2

找到解决方案。请看下图:使文件无法正常工作?

我试图让我的makefile编译三个C程序转变成一个可执行文件,但我得到以下错误:

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

是的,我使用的是使清洁我每次都使用它。 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 

我想不通这是为什么....

我使用使清洁每次。

试图编译:

[[email protected]] (34)$ make clean 
rm -f *.o 
[[email protected]] (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 
[[email protected]] (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 

SOLUTION

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 

回答

6

请阅读介绍到makefile文件。这看起来像我的作业。

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

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

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

你的Makefile可以一样容易写,因为这(趁着化妆内置的规则):

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标志以及编译行。

+0

我每次都使用make clean。 试图编译: – katiea

+0

查看上面的更改 – katiea

+1

哦。你使用'-m32'标志将目标文件编译为32位,但是你并没有在链接行上加上'-m32',所以它试图把程序作为一个64位程序连接起来,从而将32位'.o '文件格式错误。 – MadScientist