Makefile多重定义错误

时间:2019-03-11 02:16:40

标签: c

以下是我运行makefile时遇到的错误:

 gcc ./bin/sortMe.o ./bin/compare_id_ascending.o ./bin/compare_id_descending.o ./bin/compare_name_ascending.o ./bin/compare_name_descending.o ./bin/compare_money_ascending.o ./bin/compare_money_descending.o -o sortMe
./bin/compare_money_descending.o: In function `compare_money_ascending':
compare_money_descending.c:(.text+0x0): multiple definition of `compare_money_ascending'
./bin/compare_money_ascending.o:compare_money_ascending.c:(.text+0x0): first defined here
./bin/sortMe.o: In function `main':
sortMe.c:(.text+0x59f): undefined reference to `compare_money_descending'
collect2: error: ld returned 1 exit status
makefile:11: recipe for target 'sortMe' failed
make: *** [sortMe] Error 1

这是我的makefile的样子:

CC = gcc
CFLAGS = -ansi -Wall -I$(IDIR)

IDIR = ./include/
SDIR = ./src/
BDIR = ./bin/

all : sortMe

sortMe : $(BDIR)sortMe.o $(BDIR)compare_id_ascending.o $(BDIR)compare_id_descending.o $(BDIR)compare_name_ascending.o $(BDIR)compare_name_descending.o $(BDIR)compare_money_ascending.o $(BDIR)compare_money_descending.o
    $(CC) $(BDIR)sortMe.o $(BDIR)compare_id_ascending.o $(BDIR)compare_id_descending.o $(BDIR)compare_name_ascending.o $(BDIR)compare_name_descending.o $(BDIR)compare_money_ascending.o $(BDIR)compare_money_descending.o -o $@

$(BDIR)sortMe.o : $(SDIR)sortMe.c $(IDIR)compareElements.h $(IDIR)compareFunctions.h
    $(CC) $(CFLAGS) -c $(SDIR)sortMe.c -o $@

$(BDIR)compare_id_ascending.o : $(SDIR)compare_id_ascending.c $(IDIR)compareElements.h $(IDIR)compareFunctions.h
    $(CC) $(CFLAGS) -c $(SDIR)compare_id_ascending.c -o $@

$(BDIR)compare_id_descending.o : $(SDIR)compare_id_descending.c $(IDIR)compareElements.h $(IDIR)compareFunctions.h
    $(CC) $(CFLAGS) -c $(SDIR)compare_id_descending.c -o $@

$(BDIR)compare_name_ascending.o : $(SDIR)compare_name_ascending.c $(IDIR)compareElements.h $(IDIR)compareFunctions.h
    $(CC) $(CFLAGS) -c $(SDIR)compare_name_ascending.c -o $@

$(BDIR)compare_name_descending.o : $(SDIR)compare_name_descending.c $(IDIR)compareElements.h $(IDIR)compareFunctions.h
    $(CC) $(CFLAGS) -c $(SDIR)compare_name_descending.c -o $@

$(BDIR)compare_money_ascending.o : $(SDIR)compare_money_ascending.c $(IDIR)compareElements.h $(IDIR)compareFunctions.h
    $(CC) $(CFLAGS) -c $(SDIR)compare_money_ascending.c -o $@

$(BDIR)compare_money_descending.o : $(SDIR)compare_money_descending.c $(IDIR)compareElements.h $(IDIR)compareFunctions.h
    $(CC) $(CFLAGS) -c $(SDIR)compare_money_descending.c -o $@

run :
    ./sortMe

clean :
    rm ./bin/*.o ./sortMe

这些错误确实使我感到困惑,如果有人指出我的错误,我将不胜感激。 另外,我在声明它们的地方都必须添加$(BDIR),$(SDIR)和$(IDIR)吗?

1 个答案:

答案 0 :(得分:1)

错误消息:

./bin/compare_money_descending.o: In function `compare_money_ascending':
compare_money_descending.c:(.text+0x0): multiple definition of `compare_money_ascending'

说源文件compare_money_descending.c包含compare_money_ascending的定义,尽管它的先前定义已经存在。由于文件名compare_money_descending.c暗示它包含一个名为compare_money_descending的函数,但是我们遇到关于compare_money_ascending的错误,因此很可能错误是compare_money_descending.c包含一个使用compare_money_ascending的功能名称,compare_monehy_descending的目标位置。您很有可能复制并粘贴了源代码以用作新功能,但是忘记更改名称。

第二条消息:

sortMe.c:(.text+0x59f): undefined reference to `compare_money_descending'

告诉我们该程序不包含compare_money_descending的定义,这容易证实上述假设。