Write Makefile with me (11)

zhaozj2021-02-08  297

Make's run ------

In general, the easiest is to enter the make command directly on the command line, and the make command will be executed by the current directory Makefile, everything is automatic. But sometimes you may just want Make to recompile certain files, not the entire project, and sometimes you have several compilation rules, you want to use different compilation rules in different times, and so on. This chapter is to tell how to use the make command.

First, Make's exit code

There are three exit code after the board command is executed:

0 - means successfully executed. 1 - Any error occurs if the Make is running, it returns 1. 2 - If you use the "-q" option of make, and make make some targets don't need to be updated, then returns 2.

Make's related parameters we will tell in subsequent chapters.

Second, specify Makefile

As we said, GNU Make finds the default Makefile rule to find three files in the current directory - "gnumakefile", "makefile", and "makefile". It is found in order to find these three files, once found, start reading this file and execute.

Currently, we can also assign a special name for Makefile to the Make command. To achieve this feature, we want to use the "-f" or "-file" parameter ("--makefile" parameter). For example, we have a Makefile name is "hchen.mk", then we can let Make to perform this file:

Make -f hchen.mk

If the command line in Make is, you use the "-f" parameter at once, then all specified Makefile will be delivered together to Make.

Third, designated goals

In general, MAKE's ultimate goal is the first target in Makefile, while other goals are generally brought by this target. This is the default behavior of Make. Of course, in general, your Makefile's first goal is made up of many targets, you can indicate Make to complete the target you specify. To achieve this, it is very simple. You need to direct the name directly with the target after the make command (as shown in the "Make Clean" form mentioned above)

Any goals in makefile can be specified as the ultimate target, but except "-" heads, or contains "=" target, because there are targets of these characters, they are parsed into command line parameters or variables. Even the goals we have clearly written can also become the ultimate goal of Make, that is, as long as Make can find its implicit rules, this implicit target can also be specified as the ultimate goal.

There is a Make environment variable called "makecmdgoals". The list of the ultimate target you specify is stored in this variable. If you don't specify the target, this variable is null. This variable allows you to use in some particular situations. For example, the example below:

Sources = foo.c bar.c ifneq ($ (Makecmdgoals), Clean) Include $ (Sources: .c = .d) Endif

Based on this example, as long as the command we entered is not "Make Clean", Makefile will automatically contain two Makefiles "foo.d" and "bar.d".

Methods using the specified ultimate goal can easily compile our programs, such as the following example:

.Phony: All All: PROG1 PROG2 PROG3 PROG4

From this example, we can see that there are four programs that need to be compiled - "PROG1", "PROG2", "PROG3", and "PROG4", we can compile all the "make all" commands. Goal (if the AL is set to the first goal, simply perform "make"), we can also use "make prog2" to compile the target "PROG2" separately. Often Make can specify all the goals in all makefile, then "pseudo-target", so we can make our makefile to complete different things based on this nature. In UNIX world, when the software is released, especially the release of this open source software, its Makefile contains compilation, installation, packaging and other functions. We can refer to this rule to write the goals in our makefile.

"All" pseudo-objective is the goal of all goals, and its feature is generally compiled all the goals. "Clean" pseudo-objective function is to delete all files created by Make. "Install" pseudo-objective function is to install the compiled program, in fact, copy the target execution file to the specified target. "Print" The function of this pseudo-target is to illustrate the changed source file. "TAR" pseudoactive function is to package the source package backup. That is a TAR file. "DIST" pseudo-target function is to create a compressed file, which is generally pressed into the z file. Or GZ file. "Tags" pseudo-objective function is to update all the goals for complete recompilation. "Check" and "Test" are generally used to test the process of Makefile.

Of course, a project's makefile does not have to write such a goal, these things are gnu's things, but I think the GNU has a certain thing (waiting for the program files under your UNIX. You will find these features very useful), it is just a description. If you want to write this function, it is best to use this name to name your goals, so that some, the normative benefits is - do not explain, Everyone understands. And if you have these features in your makefile, one is very practical, and the other is that you can look very professional (not the work of the beginner).

Fourth, check rules

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

"-N" "--just-print" "--dry-run" "--Recon" does not perform parameters, these parameters are just print commands, regardless of whether the target is updated, print the rules and the commands under the joint rules, but Do not execute, these parameters are very useful for us to debug Makefile.

"-T" "--Touch" means that the time update of the target file, but does not change the target file. That is, Make pretend to be compiled, but is not a real compilation target, just turn the target into a compiled state.

The behavior of "-q" "--question" is the meaning of finding the target, that is, if the target exists, then what else does not output, and of course, it will not perform compilation, if the target does not exist, it will print An error message.

"-W " --what-if = "" --ness-new = "" --new-file = "This parameter needs to specify a file. Generally, the source file (or dependent file), Make will run according to the rule, in general, in general, can be used with the "-n" parameter to view the rule command that occurred in this dependency file. Another interesting usage is to combine "-P" and "-v" to output the information when Makefile is executed (this will be described later).

V. Make parameters

The parameters definitions of all GNU Make version 3.80 are listed below. Other versions of the MAKE of the manufacturer are similar, but the specific parameters of other manufacturers make specific parameters, please refer to the respective product documents.

The role of "-b" "- m" these two parameters is to ignore compatibility with other version of Make.

"-B" "- always-make" thinks that all goals need to be updated (recompile).

"-C

" "- directory = " Specifies the directory to read the Makefile. If there is a plurality of "-c" parameters, Make's interpretation is the rear path as the relative path as the previous directory as the specified directory. Such as: "make -c ~ hchen / test -c prog" equivalent to "make -c ~ hchen / test / prog".

"-Debug [= ]" outputs the debug information of MAKE. It has several different levels to choose from, if there is no parameters, that is, the simplest debugging information is output. The following is the value of : a - that is, all, output all debugging information. (It will be very much) B - that is, Basic, only output simple debugging information. That is, the output does not require recompile. V - is also verbose, above the level of B option. The output information includes which makefile is parsed, does not need to be recompiled dependent file (or dependent target), and the like. I - that is, Implicit, the implicit rule is output. J - is also the JOBS, the output execution rules, such as the PID, return code, etc. of the command. M - is Makefile, output Make Read Makefile, update makefile, and executes Makefile information.

"-D" is equivalent to "--debug = a".

"-E" "- Environment-overrides" indicates the value of the value of the environment variable overrides the variable defined in the Makefile.

"-F = " "- file = " - Makefile = "Specifies the Makefile you need to do.

"-H" "- help" displays help information.

"-I" "- ignore-errors" ignores all errors during execution.

"-I

" "- INCLUDE-DIR = " Specifies a search target that is included in the Makefile. You can use multiple "-i" parameters to specify multiple directories.

"-J []" "" "" means the number of running commands simultaneously. If there is no this parameter, how much can be run when Make runs the command. If there is more than one "-j" parameter, then only the last "-j" is valid. (Note this parameter is useless in MS-DOS)

"-K" "- keep-going" error does not stop running. If generated a target fail, it will not be executed if the goal is dependent on it. "-L " "- max-load [= ]" Specifies the load of the Make running command.

"-N" "- Just-print" "- DRY-RUN" "- Recon" only outputs the command sequence during execution, but does not execute.

"-O " - Old-file = "" - Assume-old = "does not regenerate the specified , even if the dependent file of this goal is new in it.

"-P" "- print-data-based" outputs all data in Makefile, including all rules and variables. This parameter will make a simple makefile output a bunch of information. If you just want to output information, you can use the "Make -QP" command without performing Makefile. If you want to see the preset variables and rules before performing makefile, you can use "make -f / dev / null". The information of this parameter output will contain the file name and line number of your makefile file, so use this parameter to debug your makefile will be very useful, especially when your environment variable is very complicated.

"-Q" "- question" does not run the command, nor does it output. Just check if the specified target needs to be updated. If it is 0, it means to update, if it is 2, an error occurs.

"-R" "- no-builtin-rules" is forbidden to use Make to use any implicit rules.

"-R" "- no-builtin-variabes" prohibits make from using any implicit rules that do on variables.

"-S" "- Silent" - Quiet "does not output the output of the command at the command.

"-S" "- no-keyp-going" "- STOP" cancels the "-k" option. Because some, make inherited from the environment variable "makeflags". So you can use this parameter in the command line to make the "-k" option in the environment variables failed.

"-T" "- touch" is equivalent to Unix's touch command, just turning the dates of the target into the latest, that is, blocking the command to generate the target from running.

"-V" "- version" outputs the version of the Make program, copyright, etc. About Make.

"-W" "- print-directory" outputs information before and after running Makefile. This parameter is useful for tracking nested calls Make.

"--NO-print-Directory" prohibits "-w" option.

"-W " - What-if = "" - new-file = "" - Assume-file = "Assume the target need to update, if and "-N" option is used, then this parameter outputs the running action when the target update is output. If there is no "-n", just like the "touch" command running UNIX, the modification time of is the current time.

"--Warn-undefined-variables" As long as Make finds an undefined variable, then the warning message is output.

<- Previous Next -> (All rights reserved, please indicate the author and source)

转载请注明原文地址:https://www.9cbs.com/read-1131.html

New Post(0)