make
has certain default rules
$(CC) $(CFLAGS) -o $@ $^
is the general default rule.
mgrid.o: array_alloc.h timer.h
only tells that mgrid.o
is also dependent on array_alloc.h timer.h
Question
I am trying to edit a makefile, I have built my own before but I'm struggling to understand this very minimal example, which clearly uses conventions that I am not aware of. I've checked the GNU documentation but I'm not quite sure what to search for.
The makefile defines SRCS
OBJS
LIBS
CC
CFLAGS
The makefile then compiles the libraries without being told specifically how. e.g. line in file is:
mgrid.o: array_alloc.h timer.h
But the makefile actually executes:
gcc -std=c89 -Wall -Wextra -pedantic -c -o mgrid.o mgrid.c
Could anybody help explain what is going on here? And where I can find information on how these standards are defined?
Solution
make
has certain default rules
$(CC) $(CFLAGS) -o $@ $^
is the general default rule.
mgrid.o: array_alloc.h timer.h
only tells that mgrid.o
is also dependent on array_alloc.h timer.h
OTHER TIPS
mgrid.o: array_alloc.h timer.h
means "I want to compile mgrid.o only if array_alloc.h timer.h have been changed." You do not indicate which rule must be applied, so make will take his dafault rule for .o files which is the line given by Sergey L. :
$(CC) $(CFLAGS) -o $@ $^
When make sees a .o file, it looks for a .c file with the same name.
In your makefile, you set the dependencies of mgrid.o
to be array_alloc.h
and timer.h
.
The makefile checks if the following files exist:
array_alloc.h timer.h
Since it does, it then proceeds to compile mgrid.c
to mgrid.o
.
There's a good manual at https://www.gnu.org/software/make/manual/make.html
As Sergey L. said, it tells that mgrid is dependent on these two files, that means: checks if the files exist, checks if they where modified after mgrid.o has been compiled : if so, recompile it.