Reputation: 11
Following is the error I get when I run my 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
This is what my makefile looks like:
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
These errors really confuse me and would appreciate it if anyone pointed out my mistake. Also, do I have to add $(BDIR) $(SDIR) and $(IDIR) everywhere I declare them?
Upvotes: 0
Views: 59
Reputation: 224310
The error messages:
./bin/compare_money_descending.o: In function `compare_money_ascending':
compare_money_descending.c:(.text+0x0): multiple definition of `compare_money_ascending'
say that the source file compare_money_descending.c
contains a definition of compare_money_ascending
although a prior definition of it already exists. Since the file name compare_money_descending.c
suggests it contains a function named compare_money_descending
but we are getting an error about compare_money_ascending
, it is likely the mistake is that compare_money_descending.c
contains a definition using a function name of compare_money_ascending
where compare_monehy_descending
was intended. Quite likely you copied and pasted source code to use as a new function but forget to change the name.
The second message:
sortMe.c:(.text+0x59f): undefined reference to `compare_money_descending'
tells us the program does not contain a definition of compare_money_descending
, which tends to confirm the above hypothesis.
Upvotes: 2