Write with Me Makefile (11) _makefile

Source: Internet
Author: User
Tags builtin gz file

Make's Run
——————

In general, the easiest thing to do is to enter the make command directly at the command line, and the make command will find the makefile of the current directory and everything is automatic. But sometimes you just want make to recompile some files instead of the whole project, and sometimes you have several sets of compilation rules, you want to use different compilation rules at different times, and so on. This section describes how to use the Make command.

One, make exit code

The make command has three exit codes after execution:

0--represents a successful execution.
1--returns 1 if any errors occur while make is running.
2--If you use the "-q" option of make and made so that some goals do not need to be updated, then return 2.

The related parameters of make are described in the following chapters.


II. Designation of Makefile

As we said earlier, the rule for GNU make to find the default makefile is to find three files in turn in the current directory--"Gnumakefile", "Makefile" and "makefile". It finds these three files sequentially and, once found, starts reading the file and executes it.

Currently, we can also assign a special name to the Make command makefile. To achieve this, we use make's "-F" or "--file" parameters ("--makefile" parameters are also OK). For example, we have a makefile whose name is "Hchen.mk", so that we can allow make to execute this file:

Make–f hchen.mk

If you have used the "-f" argument more than once at the command line on make, all specified makefile will be connected to make execution.


III. Designation of targets

In general, the ultimate goal of make is the first goal in Makefile, and other goals are generally associated with this goal. This is the default behavior for make. Of course, in general, the first goal in your makefile is made up of a number of goals, and you can instruct make to accomplish the goals you specify. To achieve this, it is simple to do so directly after the make command with the name of the target (as mentioned in the "Made Clean" form)

Any target in the makefile can be specified as the ultimate goal, but except for the "-" or "=" target, because the target of these characters is parsed into command line arguments or variables. Even goals that are not explicitly written by us can be the ultimate goal of make, that is, as long as make can find its implied rule derivation rules, the implied target can also be designated as the ultimate goal.

There is a make environment variable called "Makecmdgoals", which holds a list of the ultimate goals you specify, and if you do not specify a target on the command line, then the variable is null. This variable allows you to use it in some of the more specific situations. For example, the following examples:

Sources = foo.c bar.c
Ifneq ($ (makecmdgoals), clean)
Include $ (SOURCES:.C=.D)
endif

Based on the above example, as long as we enter the command is not "make clean", then makefile will automatically contain "FOO.D" and "BAR.D" these two makefile.

Using the method that specifies the ultimate goal makes it easy for us to compile our program, such as the following example:

. Phony:all
All:prog1 prog2 Prog3 Prog4

From this example, we can see that there are four programs in this makefile that need to be compiled--"Prog1", "prog2", "Prog3" and "Prog4", and we can use the "make all" command to compile all the goals (if you set all to the first goal, You can also use "make prog2" to compile the target "prog2" separately by simply executing "make".

So that make can specify the target in all makefile, then also include "pseudo target", so we can according to this nature to let our makefile according to specify different target to accomplish different things. In the Unix world, when software is released, especially when the GNU Open-source Software is released, its makefile includes the ability to compile, install, and package. We can refer to this rule to write the goal in our makefile.

     All
        This pseudo target is the goal of all goals, and its function is generally to compile all the goals.
     Clean
        This pseudo target feature is to delete all files created by make.
     "Install"
        this pseudo target feature is to install a compiled program. In fact, the goal is to copy the file to the specified target. The
     "print"
        function of this pseudo target is an example of a changed source file.
     tar
        This pseudo target feature is to package the source program back up. Which is a tar file.
     "Dist"
        this pseudo target feature is to create a compressed file, The tar file is generally pressed into a Z file. or GZ file.
     "TAGS"
        this pseudo target feature is to update all targets for full recompilation.
     "Check" and "Test"
        These two pseudo targets are commonly used to test makefile processes.

Of course, the makefile of a project does not necessarily have to write such a goal, these things are GNU stuff, but I think GNU has some good things to do with it (you'll find these features useful when you have a lot of program files under your Unix), and here's just a description, If you want to write this feature, it's best to name your goal by naming it this way, so the code, the good of the spec, is--no explanation, everyone knows. And if you have these features in your makefile, one is very practical, and the second is that you can make your makefile very professional (not that kind of beginner's work).


Iv. Inspection Rules

Sometimes we don't want the rules in our makefile to be executed, we just want to check our command, or execute the sequence. So we can use the following parameters of the make command:

"-N"
"--just-print"
"--dry-run"
"--recon"
Do not execute parameters, these parameters are just print commands, regardless of whether the target is updated, the rules and the commands under the joint rules are printed, but not executed, these parameters for our debugging makefile very useful.

"-T"
"--touch"
The meaning of this parameter is to update the target file's time without changing the target file. In other words, make pretends to compile the target, but not the actual compilation target, but the target into a compiled state.

"-Q"
"--question"
The behavior of this parameter is to find the meaning of the target, that is, if the target exists, then it will not output anything, of course, will not execute the compilation, if the target does not exist, it will print an error message.

"-W <file>"
"--what-if=<file>"
"--assume-new=<file>"
"--new-file=<file>"
This parameter requires a file to be specified. Typically, a source file (or dependent file), make will run a command that relies on this file based on rule derivation, and can generally be used with the "-n" parameter to view the rule commands that occur with this dependent file.

Another interesting use is to combine "-P" and "-V" to output the information that makefile is executing (this will be described later).


Five, make parameters

The following is a list of the parameters defined for all GNU make 3.80 editions. Other versions and production of Make the same, but other producers of the specific parameters or please refer to their product documentation.

"-B"
"-M"
The effect of these two parameters is to ignore compatibility with other version make.

"-B"
"--always-make"
Think all goals need to be updated (recompiled).

"-C <dir>"
"--directory=<dir>"
Specifies the directory where the makefile is read. If there is more than one "-c" argument, make is explained by the following path as the relative path to the previous, and the specified directory as the last directory. For example: "Make–c ~hchen/test–c prog" is equivalent to "Make–c ~hchen/test/prog".

"-DEBUG[=<OPTIONS>]"
Output debug information for make. It has several different levels to choose from, and if there are no parameters, it is the easiest debugging information to output. Here is the value of <options>:
a--is all, outputting all debugging information. (will be very much)
b--is basically the output of simple debugging information. That is, the output does not need to recompile the target.
v--is the verbose, above the B-option level. The information in the output includes which makefile is parsed, the dependent files that do not need to be recompiled (or dependent targets), and so on.
i--is the implicit, the output of the implied rules.
j--is jobs, the output executes the details of the commands in the rule, such as the PID of the command, the return code, and so on.
m--is makefile, output make reads makefile, updates makefile, executes makefile information.

"-D"
Equivalent to "--debug=a".

"-E"
"--environment-overrides"
Indicates that the value of the environment variable overrides the value of the variable defined in makefile.

"-f=<file>"
"--file=<file>"
"--makefile=<file>"
Specifies the makefile to perform.

"-H"
"--help"
Displays help information.

"-I."
"--ignore-errors"
All errors are ignored at execution time.

"-I <dir>"
"--include-dir=<dir>"
Specifies a search target that is contained makefile. Multiple directories can be specified using multiple "-i" parameters.

"-j [<jobsnum>]"
"--JOBS[=<JOBSNUM>]"
Refers to the number of simultaneous commands running. Without this parameter, make runs as much as you can when you run the command. If there is more than one "-j" parameter, then only the last "-j" is valid. (Note that this parameter is useless in MS-DOS)

"K"
"--keep-going"
An error does not stop running. If a target fails to build, then the target on which it is dependent will not be executed.

"-L <load>"
"--load-average[=<load]"
"-MAX-LOAD[=<LOAD>]"
Specifies the load of the make Run command.

"-N"
"--just-print"
"--dry-run"
"--recon"
Outputs only the sequence of commands during execution, but does not execute.

"-O <file>"
"--old-file=<file>"
"--assume-old=<file>"
Does not regenerate the specified <file>, even if the target's dependent file is new to it.

"-P"
"--print-data-base"
Outputs all the data in the makefile, including all the rules and variables. This parameter will allow a simple makefile to output a bunch of information. If you just want to output information and do not want to perform makefile, you can use the "MAKE-QP" command. If you want to see the preset variables and rules before makefile, you can use "Make–p–f/dev/null". This parameter outputs information that contains the file name and line number of your makefile file, so it is useful to debug your makefile with this parameter, especially if your environment variables are complex.

"-Q"
"--question"
Do not run the command, nor output. Just check to see if the specified target needs to be updated. If it is 0, it is updated, and if it is 2 it indicates an error occurred.

"-R"
"--no-builtin-rules"
Prohibit make from using any implied rules.

"-R"
"--no-builtin-variabes"
Prevent make from using any implied rules that apply to variables.

"-S"
"--silent"
"--quiet"
The output of the command is not output when the command is run.

"-S"
"--no-keep-going"
"--stop"
Cancels the role of the "-k" option. Because sometimes the option to make is inherited from the environment variable "makeflags". So you can use this argument on the command line to invalidate the "k" option in the environment variable.

"-T"
"--touch"
The equivalent of Unix touch commands, only to change the target date to the latest, that is, to prevent the command to build the target run.

"-V"
"--version"
Output the version of the Make program, copyright, and other information about make.

"-W"
"--print-directory"
Outputs the information before and after running makefile. This parameter is useful for tracking nested call make.

"--no-print-directory"
Disables the "-w" option.

"-W <file>"
"--what-if=<file>"
"--new-file=<file>"
"--assume-file=<file>"
Assuming that the target <file> needs to be updated, and if the "-n" option is used, this parameter outputs the running action when the target is updated. If you do not have "-n" then it is the same as running the "touch" command on Unix, making the <file> modification time the current time.

"--warn-undefined-variables"
As soon as make discovers that there are undefined variables, the warning message is output.


Source: http://blog.csdn.net/haoel/article/details/2896

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.