【问题标题】:Make file not working?使文件不起作用?
【发布时间】:2013-11-25 18:53:33
【问题描述】:

找到解决方案。见下文:

我试图让我的 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

【问题讨论】:

    标签: gcc compiler-construction makefile executable


    【解决方案1】:

    请阅读 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 标志。

    【讨论】:

    • 我每次都在使用 make clean。尝试编译:
    • 哦。您正在将目标文件编译为 32 位,带有 -m32 标志,但您没有将 -m32 放在链接行上,因此它试图将程序链接为 64 位程序,因此 32 位 .o 文件具有格式错误。
    • 就像这样:所有:cachesim.c cache.c trace_file_parser.c $(CC) -o cachesim cachesim.c cache.c trace_file_parser.c $(CFLAGS)
    • 我已经更新了我的答案。由于我前面所述的原因,您原来的 makefile 不正确。
    • 我理解的差距是 -m32 标志。在我明白这一点之后,我能够完成一个工作 MakeFile。我过去使用过 Makefile,但直到今天才收到我上面提到的错误。谢谢你帮我解析意思。
    猜你喜欢
    • 2013-11-24
    • 2013-04-24
    • 1970-01-01
    • 2016-12-20
    • 2011-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多