Basic Introduction of Makefile-set1
- prashant raj
- Mar 16, 2021
- 2 min read
Updated: May 2, 2021

What is makefile?
"makefile" is a file which tell to 'make' utility what to do. Most
often, the makefile tells 'make' utility how to compile and link a program.
What is make?
'make' is utility which automatically determines which pieces of a large
program need to be recompiled, and issues commands to recompile them.
Note:
1) to use 'make', you must write a file called the "makefile"
that describes the relationships among files in your program and
provides commands for updating each file. In a program, typically, the
executable file is updated from object files, which are in turn made by
compiling source files.
2) When 'make' recompiles the editor, each changed C source file must be
recompiled. If a header file has changed, each C source file that
includes the header file must be recompiled to be safe. Each
compilation produces an object file corresponding to the source file.
Finally, if any source file has been recompiled, all the object files,
whether newly made or saved from previous compilations, must be linked
together to produce the new executable editor.
Rules to create makefile:
TARGET ... : dependency ...
RECIPE
...
...
TARGET:
A "target" is usually the name of a file that is generated by a
program; examples of targets are executable or object files. A target
can also be the name of an action to carry out, such as 'clean' (*note
Phony Targets::).
Dependency:
A " Dependency" is usually as input that is used as input to create the
target. A target often depends on several files.
RECIPE:
recipe is in a rule with prerequisites and serves to create
a target file if any of the prerequisites change. However, the rule
that specifies a recipe for the target need not have prerequisites. For
example, the rule containing the delete command associated with the
target 'clean' does not have prerequisites.
Simple Example fo makefile:
Here is a straightforward makefile that describes the way an executable
file called 'edit' depends on eight object files which, in turn, depend
on eight C source and three header files.
In this example, all the C files include 'defs.h', but only those
defining editing commands include 'command.h', and only low level files
that change the editor buffer include 'buffer.h'.
edit : main.o kbd.o command.o display.o insert.o search.o files.o \ utils.o
cc -o edit main.o kbd.o command.o display.o insert.o \ search.o files.o utils.o
main.o : main.c defs.h
cc -c main.c
kbd.o : kbd.c defs.h command.h
cc -c kbd.c
command.o : command.c defs.h command.h
cc -c command.c
display.o : display.c defs.h buffer.h
cc -c display.c
insert.o : insert.c defs.h buffer.h
cc -c insert.c
search.o : search.c defs.h buffer.h
cc -c search.c
files.o : files.c defs.h buffer.h command.h
cc -c files.c
utils.o : utils.c defs.h
cc -c utils.c
clean :
rm edit main.o kbd.o command.o display.o \
insert.o search.o files.o utils.o
We split each long line into two lines using backslash/newline; this is
like using one long line, but is easier to read. *Note Splitting Long
Lines: Splitting Lines.
To use this makefile to create the executable file called 'edit',
type:
make
To use this makefile to delete the executable file and all the object
files from the directory, type:
make clean
To execute the executable file:
./edit
Comments