C Makefile提供具有多个定义的链接器错误

时间:2014-11-10 07:34:35

标签: c gcc makefile

我有以下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

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

1 个答案:

答案 0 :(得分:-2)

好像你多次编译你的文件。试试这个:

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将为您完成工作。