Makefile pseudo-Target

Source: Internet
Author: User

A pseudo-target is a target that does not represent a real file name. When executing make, you can specify this target to execute the rule-defined command, sometimes we can call a pseudo target a tag. There are two reasons for using a pseudo-target: 1. avoid the Target defined in our makefile to only execute commands (this objective is to execute a series of commands without creating this objective) conflicts with the actual file name in the working directory. 2. Improve the efficiency of executing make, especially for a large project, you may be equally concerned about the compilation efficiency. We will analyze and discuss the two issues as follows:

1. If we need to write such a rule: the command defined by the rule is not to create the target file, but to use make to specify a specific target to execute some specific commands. As shown below:

Clean:

RM *. O temp

 

In the rule, "RM" is not a command to create the file "clean", but to delete all. O files and temp files in the current directory. If the file "clean" does not exist in the working directory, after "make clean" is input, "RM *. O Temp" will always be executed. This is our original intention.

However, the current working directory contains the file "clean", which is different when we input "make clean. The rule does not depend on files. Therefore, the target is considered to be the latest command, instead of executing the command defined by the rule. The command "RM" will not be executed. This is not our original intention. To avoid this problem, we can explicitly declare the target "clean" as a pseudo target. To declare a target as a pseudo-target, you must use it as the dependency of the special target. Phony. As follows:

. Phony: clean

In this way, the target "clean" is a pseudo-target, regardless of whether the "clean" file exists in the current directory. Enter "make clean. All "RM" commands are executed. Besides, when a target is declared as a pseudo-target, make will not try to search for implicit rules to create this target when executing this rule. This also improves the execution efficiency of make. At the same time, we do not have to worry about the failure of our expectation due to the duplicate of the target and file name. When writing a pseudo-target rule, you must first declare that the target is a pseudo-target, and then define the rule for the pseudo-target. The target "clean" format should be as follows:

. Phony: clean

Clean:

RM *. O temp

2. Another application of pseudo targets is in the parallel and Recursive Execution of make. In this case, a variable is usually defined as all subdirectories that require make. The implementation of make for multiple directories can be completed using a shell loop in a rule. As follows:

Subdirs = Foo bar Baz

Subdirs:

For dir in $ (subdirs); do \

$ (Make)-C $ dir ;\

Done

However, this implementation method has the following problems. 1. Make will not exit when the subdirectory executes make. That is to say, after the make command fails to be executed on a directory, the make command will be executed on other directories. When the final execution fails, it is difficult to locate the directory where the makefile is incorrect according to the error prompt. This makes it very difficult to locate the problem. To avoid this problem, we can add error monitoring in the command line section and exit make after the command execution error. Unfortunately, if you use the "-k" option when executing make, this method will not work. 2. another problem is that the "make" directory parallel processing function is not used in this shell loop mode, because the rule command is a complete shell command and cannot be executed in parallel.

We can overcome the two problems in the above implementation methods through the pseudo-target method.

Subdirs = Foo bar Baz

. Phony: subdirs $ (subdirs)

Subdirs: $ (subdirs)

$ (Subdirs ):

$ (Make)-C $ @

Foo: Baz

In the preceding implementation, a rule "foo: Baz" without command lines is used to limit the make sequence of sub-directories. Before processing the "foo" directory, you must wait until the "Baz" directory is processed. When writing a makefile for executing make in parallel, you must pay special attention to the directory processing sequence.

Generally, a pseudo-target is not dependent on another target file. This is because when the dependency of a target file contains a pseudo target, the command defined by the pseudo target is executed every time this rule is executed (because it is a rule dependency, when recreating the target file of a rule, you must first recreate its dependency ). When a pseudo-target is not dependent on any target (this target is a file that can be created or already exists), we can only explicitly specify this pseudo-target using the make command line option, to execute the commands defined by it. For example, make clean ".

In makefile, pseudo targets can have their own dependencies. To create multiple executable programs in a directory, we can describe the reconstruction rules of all programs in a makefile. Because the first goal in makefile is the "ultimate goal", the agreed approach is to use a pseudo goal called "all" as the ultimate goal, and its dependent files are the programs to be created. The following is an example:

# Sample makefile

ALL: prog1 prog2 prog3

. Phony: All

Prog1: prog1.o utils. o

CC-O prog1 prog1.o utils. o

Prog2: prog2.o

CC-O prog2 prog2.o

Prog3: prog3.o sort. O utils. o

CC-O prog3 prog3.o sort. O utils. o

When executing make, the target "all" is taken as the ultimate goal. To update it, make creates (does not exist) or recreates (already exists) All dependent files (prog1, prog2, and prog3) for the target "all ). When you need to update a program separately, you can use the make command line option to specify the program to be rebuilt. (For example, "Make prog1 "). When a pseudo-target is dependent on another pseudo-target, make processes it as a sub-routine of another pseudo-target (which can be understood as follows: as a required part of another pseudo-target, function calls in C language are the same ). The following example shows the usage:

. Phony: cleanall cleanobj cleandiff

Cleanall: cleanobj cleandiff

Rm Program

Cleanobj:

RM *. o

Cleandiff:

RM *. Diff

The "cleanobj" and "cleandiff" pseudo targets are a bit like "subprograms" (the commands defined by them will be executed when the target "clearall" is executed "). Run the "make cleanall", "Make cleanobj", and "make cleandiff" commands to clear different types of files. In this example, the special target ". Phony" is used to declare multiple pseudo targets. They are separated by null, and then the rules for each pseudo target are defined.

Note:

In general, in the Command defined by the pseudo-target to clear the file, "RM" uses the option "-F" (-- force) to prevent errors and exits when the file is missing to be deleted, failed to make the "make clean" process. You can also add "-" before "RM" to prevent "RM" from exiting. In this way, make will prompt the error message but will not exit. In order not to see such annoying information, you need to use the first method described above.

In addition, make has an embedded implicit variable "RM", which is defined as "Rm = Rm-F ". Therefore, you can use the variable "$ (RM)" to replace "RM" When writing the command line of the "clean" rule. This saves unnecessary trouble! This is our recommended usage.

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.