Reputation: 3747
I have a file hello.c (no hello.o yet).
When I give command
gcc -o hello hello.o
It gives error gcc: error: hello.o: No such file or directory
, but when I create a makefile containing following rule,
hello: hello.o
gcc -o hello hello.o
and run make
, it automatically creates hello.o
from hello.c
and then successfully creates hello
executable.
Since I didn't write command to create hello.o from hello.c in makefile, how does make know which command it should run?
Upvotes: 0
Views: 270
Reputation: 21003
make
has not only uses the rules you added explicitly in makefiles, it has implicit rules as well. The full list you can find at https://www.gnu.org/software/make/manual/html_node/Catalogue-of-Rules.html#Catalogue-of-Rules
and the first implicit rule in the list is
Compiling C programs
n.o is made automatically from n.c with a recipe of the form ‘$(CC) $(CPPFLAGS) $(CFLAGS) -c’.
Upvotes: 4
Reputation: 399949
Because make has been around the block enough to have implicit rules about how to transform a .c file to a .o file:
Compiling C programs
n.o is made automatically from n.c with a recipe of the form ‘$(CC) $(CPPFLAGS) $(CFLAGS) -c’.
Upvotes: 0