My makefile is giving the following error messages:

ma.c:(.text+0x5aa): multiple definition of `dividetoken'
obj/src/ag.o:ag.c:(.text+0x0): first defined here
obj/src/ma.o: In function `main':
ma.c:(.text+0x629): multiple definition of `main'
obj/src/ag.o:ag.c:(.text+0x6ef): first defined here
obj/src/cv.o: In function `readln':
cv.c:(.text+0xb2): multiple definition of `readln'
obj/src/ag.o:ag.c:(.text+0x7f): first defined here
obj/src/cv.o: In function `dividetoken':
cv.c:(.text+0x176): multiple definition of `dividetoken'
obj/src/ag.o:ag.c:(.text+0x0): first defined here
obj/src/cv.o: In function `main':
cv.c:(.text+0x1f5): multiple definition of `main'
obj/src/ag.o:ag.c:(.text+0x6ef): first defined here
obj/src/sv.o: In function `readln':
sv.c:(.text+0x0): multiple definition of `readln'
obj/src/ag.o:ag.c:(.text+0x7f): first defined here
obj/src/sv.o: In function `dividetoken':
sv.c:(.text+0x150): multiple definition of `dividetoken'
obj/src/ag.o:ag.c:(.text+0x0): first defined here
obj/src/sv.o: In function `main':
sv.c:(.text+0xcb5): multiple definition of `main'
obj/src/ag.o:ag.c:(.text+0x6ef): first defined here
collect2: error: ld returned 1 exit status
makefile:17: recipe for target 'program' failed
make: *** [program] Error 1

What should I do ? Can it be easily solved ? I don't know what to do

My makefile looks like this:

ODIR = obj
IDIR = include
SDIR = src

CC = gcc
EXE = program

DEPS = $(IDIR)/$(wildcard*.h)
SOURCES = $(wildcard $(SDIR)/*.c)

OBJECTS = $(foreach o, $(patsubst %.c,%.o,$(SOURCES)), $(ODIR)/$o)

$(ODIR)/%.o : %.c $(DEPS)
    $(CC) -c -o [email protected] $< 

$(EXE): $(OBJECTS)
    $(CC) $(OBJECTS) -o $(EXE)

clean:
    rm $(ODIR)/$(SDIR)/*
    rm $(EXE)

I hope this can help to see what my mistake was.

1 Answers

2
Chris Turner On

Your Makefile looks fine - I can't see any problems with it as such[*]. The problem is your code that you're compiling.

Take a look at one of the errors you're getting:

obj/src/sv.o: In function `main':

sv.c:(.text+0xcb5): multiple definition of `main'

obj/src/ag.o:ag.c:(.text+0x6ef): first defined here

That is saying that you have a file called "sv.c" that has a function called main in it. And that you have another file called "ag.c" that also has a function called main in it. You're trying to compile both those files into the same program, but you're only allowed to have one function called main.

[*] At a guess it looks like you've got several programs living in your "src" directory and your Makefile is trying to build everything into one giant program. If this is the case you need to either separate the code into their own directories or change your Makefile so that SOURCES only lists the source files that are relevant to the program you're compiling.

If you want to compile several programs within the same Makefile you need to split things up. Each program needs its own build rule that specifies the dependencies it needs. You can generate the OBJECTS variable for each of them the same way - but you'll need to list the C files each program needs.

SOURCES1 = ag.c other.c something.c
SOURCES2 = sv.c morecode.c something.c

OBJECTS1 = $(foreach o, $(patsubst %.c,%.o,$(SOURCES1)), $(ODIR)/$o)
OBJECTS2 = $(foreach o, $(patsubst %.c,%.o,$(SOURCES2)), $(ODIR)/$o)

$(ODIR)/%.o : %.c $(DEPS)
    $(CC) -c -o [email protected] $< 

program1: $(OBJECTS1)
    $(CC) $^ -o [email protected]

program2: $(OBJECTS2)
    $(CC) $^ -o [email protected]

For reference $^ means all the dependencies.