1. Shell related
1.1 Default shell
Makefile
Command is used by the shell
command rows, they are executed one by one.
Multiple commands separated by semicolons to be used, Makefile
any command to be tab
key 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 make
process Makefile
when the process, if not explicitly specified, all rules for parsing command line bin/sh
to complete.
Such as:
all:
echo $(SHELL)
Execution make
results 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 shell
command, and the shell
results of the command as a return function.
And the role <shell command>
as `anti quotes
all:
echo $(shell pwd)
The implementation of make
the results:
wohu@ubuntu:~/cpp/func$ make
echo /home/wohu/cpp/func
/home/wohu/cpp/func
wohu@ubuntu:~/cpp/func$
2. Command echo
Typically make
will 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 make
in the course of implementation will not show the command to be executed.
Example:
all:
@echo $(SHELL)
Execution make
results after
wohu@ubuntu:~/cpp/func$ make
/bin/sh
wohu@ubuntu:~/cpp/func$
No characters before executing the command @
, then make
the output will be /bin/sh
.
Makefile
In writing shell
you can add two kinds of prefix command @
and -
, or no prefix. Three formats of shell
commands 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
-n
Or--just-print
to 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 checkingMakefile
the contents;-s
Or--slient
it 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 shell
is 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 Makefile
the writing on the same line of multiple commands belong to a complete shell
command line, writing a separate command line is a separate shell
command line.
Therefore: In a command in the command-line rule
cd
change will not affect their execution directory command behind. That is after the working directory command will not be executed before usingcd
the directory entry. If you achieve this, it is not able tocd
and 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 completeshell
command 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 shell
command line written on multiple lines, you need to use the backslash \
to multiple lines of command is connected, that they are a complete shell
command line.
5. Concurrent execution of commands
GNU make
Supports 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 make
command-line options -j
or --jobs
to tell make
at the same time allows multiple commands simultaneously.
If the option -j
exists after an integer, its meaning is told make
at the same time allows the number of commands executed simultaneously row. This number is called job slots. When -j
time digital option does not appear, use the default job solts, a value of 1, indicating make
the 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,
make
only 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.