Easy-to-understand Makefile Getting Started (08)—default shell (/bin/sh), command echo, make parameters (-n only displays commands but does not execute, -s prohibits all echoes), single-line commands, multi-line commands, concurrent carried out

1. Shell related

1.1 Default shell

MakefileCommand is used by the shellcommand rows, they are executed one by one.

Multiple commands separated by semicolons to be used, Makefileany command to be tabkey to begin.

There can be blank lines and comment lines between multiple command lines, and the blank lines will be automatically ignored when the rule is executed.

Usually there may be differences in the system shell. But the makeprocess Makefilewhen the process, if not explicitly specified, all rules for parsing command line bin/shto complete.

Such as:

all:
	echo $(SHELL)

Execution makeresults after

wohu@ubuntu:~/cpp/func$ make 
echo /bin/sh
/bin/sh
wohu@ubuntu:~/cpp/func$ 

1.2 Execute shell

grammar:

$(shell <shell command>)

Its role is to execute a shellcommand, and the shellresults of the command as a return function.

And the role <shell command>as `anti quotes

all:
	echo $(shell pwd)

The implementation of makethe results:

wohu@ubuntu:~/cpp/func$ make
echo /home/wohu/cpp/func
/home/wohu/cpp/func
wohu@ubuntu:~/cpp/func$ 

2. Command echo

Typically makewill be executed if the command line output to the standard output device before executing the command line. We call it "echo", if the command line with the rules of the character @start, then makein the course of implementation will not show the command to be executed.

Example:

all:
	@echo $(SHELL)

Execution makeresults after

wohu@ubuntu:~/cpp/func$ make 
/bin/sh
wohu@ubuntu:~/cpp/func$ 

No characters before executing the command @, then makethe output will be /bin/sh.

MakefileIn writing shellyou can add two kinds of prefix command @and -, or no prefix. Three formats of shellcommands differences are as follows:

  • No prefix: output the executed command and the result of the command execution, stop execution if an error occurs
  • Prefix @: only output the result of command execution, stop execution if error occurs
  • Prefix -: If there is an error in the command execution, ignore the error and continue execution

3. Parameters of make

  • -nOr --just-printto display only the commands to be executed when performing, but not really executing this command, including the use of @command character to start, usually written for checking Makefilethe contents;
  • -sOr --slientit is prohibited to display all of the execution of the command, as if all the rows using the command @to start the same;

4. Execution of commands

When the rules of the target needs to be rebuilt, this rule commands defined will be executed, if a multi-line command, then each line of command will be in a separate sub shellis the implementation process.

Therefore, the execution of commands between multiple command lines is independent of each other, and there is no interaction between them.

In Makefilethe writing on the same line of multiple commands belong to a complete shellcommand line, writing a separate command line is a separate shellcommand line.

Therefore: In a command in the command-line rule cdchange will not affect their execution directory command behind. That is after the working directory command will not be executed before using cdthe directory entry. If you achieve this, it is not able to cdand the following two lines to write on command. Instead, put these two commands on one line and separate them with a semicolon. This is a complete shellcommand line.

all:
	cd /home/wohu/cpp/func;\
	pwd ; \
	ls

Execute make result

wohu@ubuntu:~/cpp/func$ make
cd /home/wohu/cpp/func;\
pwd ; \
ls
/home/wohu/cpp/func
demo.cpp  demo.h  Makefile
wohu@ubuntu:~/cpp/func$ 

If you want a complete shellcommand line written on multiple lines, you need to use the backslash \to multiple lines of command is connected, that they are a complete shellcommand line.

5. Concurrent execution of commands

GNU makeSupports simultaneous execution of multiple commands. Normally, only one command is being executed at the same time, and the next command can only be executed after the current command ends. But it is by makecommand-line options -jor --jobsto tell makeat the same time allows multiple commands simultaneously.

If the option -jexists after an integer, its meaning is told makeat the same time allows the number of commands executed simultaneously row. This number is called job slots. When -jtime digital option does not appear, use the default job solts, a value of 1, indicating makethe serial command execution rules (the same time can only be performed by a single command).

The problem with executing commands in parallel is obvious:

  • The output information of multiple concurrently executed commands will be output to the terminal at the same time. When an error occurs, it is difficult to distinguish the command execution error based on a large amount of messy information;
  • At the same time, there may be multiple command execution processes that read standard input at the same time, but for the standard input device, only one process can access it at the same time. That is to say, at a certain point in time, makeonly one of the processes currently executing can be guaranteed to read the standard input stream. The standard input stream of other process keys will be set to invalid. Therefore, at this moment, only one of the processes that execute commands gets the standard input, and the other processes that need to read the standard input stream cause fatal errors because the input stream is invalid.

Guess you like

Origin blog.csdn.net/wohu1104/article/details/111054672