Makefile. Phony pseudo target

Source: Internet
Author: User

From: http://www.cnblogs.com/chenyadong/archive/2011/11/19/2255279.html

However, my test code makefile

cc =gcc.PHONY:cleanall:mainmain:main.c$(cc) -o $@ $<clean:rm main

Main. c

#include<stdio.h>int main(){printf("hello world \n");return 0;}

Original article:

My understanding:

Take the clean command as an example. If you define another file named clean after make is complete, the RM command will not be executed when you execute make clean.

To avoid this problem, you need. Phony: clean

 

========================================================== ========================================================== ========

The so-called pseudo-target is such a target. It does not represent a real file name. When executing make, you can specify this target to execute the command defined by its rule, sometimes we turn a pseudo target into a tag.

So what is a pseudo-target? As a beginner, we may not care about this issue. Let's take a look at when we will need it.

First, let's look at the following example:

There is only one myls1.c in the current directory. In order to allow the program to manage makefiles, a simple makefile is written as follows.

Run:

You will find that you can use this makefile to manage the current project and generate the execution file myls as scheduled.

Run make clean to delete the executable program.

Then I set up a clean file in the current directory. What is the result of this execution?

So why can't I execute it again? My makefile does not actually modify anything. Why can't I manage my project's makefile at this time.

To solve this problem, add two lines. The modified makefile is as follows:

Run the following command again:

In this way, the problem is solved. What is the specific reason?

In makefile, we can solve the above problem by using a pseudo target. Why should we use a pseudo target, to avoid name conflicts between the targets defined in makefile that only execute commands and the actual files in the working directory, the other is the efficiency of submitting and executing makefile.

First case:

If we need to write such a rule: the command defined by the rule is not to create the target file, but to explicitly specify it through the make command line to execute some special commands, just like clean in the example. When there is no clean file in the folder, we enter "make clean" to execute as expected. However, once the clean file appears in the folder, we enter "make clean" again ", because this rule does not have any dependency files, the target is considered to be the latest and does not execute the commands defined by the rule. Therefore, the RM command is not executed. To solve the problem, we define the target clean as a pseudo target.

That is, add:

. Phony: clean

If there is a clean file in the directory, you only need to enter "make clean" to execute the RM command.

When a target is declared as a pseudo-target, make will not try to find the implicit rule to create it when executing the rule. In this way, the execution efficiency of make is improved, and there is no need to worry about the duplicate Object Name and file name.

Case 2:

Another type of pseudo-target is used in the parallel and Recursive Execution of make.

Example:

Subdirs = Foo bar Baz
Subdirs:
For dir in $ (subdirs)
Do
$ (Make)-C $ dir
Done

In this case, several problems may occur:

1. Make will not exit when the subdirectory executes make;

2. When using this shell loop method, the directory parallel processing function of make is not used.

With a pseudo-target, you can solve the above two problems.

Subdirs = Foo bar Baz
. Phony: subdirs $ (subdirs)
Subdirs: $ (subdirs)
$ (Subdirs ):
$ (Make)-C $ @

Generally, a pseudo-target is not dependent on another target. When a pseudo-target is not dependent on any target, we can only use the make command to specify it as the ultimate goal of make and execute the command defined by its rule.

There is also a special pseudo-target -- all. If we create multiple executable programs in a directory, we can describe the reconstruction rules of all programs in a makefile.

ALL: P1 P2 p3
P1: p1.c
P2: p2.c
P3: p3.c

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.