Hi guys!
I'm trying to understand deeply how makefiles work.
For example, I've the following one:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
CC = gcc
CFLAGS = -I.
DEPS = int_array.h
OBJS = int_array.o test_int_array.o
%.o: %.c $(DEPS)
$(CC) -c -o $@ $< $(CFLAGS)
test_int_array: $(OBJS)
$(CC) -o $@ $^ $(CFLAGS)
clean:
rm -rf *.o test_int_array *.dSYM
|
The part that I really don't understand fully is :
1 2 3 4 5 6 7 8 9 10
|
...
%.o: %.c $(DEPS)
$(CC) -c -o $@ $< $(CFLAGS)
test_int_array: $(OBJS)
$(CC) -o $@ $^ $(CFLAGS)
...
|
I know that the option
-c
basically indicates just to run the preprocessor, compiling and assembling steps (i.e. without producing executables, I guess).
-o
means to write the output to the specified file. Which file in this case?
I understood that
$@
(and
$^
for right) is apparently referring to a "left" side, but which one? Is it referring, in the first case, to the left side of
:
, that is
%.o
?
What does
$<
mean?
Could you please explain step by step how the make tool would interpret those two statements?
I think I understood this part more or less:
1 2 3 4 5 6
|
...
test_int_array: $(OBJS)
$(CC) -o $@ $^ $(CFLAGS)
...
|
which should mean produce an executable called "test_int_array" (which basically is indicated by these options
-o $@
from the
$(OBJS)
files on the right (stated using the option
$^
).
Is
$(CFLAGS)
needed in both cases? Does the order matter?