当前位置:网站首页>Command of gun make (4) rule

Command of gun make (4) rule

2022-06-26 01:55:00 Chen_ Hulk

The order of the rule is made up of shell The command line consists of , Be executed one by one .

Usually there are different in the system shell, But in make Handle Makefile In the process , If not specified , Then for command line parsing in all rules, use “/bin/sh” complete .

 

1.  Command echo

Echo refers to outputting the command to be executed to the standard output device before executing the command .

@ Parameters :

If the command line starts with @ Start , be make The command to be executed will not be echoed when the command is executed .

such as :

@echo  Start compilation  
 Output is : Start compilation 

echo  Start compilation  
 Output is :echo  Start compilation 

 

-n or --just-print Parameters :
make Only the command to be executed is displayed during execution , But I will not really execute these commands .

 

-s or --slient Parameters :
Disable the explicit... Of the command to be executed , It's like all the commands you want use @ At first .

 

2. Execution of command

2.1 Independent execution

When a multiline command , Each line of command will be in a separate sub - shell In process execution , Execution is independent of each other .

Multiple commands on the same line belong to a complete shell Command line .

2.2 Concurrent execution

Use -j perhaps --job bring make Multiple commands can be executed at the same time .

The problem of concurrent execution :

  • The output information of multiple simultaneously executed commands will be output to the terminal at the same time .
  • Only one of the multiple processes executing commands at the same time can obtain standard input , Other processes that need to read the standard input stream cause errors because the input stream is invalid .

 

3. Command execution error

If a command in a rule goes wrong ,make The execution of subsequent commands of the current rule will be abandoned , It is also possible to terminate the execution of all rules .

 

In order to ignore some unrelated command execution failure :

  • Add... Before the command “-” Indicates that the execution of this command is ignored .
  • Use -i perhaps --ignore-errors, bring make Ignore command execution errors in all rules .
  • Use -k perhaps --keep-going, Make it impossible to exit immediately when an error occurs , Instead, continue the execution of subsequent commands , There is no abnormal exit until the last link is executed .

 

4. interrupt make Implementation

make If you receive a fatal signal when executing a command , that make The target files of the rules that have been rebuilt in this process will be deleted .

reason :
If the interrupt signal shuts down the compiler , Then it generates foo.o It could be incomplete , At that time, this was not completed foo.o The time stamp of the file is larger than that of the source program foo.c Timestamp NEW , If you don't delete foo.o, Then execute next time make This file is considered to be up-to-date and will not be rebuilt .

 

5. make Recursive execution of

make Recursive execution means to execute in makefile Use in make As a command to execute itself or other makefile Documentation process .

stay make When called recursively , Variables should be used in the command line of the rule MAKE Instead of make. The advantage is when we use a different version of make The program , It can guarantee the use of the top layer make Program and its subdirectories make The program remains .

subsystem:
    cd subdir && $(MAKE)

 Equivalent to entering subdirectory for execution :
subsystem:
    $(MAKE) -C subdir             

 

6. Variables and recursion

stay make In the process of execution , When using environment variables to pass variables defined by the upper layer , Will not cover the child make in Makefile Variable definition with the same name in the file .

If there is a repetition , Then take the son Makefile Whichever comes first , Unless used -e Options .

 

If you need to pass a variable to a child make, need export Make a designation .

export VAR
  • If not used export Make a designation , The upper make Only the environment variables that have been initialized and Use the variables specified on the command line (make CFLAGS += -g) Pass to child make Program .
  • There are special variables SHELL and MAKEFLAGS , These two variables unless you use unexport Make a statement , They are in the whole make It is always automatically passed to the child during execution make.
  • When you don't want to pass MAKEFLAGS, You need to call the sub make Empty the variable here :
sub:
    cd subdir && $(MAKE) MAKEFLAGS=
  • One without parameters export The indicator indicates that Makefile All variables defined in are passed to the child make. If you don't need one of them , Use unexport Specified separately , But without parameters unexport It makes no sense .
export

 

7. -w Options

In multilevel make During recursive call , Use -w perhaps --print-directory Give Way make Give corresponding prompt information before starting to compile a directory and after completing directory compilation , Easy to track .

Usually -w Will be opened by default . have access to -s Disable this option .

 

such as , In the catalog /u/gun/make Execute under directory make -w:

 See... Before execution starts :
make:Entering directory '/u/gun/make'
 After the execution is completed, you see :
make:Leaving directory '/u/gun/make'

 

8. Define command packages

Use define Define a set of commands as command packages , Implement functions similar to functions .

In the command package , References to variables and functions in the command body are not expanded , When rules are used, they are fully expanded .

such as :

define run-yacc
yacc $(firstword $^)
mv y.tab.c [email protected]
endef

 Use :
foo.c:foo.y
    $(run-yacc)

 Replacement process :
 Command package  $^  Will be  foo.y Replace 
[email protected] Will be foo.c Replace 

 

原网站

版权声明
本文为[Chen_ Hulk]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206252357252768.html