0

I have a list of two source files, but the makefile compiles the first source file twice. This is my makefile:

Source = ../Src
Source_Files = $(Source)/File_1.c  \
               $(Source)/File_2.c 

Obj = ./Obj
Object_Files = $(notdir $(Source_Files))
Object_Files := $(Object_Files:.c=.o)
Object_Files := $(addprefix $(Obj)/, $(Object_Files))

all: $(Object_Files)

$(Object_Files): $(Source_Files) 
    @echo Compile $<
    @gcc -o $@ -c $<

The output looks like this:
Compile ../Src/File_1.c
Compile ../Src/File_1.c

Why File_2.c does not get compiled?

Daniel
  • 1

1 Answers1

1

$< means "the first dependency", and since $(Source_Files) is always $(Source)/File_1.c $(Source)/File_2.c, the first dependency is always $(Source)/File_1.c.

To get what you want, you need to use patterns, e.g.

$(Object_Files): $(Obj)/%.o: $(Source)/%.c
    @echo Compile $<
    @gcc -o $@ -c $<

With this code, there is only one dependency, and it is the one generated from the current target.

root
  • 5,528
  • 1
  • 7
  • 15