make and makefile file format of Linux

Posted by jaxxstorm on Tue, 04 Jan 2022 15:31:49 +0100

Another solution is to escape with a backslash before the newline character.

var-kept:

export foo<span class="token operator" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(166, 127, 89);">=</span>bar<span class="token punctuation" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(153, 153, 153);">;</span> \\
<span class="token keyword" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(0, 119, 170);">echo</span> <span class="token string" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(102, 153, 0);">"foo=\[$<span class="token property" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(153, 0, 85);">$foo</span>\]"</span>

The last method is to add ONESHELL: command.

.ONESHELL:

var-kept:

export foo<span class="token operator" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(166, 127, 89);">=</span>bar<span class="token punctuation" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(153, 153, 153);">;</span> 
<span class="token keyword" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(0, 119, 170);">echo</span> <span class="token string" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(102, 153, 0);">"foo=\[$<span class="token property" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(153, 0, 85);">$foo</span>\]"</span>

3, Makefile file syntax

3.1 notes

The pound sign (#) represents a comment in the Makefile.

#This is a comment

result.txt: source.txt

<span class="token comment" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(112, 128, 144);"># This is a comment

cp source.txt result.txt # this is also a comment

3.2 echoing

Normally, make prints each command and then executes it. This is called echoing.

test:

<span class="token comment" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(112, 128, 144);"># This is a test

If you execute the above rule, you will get the following results.

$ make test

#This is a test

You can turn off the echo by prefixing the command with @.

test:

@<span class="token comment" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(112, 128, 144);"># This is a test

If you execute make test now, there will be no output.

During the construction process, you need to know which command is currently executing, so you usually only add @ before comments and pure display echo commands.

test:

@<span class="token comment" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(112, 128, 144);"># This is a test

@echo TODO

3.3 wildcards

A wildcard is used to specify a set of file names that meet the criteria. The wildcards of Makefile are consistent with Bash, mainly including asterisk (*) and question mark (?) And [...]. For example, * O means all files with the suffix o.

clean:

    rm <span class="token operator" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(166, 127, 89);">-</span>f <span class="token operator" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(166, 127, 89);">\*</span><span class="token punctuation" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(153, 153, 153);">.</span>o

3.4 pattern matching

The Make command allows regular matching of file names. The main matching character used is%. For example, suppose there is F1 in the current directory C and F2 C two source files, which need to be compiled into corresponding object files.

%.o: %.c

Equivalent to the following.

f1.o: f1.c

f2.o: f2.c

Using the matching character%, you can build a large number of files of the same type with only one rule.

3.5 variables and assignors

Makefile allows you to customize variables with an equal sign.

txt = Hello World

test:

@<span class="token keyword" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(0, 119, 170);">echo</span> $<span class="token punctuation" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(153, 153, 153);">(</span>txt<span class="token punctuation" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(153, 153, 153);">)</span>

In the above code, the variable txt is equal to Hello World. When called, the variable needs to be placed in $().

When calling Shell variables, you need to add a dollar sign before the dollar sign, because the Make command will escape the dollar sign.

test:

@<span class="token keyword" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(0, 119, 170);">echo</span> $<span class="token property" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(153, 0, 85);">$HOME</span>

Sometimes, the value of a variable may point to another variable.

v1 = $(v2)

In the above code, the value of variable v1 is another variable v2. At this time, a question arises: is the value of v1 extended at definition time (static extension) or at run time (dynamic extension)? If the value of V2 is dynamic, the results of the two extension methods may be very different.

To solve similar problems, Makefile provides four assignment operators (=,: =,? =, + =). See StackOverflow.

VARIABLE = value

#Recursive extension is allowed during execution.

VARIABLE := value

#Extend at definition time.

VARIABLE ?= value

#The value is set only when the variable is empty.

VARIABLE += value

#Append the value to the end of the variable.

3.6 built in variables

The Make command provides a series of built-in variables, such as, ( C C ) finger towards When front send use of Compile translate implement , (CC) points to the compiler currently in use, (CC) points to the currently used compiler, (MAKE) points to the currently used Make tool. This is mainly for cross platform compatibility. For a detailed list of built-in variables, see manual.

output:

$<span class="token punctuation" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(153, 153, 153);">(</span>CC<span class="token punctuation" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(153, 153, 153);">)</span> <span class="token operator" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(166, 127, 89);">-</span>o output input<span class="token punctuation" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(153, 153, 153);">.</span>c

3.7 Automatic Variables

The Make command also provides automatic variables whose values are related to the current rule. There are mainly the following.

(1)$@

$@ refers to the current target, which is the target currently built by the Make command. For example, $@ of make foo refers to foo.

a.txt b.txt:

touch <span class="token property" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(153, 0, 85);">$@</span>

Equivalent to the following.

a.txt:

touch a<span class="token punctuation" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(153, 153, 153);">.</span>txt

b.txt:

touch b<span class="token punctuation" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(153, 153, 153);">.</span>txt

(2)$<

< finger generation The first one individual front Set strip piece . than as , gauge be by t : p 1 p 2 , that Do you < refers to the first precondition. For example, if the rule is t: p1 p2, then < refers to the first precondition. For example, if the rule is t:p1p2, then < refers to p1.

a.txt: b.txt c.txt

cp $<span class="token operator" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(166, 127, 89);"><</span> <span class="token property" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(153, 0, 85);">$@</span> 

Equivalent to the following.

a.txt: b.txt c.txt

cp b<span class="token punctuation" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(153, 153, 153);">.</span>txt a<span class="token punctuation" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(153, 153, 153);">.</span>txt 

(3)$?

? finger generation than order mark more new of place have front Set strip piece , of between with empty grid branch Septum . than as , gauge be by t : p 1 p 2 , his in p 2 of Time between stamp than t new , ? Refers to all preconditions for proxy target update, separated by spaces. For example, the rule is t: p1 p2, where the timestamp of p2 is newer than t, ? Refers to all preconditions for proxy target update, separated by spaces. For example, the rule is t:p1p2, where the timestamp of P2 is newer than t,? It means P2.

(4)$^

$^ refers to all preconditions, separated by spaces. For example, if the rule is t: p1 p2, $^ refers to p1 p2.

(5)$*

KaTeX parse error: Undefined control sequence: \* at position 1: \ ̲*̲ Refers to the matching part of the matching character%, *, indicating f1.

(6) $(@ D) and $(@ F)

($) and ($@ D) respectively @ of order record name and writing piece name . than as , @The directory name and file name of the. For example, @The directory name and file name of the. For example, @ is Src / input c. So ( @ D ) of value by s r c , (@ D) is src, The value of (@ D) is src, and the value of (@ F) is input c.

(7) $(< d) and $(< f)

$(< d) and $(< f) point to the directory name and file name of $<, respectively.

For a list of all automatic variables, see manual . Here is an example of an automatic variable.

dest/%.txt: src/%.txt

@<span class="token punctuation" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(153, 153, 153);">\[</span> <span class="token operator" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(166, 127, 89);">-</span>d dest <span class="token punctuation" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(153, 153, 153);">\]</span> <span class="token operator" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(166, 127, 89);">||</span> mkdir dest
cp $<span class="token operator" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(166, 127, 89);"><</span> <span class="token property" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(153, 0, 85);">$@</span>

The above code copies the txt file in the src directory to the dest directory. First, judge whether the dest directory exists. If it does not exist, create a new directory. Then, $< refers to the pre file (src/%.txt), and $@ refers to the target file (dest/%.txt).

3.8 judgment and circulation

Makefile uses Bash syntax to complete judgment and loop.

ifeq ($(CC),gcc)

libs=$(libs_for_gcc)

else

libs=$(normal_libs)

endif

The above code determines whether the current compiler is gcc, and then specifies a different library file.

LIST = one two three

all:

<span class="token keyword" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(0, 119, 170);">for</span> i <span class="token keyword" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(0, 119, 170);">in</span> $<span class="token punctuation" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(153, 153, 153);">(</span>LIST<span class="token punctuation" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(153, 153, 153);">)</span><span class="token punctuation" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(153, 153, 153);">;</span> <span class="token keyword" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(0, 119, 170);">do</span> \\
    <span class="token keyword" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(0, 119, 170);">echo</span> $<span class="token property" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(153, 0, 85);">$i</span><span class="token punctuation" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(153, 153, 153);">;</span> \\
<span class="token keyword" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(0, 119, 170);">done</span>

#Equivalent to

all:

<span class="token keyword" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(0, 119, 170);">for</span> i <span class="token keyword" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(0, 119, 170);">in</span> one two three<span class="token punctuation" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(153, 153, 153);">;</span> <span class="token keyword" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(0, 119, 170);">do</span> \\
    <span class="token keyword" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(0, 119, 170);">echo</span> <span class="token property" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(153, 0, 85);">$i</span><span class="token punctuation" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(153, 153, 153);">;</span> \\
<span class="token keyword" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(0, 119, 170);">done</span>

The running result of the above code.

one

two

three

3.9 functions

Makefile can also use functions in the following format.

$(function arguments)

#Or

${function arguments}

Makefile provides many Built in function , available for calling. Here are some commonly used built-in functions.

(1) shell function

Shell functions are used to execute shell commands

srcfiles := $(shell echo src/{00..99}.txt)

(2) wildcard function

The wildcard function is used to replace the wildcard of Bash in Makefile.

srcfiles := $(wildcard src/*.txt)

(3) subst function

The subst function is used to replace text. The format is as follows.

$(subst from,to,text)

The following example replaces the string "feet on the street" with "feet on the street".

$(subst ee,EE,feet on the street)

Here is a slightly more complex example.

comma:= ,

empty:=

#The space variable uses two empty variables as identifiers, one of which is a space

space:= $(empty) $(empty)

foo:= a b c

bar:= $(subst < s p a n c l a s s = " t o k e n p u n c t u a t i o n " s t y l e = " m a r g i n : 0 p x ; p a d d i n g : 0 p x ; l i s t − s t y l e − t y p e : n o n e ; b o r d e r : n o n e ; c o l o r : r g b ( 153 , 153 , 153 ) ; " > ( < / s p a n > s p a c e < s p a n c l a s s = " t o k e n p u n c t u a t i o n " s t y l e = " m a r g i n : 0 p x ; p a d d i n g : 0 p x ; l i s t − s t y l e − t y p e : n o n e ; b o r d e r : n o n e ; c o l o r : r g b ( 153 , 153 , 153 ) ; " > ) < / s p a n > < s p a n c l a s s = " t o k e n p u n c t u a t i o n " s t y l e = " m a r g i n : 0 p x ; p a d d i n g : 0 p x ; l i s t − s t y l e − t y p e : n o n e ; b o r d e r : n o n e ; c o l o r : r g b ( 153 , 153 , 153 ) ; " > , < / s p a n > <span class="token punctuation" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(153, 153, 153);">(</span>space<span class="token punctuation" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(153, 153, 153);">)</span><span class="token punctuation" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(153, 153, 153);">,</span> <spanclass="tokenpunctuation"style="margin:0px;padding:0px;list−style−type:none;border:none;color:rgb(153,153,153);">(</span>space<spanclass="tokenpunctuation"style="margin:0px;padding:0px;list−style−type:none;border:none;color:rgb(153,153,153);">)</span><spanclass="tokenpunctuation"style="margin:0px;padding:0px;list−style−type:none;border:none;color:rgb(153,153,153);">,</span>(comma),$(foo))

# bar is now `a,b,c'.

(4) patsubst function

The patsubst function is used to replace pattern matching. The format is as follows.

$(patsubst pattern,replacement,text)

The following example replaces the file name "x.c.c bar.c" with "x.c.o bar.o".

$(patsubst %.c,%.o,x.c.c bar.c)

(5) Replace suffix

Topics: Design Pattern Interview Programmer