What is a makefile?

It is simply understood as a compilation rule of a project file, specifying which files are compiled first, which files are compiled after, which files are not compiled, and so on.

The makefile rules

Simple makefile file

C GCC -o hello hello. C clean: rm -f helloCopy the code

Special advice: each command must be preceded by a TAB character, not a space.

1. Command format

target:required
<TAB>commond
Copy the code

Target is the name of the file to be generated. It can be an OBJ file or an executable, or a command such as clean.

Required refers to the files, source code, that need to be relied upon.

Commond is the command to execute when generating the object file. There can be multiple commands, each on one line.

2. Define variables in the Makefile

The syntactic form of a variable definition

Immediate = deffered (recursive) immediate? =deffered (conditional) immediate :=immediate (simple) immediate +=deffered or immediate (append) define immediate deffered endefCopy the code

GNU Make assigns values to variables in two ways, immediate variables and delayed variables.

Delay variable: determine the value when it is actually used, using =,? = define is a delay variable.

Immediate variables: := Assignments are immediate variables

3. Makefile common functions

The function call format is as follows:

$(function  arguments)
Copy the code

Function (); arguments (); Function names and parameters are separated by Spaces, the latter are separated by tabs, and multiple parameters are separated by,.

  • $(subst from,to, text)

Replace each to with from in text for example: $(subst EE, EE, free fresh)

  • $(patsubst pattern,replacement,text) In text, those conforming to the pattern rule are replaced by replacement. For example, $(patsubstr %.c,%.o, test.c a.c b.c) replaces all.c files with.o files. The result is “test.o.o.o.O”.

  • $(strip string) Strips leading and trailing Spaces, and turns multiple Spaces in between into a single space.

  • $(findString target, in) finds target in, returns target if found, otherwise returns null for example: (FindStringa,c,b,a) returns a, if (findString a,c,b,a) returns a, if (findString a,c,b,a) returns a, if (findString a,c,b) returns “”.

  • $(filter pattern… , text) Filters the contents that do not match pattern in text.

  • $(filtere-out pattern… , text) the inverse function of filter retains the contents that match pattern in text matches.

  • $(sort list) The words in the list are sorted alphabetically and separated by a single space without duplicates.

  • $(dir names...)