MAKE Utility


The MAKE utility is a build automation tool that is commonly used in Software Development - 20230221103859 to compile and link source code into executable programs. The MAKE utility reads a file called a "makefile" that specifies the dependencies between the source files and the target executables, and it automates the process of recompiling the source code whenever necessary.

A makefile typically lists all the source files and object files needed to build an executable, and it specifies the dependencies between these files. For example, a makefile might specify that an object file depends on a header file and a source file, and that the executable depends on several object files. When the makefile is processed by the MAKE utility, it uses this information to determine which source files need to be recompiled and which object files need to be relinked in order to produce the final executable.

The MAKE utility compares the timestamps of the source files and object files to determine whether they need to be recompiled. If a source file has been modified since the last time it was compiled, the MAKE utility recompiles it and updates the object file. If an object file has been modified, the MAKE utility relinks the executable with the updated object file.

The MAKE utility can save time and reduce the risk of errors in the build process by automating the process of recompiling source code. It is commonly used in software development to manage complex build processes, and it is an essential tool for many software projects.

MAKE is used mainly for C/C++. For java, we can use Ant

How To Use the Makefile

Production Rule

mod1.o: mod1.c mod1.h mod2.h  
	cc -c mod1.c

Linking

appli: mod1.o mod2.o
	cc -o appli mod1.o mod2.o

Compilation

mod1.o: mod1.c mod1.h mod2.h
	cc -c mod1.c

Then, we can just execute it using make. It will update the main target. We can specify a target using make mod1.o.

Phony Targets

A phony target is a special type of target in a makefile that is used to specify a set of dependencies and commands that should be executed whenever the target is specified on the command line. Unlike regular targets, phony targets do not represent files and their timestamps are not used to determine whether they need to be updated.

Phony targets are often used to specify high-level targets that are not tied to specific files, but rather represent actions that should be performed, such as cleaning up the build directory, printing the value of a variable, or running a test suite. For example, a makefile might have a target named "clean" that removes all generated files from the build directory, or a target named "test" that runs the test suite for a project.

When a phony target is specified on the command line, the MAKE utility processes the dependencies and commands associated with the target, regardless of whether any of the dependencies have been modified. This allows the makefile to specify complex actions that should be performed whenever a certain target is specified, without having to worry about the timestamps of specific files.

clean:  
	rm -f *.tmp  
    rm -f core

Variables

We can use variables in makefiles. For example:

PACK_DIRS = \  
    org/appli/filters \  
    org/appli/utils \  
    org/appli/protocol

And then we reference them this way:

cc -c $(SRCDIR)mod1.c

Dependencies Factorisation

objects = mod1.o mod2.o main.o  
$(objects) : main.h  
mod1.o mod2.o :    mod2.h  
mod1.o main.o :    mod1.h

Patterns

mod%.o : mod%.c mod%.h

Functions

HEADERS = $(foreach pack, $(PACK_DIRS), $(subst /,_,$(pack))_%.h )

Directives

include config.mk
META

Status:: #wiki/notes/mature
Plantations:: Software Development - 20230221103859
References:: ILOG