gpt4 book ai didi

C Makefile 给出具有多个定义的链接器错误

转载 作者:行者123 更新时间:2023-11-30 18:15:28 25 4
gpt4 key购买 nike

我有以下 makefile:

CC = gcc
CFLAGS = -g3 -std=c99 -pedantic -Wall
HWK = /c/cs323/Hwk2/
objects = code1.o lzw.o

lzw: encode
${CC} ${CFLAGS} -o lzw encode

encode: decode
${CC} ${CFLAGS} -o encode decode

decode: lzw.o
${CC} ${CFLAGS} -o decode lzw.o


lzw.o: lzw.c ${HWK}code1.c
${CC} ${CFLAGS} -c lzw.c ${HWK}code1.c

当我尝试运行“make lzw”时,出现以下错误:

decode: In function `_fini':
(.fini+0x0): multiple definition of `_fini'
/usr/lib/gcc/x86_64-redhat-linux/4.8.2/../../../../lib64/crti.o:(.fini+0x0): first defined here
decode: In function `data_start':
(.data+0x0): multiple definition of `__data_start'
/usr/lib/gcc/x86_64-redhat-linux/4.8.2/../../../../lib64/crt1.o:(.data+0x0): first defined here
decode:(.rodata+0x8): multiple definition of `__dso_handle'
/usr/lib/gcc/x86_64-redhat-linux/4.8.2/crtbegin.o:(.rodata+0x0): first defined here
decode:(.rodata+0x0): multiple definition of `_IO_stdin_used'
/usr/lib/gcc/x86_64-redhat-linux/4.8.2/../../../../lib64/crt1.o:(.rodata.cst4+0x0): first defined here
decode: In function `_start':
(.text+0x0): multiple definition of `_start'
/usr/lib/gcc/x86_64-redhat-linux/4.8.2/../../../../lib64/crt1.o:(.text+0x0): first defined here
decode: In function `_init':
(.init+0x0): multiple definition of `_init'
/usr/lib/gcc/x86_64-redhat-linux/4.8.2/../../../../lib64/crti.o:(.init+0x0): first defined here
/usr/lib/gcc/x86_64-redhat-linux/4.8.2/crtend.o:(.tm_clone_table+0x0): multiple definition of `__TMC_END__'
decode:(.data+0x8): first defined here
/usr/bin/ld: error in decode(.eh_frame); no .eh_frame_hdr table will be created.
collect2: error: ld returned 1 exit status

我在网上发现了一些有类似错误的地方,但它们似乎都不适用(例如,我不必在我的程序中插入电源)。怎么了?

最佳答案

似乎您多次编译文件。试试这个:

CC = gcc
CFLAGS = -g3 -std=c99 -pedantic -Wall
HWK = /c/cs323/Hwk2/
objects = code1.o lzw.o

lzw: encode.o

encode.o: decode.o
${CC} ${CFLAGS} -o encode.c

decode.o: lzw.o
${CC} ${CFLAGS} -o decode.c


lzw.o: lzw.c ${HWK}code1.c
${CC} ${CFLAGS} -c lzw.c ${HWK}code1.c

请注意,您不需要(在本例中也不应该!)在同一命令行中构建文件的依赖项。用冒号标记依赖关系就足够了,make 将为您完成这项工作。

关于C Makefile 给出具有多个定义的链接器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26838864/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com