Linux Makefile Tutorial make run eight [go]

Source: Internet
Author: User
Tags builtin touch command

Run of Make

——————

In general, the simplest is to enter the make command directly at the command line, which will find the makefile of the current directory to execute, and everything is automatic. But sometimes you might just want make to recompile some files, not the whole project, and then 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.

First, make the exit code

After the make command executes, there are three exit codes:

0--indicates successful execution.

If any error occurs during make run, it returns 1.

2--If you are using the "-q" option of make, and making makes some targets do not need to be updated, then return 2.

The relevant parameters of make are described in the following sections.

II. Designation of Makefile

As we said before, GNU make is looking for the default makefile rule to find three files in the current directory--"Gnumakefile", "Makefile" and "makefile". It finds the three files sequentially and, once found, begins to read the file and execute it.

Currently, we can also assign a special name to the Make command makefile. To achieve this, we use the "-F" or "--file" parameter of Make (the "--makefile" parameter is also OK). For example, we have a makefile name of "Hchen.mk", so we can let make to execute this file:

Make–f hchen.mk

If the command line for make is, you have used the "-F" parameter more than once, then all the specified makefile will be passed together to make execution.

III. Designation of objectives

In general, the final 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 have specified. To achieve this, it is easy to do so with the name of the target immediately after the make command (as mentioned in the "Make clean" form above)

Any goal in Makefile can be specified as the ultimate goal, but in addition to the "-" heading, or containing the "=" target, because the target of these characters, will be parsed into command-line arguments or variables. Even a goal that is not explicitly written by us can be the ultimate goal of make, meaning that if make can find its implicit rule derivation rules, then this implicit goal can also be designated as the ultimate goal.

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

Sources = foo.c bar.c

Ifneq ($ (makecmdgoals), clean)

Include $ (SOURCES:.C=.D)

endif

Based on the above example, the makefile will automatically contain the two makefile of "FOO.D" and "BAR.D" as long as we enter a command other than "Make clean".

Using a method that specifies the ultimate goal is a convenient way for us to compile our program, for 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 targets (if all is set to the first target, We can also use "make prog2" to compile the target "prog2" separately.

While make can specify all targets in the makefile, it also includes "pseudo-targets", so that we can allow our makefile to accomplish different things according to the different targets specified. In the Unix world, when software is released, especially when the GNU Open Source software is released, its makefile includes the functions of compiling, installing, packaging and so on. We can refer to this rule to write the goals 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 function is to delete all files created by make.

"Install"

This pseudo-target function is to install a compiled program, in fact, to copy the target execution files to the specified target.

"Print"

The function of this pseudo-target is to sample the changed source files.

"Tar"

This pseudo-target function is to package the source program for backup. That is, a tar file.

"Dist"

This pseudo-target function is to create a compressed file, which typically presses the tar file into a Z file. or GZ files.

"TAGS"

This pseudo-target function is to update all targets for full recompilation.

"Check" and "Test"

These pseudo-targets are typically used to test the makefile process.

Of course, the makefile of a project does not have to write such a goal, these things are gnu things, but I think that the GNU to make these things must have its merits (and so on your UNIX program files, you will find that these functions are useful), here is just the explanation, If you want to write this function, it is best to use the name of your goal, so the specification, the advantage of the norm is-no explanation, we all understand. And if you have these features in the makefile, one is very practical, the second is that you can appear to be makefile very professional (not the kind of beginner's works).

Iv. Rules of Inspection

Sometimes we don't want the rules in our makefile to execute, we just want to check our commands, or the sequence of executions. 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 the Print command, regardless of whether the target is updated, the rules and associated rules of the command to print out, but do not execute, these parameters for our debugging makefile very useful.

"-T"

"--touch"

The meaning of this parameter is to update the time of the target file, but not to change the target file. In other words, make pretends to compile the target, but not the actual compilation target, just turns the target into a compiled state.

"-Q"

"--question"

The behavior of this parameter is to find the target meaning, that is, if the target exists, then it will not output, of course, do not compile, 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 dependent on this file based on the rule derivation, and, in general, can be used with the "-n" parameter to view the rule commands that occur with this dependent file.

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

V. Parameters of Make

The parameter definitions for all GNU make version 3.80 are listed below. Other versions are similar to manufacturers ' make, but the specific parameters of other manufacturers ' made will refer to their respective product documentation.

"-B"

"-M"

The purpose of these two parameters is to ignore compatibility with other versions of make.

"-B"

"--always-make"

It is considered that all targets need to be updated (recompiled).

"-C <dir>"

"--directory=<dir>"

Specifies the directory to read makefile. If there are multiple "-c" arguments, make is interpreted as a trailing path with the previous as the relative path and the last directory as the specified directory. such as: "Make–c ~hchen/test–c prog" is equivalent to "Make–c ~hchen/test/prog".

"-DEBUG[=<OPTIONS>]"

Outputs debug information for make. It has several different levels to choose from, and if there are no parameters, the simplest debugging information is output. Here is the value of <options>:

a--, which is all, outputs all debugging information. (It's going to be very much)

b--, which is basic, only outputs simple debugging information. That is, the output does not require a target to recompile.

v--is verbose, above the level of the B option. The output information includes which makefile is parsed, does not need to be recompiled dependent files (or dependent targets), etc.

i--is also the implicit, the output of the implied rules.

j--, which is jobs, outputs details of the commands in the execution rules, 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 be executed.

"-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 contains makefile. Multiple "-i" parameters can be used to specify multiple directories.

"-j [<jobsnum>]"

"--JOBS[=<JOBSNUM>]"

Refers to the number of simultaneous commands that are run. If you do not have this parameter, you can run as much as 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"

The error does not stop running. If you fail to generate a target, 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"

Only the sequence of commands in the execution process is output, but not executed.

"-O <file>"

"--old-file=<file>"

"--assume-old=<file>"

Does not regenerate the specified <file>, even if the dependent file for this target is new to it.

"-P"

"--print-data-base"

Outputs all the data in the makefile, including all rules and variables. This parameter will let a simple makefile output a bunch of information. You can use the "MAKE-QP" command if you just want to output information and do not want to perform makefile. If you want to see the preset variables and rules before executing makefile, you can use "Make–p–f/dev/null". The output of this parameter will contain the file name and line number of your makefile file, so it would be useful to use this parameter to debug your makefile, especially if your environment variable is complicated.

"-Q"

"--question"

Do not run the command, nor output it. Simply check that the specified target needs to be updated. If it is 0, the description is to be updated, and if 2 it indicates an error occurred.

"-R"

"--no-builtin-rules"

Prohibit make to use any implied rules.

"-R"

"--no-builtin-variabes"

Prohibit make to use any implied rule that acts on a variable.

"-S"

"--silent"

"--quiet"

Output of the command is not output when the command is run.

"-S"

"--no-keep-going"

"--stop"

Cancels the function of the "-k" option. Because sometimes the make option is inherited from the environment variable "makeflags". So you can use this parameter on the command line to invalidate the "-k" option in the environment variable.

"-T"

"--touch"

The equivalent of a UNIX touch command, which only changes the target's modification date to the latest, which is to prevent the command that generated the target from running.

"-V"

"--version"

Output the version of 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 calls to make.

"--no-print-directory"

The "-w" option is prohibited.

"W <file>"

"--what-if=<file>"

"--new-file=<file>"

"--assume-file=<file>"

Suppose the target <file> needs to be updated, and if used with the "-n" option, then this parameter will output the run action when the target is updated. If there is no "-n" then it is like running the UNIX "touch" command, making the <file> modification time the current time.

"--warn-undefined-variables"

Whenever make discovers an undefined variable, it outputs a warning message.

Related Article

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.