Write makefile with me (11)

Source: Internet
Author: User
Tags builtin touch command

Run make

In general, the simplest thing is to directly input the make command under the command line. The make command will find the makefile in the current directory for execution, and everything is automatic. But sometimes you may just want make to recompile some files, instead of the entire project. Sometimes you have several compilation rules, and you want to use different compilation rules in different scenarios, and so on. This section describes how to use the make command.

I. Exit code of make

The make command has three exit codes:

0 -- indicates successful execution.
1 -- if any error occurs during the make operation, 1 is returned.
2 -- if you use the "-Q" option of make and make does not need to be updated for some targets, 2 is returned.

Relevant Parameters of make will be described in subsequent chapters.

Ii. Specify makefile

As we mentioned earlier, the GNU make rule for finding the default makefile is to find three files in the current directory in sequence: "gnumakefile", "makefile", and "makefile ". These three files are searched in order. Once found, the files are read and executed.

Currently, you can also specify a makefile with a special name for the make command. To achieve this function, we need to use the "-F" or "-- file" parameter of make (the "-- makefile" parameter also works ). For example, if we have a makefile named "hchen. mk", we can let make execute this file as follows:

Make-F hchen. mk

If you use the "-F" parameter more than once on the make command line, all specified makefiles will be connected and passed to make for execution.

3. Specify the target

In general, the final goal of make is the first goal in makefile, while other goals are generally associated with this goal. This is the default act of make. Of course, in general, the first goal in your makefile is composed of many goals. You can instruct make to accomplish the target you specified. To achieve this goal, it is very easy to directly follow the target name after the make command (as mentioned above in the "make clean" form)

Any target in makefile can be specified as the ultimate goal, but except for the "-" header or the target that contains "=", because the target has these characters, it will be parsed into command line parameters or variables. Even targets that are not clearly written by US can become the ultimate goal of make. That is to say, as long as make can find its implicit rule to deduce rules, this implicit goal can also be designated as the ultimate goal.

There is a make environment variable named "makecmdgoals", which stores the list of your specified ultimate goals. If you do not specify a target on the command line, then, this variable is null. This variable can be used in some special cases. For example:

Sources = Foo. c bar. c
Ifneq ($ (makecmdgoals), clean)
Include $ (sources:. c =. d)

Based on the above example, as long as the input command is not "make clean", makefile automatically contains the makefiles "foo. D" and "bar. D.

The method of specifying the ultimate goal allows us to easily compile our program, for example, the following example:

. Phony: All
ALL: prog1 prog2 prog3 prog4

In this example, we can see that the makefile contains four programs to be compiled: "prog1", "prog2", "prog3", and "prog4 ", we can use the "make all" command to compile all targets (if all is set to the first target, we only need to execute "make "), we can also use "make prog2" to compile the target "prog2" separately ".

Although make can specify the targets in all makefiles, it also includes "pseudo targets". Therefore, we can let our makefile accomplish different tasks based on the specified targets. In the Unix world, when software is released, especially when open-source software such as GNU is released, its makefile contains functions such as compilation, installation, and packaging. We can refer to this rule to write the goal in our makefile.

This pseudo-goal is the goal of all goals, and its function is generally to compile all goals.
This pseudo-Target Function deletes all files created by make.
This pseudo-target function is used to install compiled programs. In fact, it is used to copy the target execution file to the specified target.
This pseudo-target function is used to generate the changed source file.
This pseudo-target function is to package and back up the source program. That is, a tar file.
This pseudo-target function is used to create a compressed file. Generally, it is used to compress the tar file into a Z file. Or GZ file.
This pseudo-Target Function updates all targets for full re-compilation.
"Check" and "test"
These two pseudo targets are generally 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, GNU's idea of these things must have its merits (you will find these functions useful when there are many program files in your UNIX environment, if you want to write such a function, it is best to use this name to name your target. In this way, there are some specifications, and the advantage of the specifications is that you do not need to explain them. In addition, if your makefile has these functions, one is very practical, and the other is very professional (not the kind of work for beginners ).

Iv. Check rules

Sometimes, we don't want to execute the rules in our makefile. We just want to check our commands or execution sequences. Therefore, we can use the following parameters of the make command:

"-- Just-print"
"-- Dry-run"
"-- Recon"
If you do not execute parameters, these parameters only print commands, regardless of whether the target is updated or not, and print the commands under the rules and associated rules, but do not execute them. These parameters are very useful for debugging makefile.

"-- Touch"
This parameter means to update the Time of the target file without changing the Time of the target file. That is to say, make pretends to be the compilation target, but it is not the actual compilation target. It just changes the target to a compiled state.

"-- Question"
The behavior of this parameter is to find the target. That is to say, if the target exists, nothing will be output, and of course no compilation will be executed. If the target does not exist, it prints an error message.

"-W <File>"
"-- What-if = <File>"
"-- Assume-New = <File>"
"-- New-file = <File>"
This parameter specifies a file. Generally, it is a source file (or dependent file). Make will run the command dependent on this file according to Rule derivation. Generally, it can be used with the "-n" parameter, to view the rule commands for this dependent file.

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

V. Make Parameters

The following lists all the parameter definitions of GNU make 3.80. Other versions are similar to the Make version of the manufacturer. For details about the parameters of make made by other manufacturers, refer to their product documentation.

The two parameters are used to ignore the compatibility with other versions of make.

"-- Always-make"
All goals must be updated (re-compiled ).

"-C <dir>"
"-- Directory = <dir>"
Specifies the directory for reading makefile. If multiple "-c" parameters exist, make interprets the following paths as relative paths and uses the last directory as the specified directory. For example, "make-C ~ Hchen/test-C prog "is equivalent to" make-C ~ Hchen/test/prog ".

"-Debug [= <Options>]"
Output the debugging information of make. It has several different levels to choose from. If there is no parameter, It is the simplest debugging information to output. The values of <Options> are as follows:
A -- that is, all, output all debugging information. (There will be a lot)
B -- that is, basic. Only simple debugging information is output. That is, the output does not need to be re-compiled.
V -- that is, verbose, above the level of option B. The output information includes the makefile to be parsed, dependent files (or dependent targets) that do not need to be recompiled.
I -- that is, implicit, output the implicit rules.
J -- that is, jobs, which outputs detailed Command Information in the execution rule, such as the command PID and return code.
M -- that is, makefile. Output make to read makefile, update makefile, and execute makefile.

It is equivalent to "-- DEBUG = ".

"-- Environment-overrides"
Specifies that the value of the environment variable overwrites the value of the variable defined in makefile.

"-F = <File>"
"-- File = <File>"
"-- Makefile = <File>"
Specifies the makefile to be executed.

"-- Help"
Displays help information.

"-- Ignore-errors"
Ignore all errors during execution.

"-I <dir>"
"-- Include-Dir = <dir>"
Specify a search Target containing makefile. You can use multiple "-I" parameters to specify multiple directories.

"-J [<jobsnum>]"
"-- Jobs [= <jobsnum>]"
The number of running commands at the same time. Without this parameter, make can run as much as it runs. If there is more than one "-J" parameter, only the last "-J" is valid. (Note that this parameter is useless in the MS-DOS)

"-- Keep-going"
Or stop running. If a target fails to be generated, the target dependent on it will not be executed.

"-L <load>"
"-- Load-average [= <load]"
"-Max-load [= <load>]"
Specify the load of the make command.

"-- Just-print"
"-- Dry-run"
"-- Recon"
Only the command sequence in the execution process is output, but not executed.

"-O <File>"
"-- Old-file = <File>"
"-- Assume-old = <File>"
Do not regenerate the specified <File>, even if the target dependent file is new to it.

"-- Print-data-base"
Output all data in makefile, including all rules and variables. This parameter will allow a simple makefile to output a bunch of information. If you only want to output information and do not want to execute makefile, you can use the "make-QP" command. If you want to view the preset variables and rules before executing makefile, you can use "make-p-F/dev/null ". The output information of this parameter will contain the name and row number of your MAKEFILE file. Therefore, it is very useful to use this parameter to debug your makefile, especially when your environment variables are complex.

"-- Question"
Do not run or output commands. It only checks whether the specified target needs to be updated. If the value is 0, update is required. If the value is 2, an error occurs.

"-- No-builtin-rules"
Disable make from using any implicit rules.

"-- No-builtin-variabes"
Make is prohibited from using any implicit rules acting on variables.

"-- Silent"
"-- Quiet"
When running a command, no command output is output.

"-- No-keep-going"
"-- Stop"
Disable the "-k" option. Sometimes, make options are inherited from the environment variable "makeflags. Therefore, you can use this parameter in the command line to invalidate the "-k" option in the environment variable.

"-- Touch"
It is equivalent to the Unix touch command. It only changes the modification date of the target to the latest version, that is, it prevents the generation of the target command from running.

"-- Version"
Output The make program version, copyright, and other information about make.

"-- Print-directory"
Output information before and after running makefile. This parameter is useful for tracking nested call of make.

"-- No-print-directory"
Disable the "-W" option.

"-W <File>"
"-- What-if = <File>"
"-- New-file = <File>"
"-- Assume-file = <File>"
Assume that the target <File> needs to be updated. If it is used with the "-n" option, this parameter will output the running action when the target is updated. If there is no "-n", the modification time of <File> is set to the current time, just like running the Unix "Touch" command.

"-- Warn-undefined-variables"
As long as make finds that there are undefined variables, it will output warning information.

<-Previous PageNext page->

(All Rights Reserved. Please indicate the author and source when reprinting)


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.