GNU-like make & makefile under Linux

make&makefile

What

There are countless source files of a project, which are placed in several directories according to type, function, and module. Makefile defines a series of rules to specify which files need to be compiled first, what files need to be recompiled, and what medicine is compiled last . . .
The advantage of makefile is "automated compilation & automatic interpreter". Once it is written, only one make command is needed, and the entire project is fully automated to compile, which greatly improves the efficiency of software development.
make: command
makefile: file
Explain the makefile through make == project automatic construction.
Makefile file rules:
① Target object: What executable program or target program (.obj) needs to be generated;
② Dependent object: The file that depends on when generating the target object;
③ Compilation command: How to use the dependent object to generate the target object.
format:
Target object: dependent object
== Compile command ==

How

Take the Fibonacci number program as an example:
Insert picture description here
edit with vim makefile:
Insert picture description here
Insert picture description here
directly type the make command to generate an executable program. (Of course this is the simplest makefile)
The principles of make to explain makefiles:
1. When the make interpreter interprets makefiles, it compares the generation time of dependent objects (source files) and target objects (executable programs).
If the generation time of the target object (executable program) is closer to now, it means that the target object is the latest and does not need to be recompiled.
if ((time-target object generation time) <(time-dependent object generation time))

If the generation time of the dependent object (source file) is close to now, it means that the dependent object has been changed and needs to be recompiled.
if ((time-target object generation time)> (time-dependent object generation time))
2. make only serves to generate the first target object. Once make interprets and generates the first target object, stop the interpretation.
3. When the make interpreter interprets the makefile, in order to generate the first target object, it will also determine whether the object that the first target object depends on exists. If it does not exist, it will look for the method to generate the dependent object in the subsequent statements of the makefile. , The dependent object is generated first, and then the dependent object is used to generate the first target.
Insert picture description here
Insert picture description here
Predefined variables:
$^: all dependent objects;
$@: target object.

Makefile cleanup:
delete the generated target object;
edit clean in vim, command-make clean.

Variables can also be customized in makefile

Guess you like

Origin blog.csdn.net/qq_43560037/article/details/114759409
Recommended