The role of phony target in makefile

Source: Internet
Author: User
Please write a makefile at the same time compile, link the following two programs:

MAIN1.C:
#include
int main (void)
{
printf ("main1\n");
}
MAIN2.C:
#include
int main (void)
{
printf ("main2\n");
}


"Analysis": Here you need to generate two executables main1 and main2 (two destinations). Since makefile can only have one target, it is possible to construct an ultimate goal all with no rules and rely on both executables. As follows:


Makefile
All:main1 main2
main1:main1.c
@gcc Main1.c-o main1
Main2:main2.c
@gcc Main2.c-o main2

Most of the time when we execute make, we produce a lot of process files, such as changing the makefile above to:

Makefile
All:main1 main2
main1:main1.c
@gcc Main1.c-o main1
main2:main2.o
@gcc Main2.o-o main2
Main2.o:main2.c
@gcc-C main2.c

This will generate a process file main2.o that we do not need.

If you want to delete the resulting process file, add a target clean as a front:

All:main1 main2 Clean
main1:main1.c
@gcc Main1.c-o main1
main2:main2.o
@gcc Main2.o-o main2
Main2.o:main2.c
@gcc-C main2.c
Clean
@rm-F MAIN2.O


But when we make it, MAIN2.O still exists, what's going on? The role of all and phony in makefile.

There is no dependency on the target clean here, and when make executes it is considered "root" (that is, the disk has clean, like main2.c), ignoring it (although it has rules).

Key word. Phony can solve this problem and tell make that the target is "fake" (there is no clean on the disk), and that make to generate this target will execute its rules once. The goal of phony retouching is that only rules are not dependent.

Add a sentence. Phony:clean can:

All:main1 main2 Clean
main1:main1.c
@gcc Main1.c-o main1
main2:main2.o
@gcc Main2.o-o main2
Main2.o:main2.c
@gcc-C main2.c
. Phony:clean
Clean
@rm-F MAIN2.O

Attached


Phony [' Fəuni] a. Fake


GNU make makes it possible to compile and link the entire software engineering with just one command.


Terms of Makefile:


Rules: Used to describe how to generate one or more target files


Format of the rule:

Targets:prerequisites

Command

Goal: Reliance

Command

The +++++ command needs to start with the [TAB] key ++++


The phony in makefile
Phony target
The phony target is not an actual file name: it simply executes the name of the command when explicitly requested. There are two reasons to use the phony target: avoid conflicting files with the same name and improve performance.
If you write a rule that does not produce a target file, its commands are executed every time that you make the target.
For example:
Clean
RM *.O Temp
Because the "rm" command does not produce a "clean" file, the command executes every time you execute "make clean." If the "clean" file appears in the directory, the rule is invalidated: no dependent files, the file "clean" is always up-to-date, the command will never be executed; To avoid this problem, you can use ". Phony "indicates the target. Such as:
. Phony:clean
Doing so will disregard the presence or absence of the clean file.

The known phony target is not an actual file generated by another file, and make skips the implicit rule search. That's why declaring phony goals can improve performance, even if you're not worried about the actual file existence or not.
The complete example is as follows:
. Phony:clean
Clean:
RM *.O Temp
The phony target should not be a dependency on a real target file. If so, the command executes each time the make updates the file. As long as the phony target is not dependent on the true target, the rule's command executes only when the target is specified.

Phony targets can have dependencies. When there are multiple programs in a directory, it is more convenient to place them in a makefile. Because the default target is the first target in makefile, this phony target is often called "all" and its dependent files are individual programs:
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

In this way, using "make" will generate three programs. D
When a phony target is another dependency, it acts like a subroutine, for example:
. Phony:cleanall Cleanobj Cleandiff
Cleanall:cleanobj Cleandiff
RM Program
Cleanobj:
RM *.O
Cleandiff:
RM *.diff

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.