Makefile include header
You must link against the compilation unit inc.o
which you obtain by compiling inc.c
.
In general that means that you must supply all object files that contain functions that are used in main.c
(transitively). You can compile these with implicit rules of make
, no need to specify extra rules.
You could say:
app: main.c inc.o inc.h
cc -o app inc.o main.c
And make
will know on its own how to compile inc.o
from inc.c
although it will not take inc.h
into account when determining whether inc.o
must be rebuilt. For that you would have to specify your own rules.
you didn't compile the inc.c file
app: main.c inc.h
cc -o app main.c inc.c
You need to compile inc.c
as well. A suitable approach (better scalable to larger applications) would be to split the Makefile up into different targets. The idea is: one target for every object file, then one target for the final binary. For compiling the object files, use the -c
argument.
app: main.o inc.o
cc -o app main.o inc.o
main.o: main.c inc.h
cc -c main.c
inc.o: inc.c inc.h
cc -c inc.c