Project makefile (4): pseudo target

Source: Internet
Author: User
[Copyright statement: reprinted. Please retain the Source: blog.csdn.net/gentleliu. Mail: shallnew at 163 dot com]

In general, makefile has a clean object, which is used to clear binary files generated during compilation. The Clean target is used in makefile in Section 1. The target does not have any dependent files, and the clean file will not be produced after the corresponding command is executed.

For a specific purpose, the commands defined by its rules are not used to create files, it is called a pseudo-target that only uses make to specify a target to execute certain system commands or rules with its dependency as the target (such as all.

A makefile generally does not have only one pseudo target. If you use the "Hidden Rules" of makefile and its common names, in makefiles of large projects, the following are commonly used targets:

ALL: execute major compilation tasks, which are usually used as the default target and placed at the beginning.

Install: run the compiled installation and copy the executable files, configuration files, and documents to different installation directories.

Clean: Delete the compiled binary file.

Distclean: delete all intermediate files except source files, such as configuration files and documents.

Help: print the help information of the current makefile, for example, which targets can be executed by using make.

.

 

When makefile is processed, make first reads all rules and establishes a dependency graph. The execution starts from the default target (the first target) or the specified target. Targets such as clean and tags are generally not used as default targets, and do not have any dependencies with the default targets. Therefore, make cannot generate its dependencies and decide whether to execute them. Therefore, to execute such a target, you must display the specified make target. Just like the intermediate binary file that we have already produced, we need to display the execution command: Make clean.

The pseudo-target can also be used as the default target (such as all), and the dependent files can be specified for it.

First, we need to complete the makefile of version 1.0. We can add help information, tags, and other functions.

# A common Makefile for c programs, version 1.1# Copyright (C) 2014 shallnew \at 163 \dot com CFLAGS += -g -Wall -Werror -O2CPPFLAGS += -I. -I./incLDFLAGS += -lpthread # SRC_OBJ = $(patsubst %.c, %.o, $(wildcard *.c))SRC_FILES = $(wildcard *.c)SRC_OBJ = $(SRC_FILES:.c=.o)SRC_BIN = target_bin all : $(SRC_BIN) $(SRC_BIN) : $(SRC_OBJ)>---$(CC) -o [email protected] $^ $(LDFLAGS) obj : $(SRC_OBJ) tags:>---ctags -R help:>[email protected] "===============A common Makefile for cprograms==============">[email protected] "Copyright (C) 2014 liuy0711 \at 163 \dotcom">[email protected] "The following targets are support:">[email protected]>[email protected] " all             - (==make) compile and link">[email protected] " obj             - just compile, without link">[email protected] " clean           - clean target">[email protected] " distclean       - clean target and otherinformation">[email protected] " tags            - create ctags for vim editor">[email protected] " help            - print help information">[email protected]>[email protected] "To make a target, do 'make [target]'">[email protected] "========================= Version 1.1=======================" # clean targetclean:>---$(RM) $(SRC_OBJ) $(SRC_BIN) $(SRC_BIN).exe distclean:>---$(RM) $(SRC_OBJ) $(SRC_BIN) $(SRC_BIN).exe tags *~ 

Make will print the executed command on the screen. If we don't want to print the command on the screen, simply add the symbol "@" in front of the command to display the command result. Like the help target above, only the command results are displayed. Generally, we will output "compiling XXX. C…" In make ...", Do not output compile-time commands. We can simulate makefile later.

If the current directory contains a file with the same name as the pseudo-target (such as clean), if the following result is displayed after the command make clean is executed:

# touch clean# make cleanmake: `clean' is up to date.#
This is because the clean file does not depend on the file. Make considers the target clean file to be the latest and will not execute the command corresponding to the rule. To solve this problem, we can clearly declare this target 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

This rule is written in "clean": the rule is followed by a line that can also be used to declare that "clean" is a pseudo-target.

In this way, modify the makefile and set all pseudo targets as. Phony dependencies:

.PHONY : all obj tag help clean disclean

Run the following command when the file clean exists in the current directory:

# make cleanrm -f debug.o ipc.o main.o timer.o tools.o target_bin target_bin.exe#

Solve the problem.

Finally, the final makefile for today is given:

# A common Makefile for c programs, version 1.1                                                                                                                                          # Copyright (C) 2014 shallnew \at 163 \dot com CFLAGS += -g -Wall -Werror -O2CPPFLAGS += -I. -I./incLDFLAGS += -lpthread # SRC_OBJ = $(patsubst %.c, %.o, $(wildcard *.c))SRC_FILES = $(wildcard *.c)SRC_OBJ = $(SRC_FILES:.c=.o)SRC_BIN = target_bin all : $(SRC_BIN) $(SRC_BIN) : $(SRC_OBJ)>---$(CC) -o [email protected] $^ $(LDFLAGS) obj : $(SRC_OBJ) tag:>---ctags -R help:>[email protected] "===============A common Makefile for cprograms==============">[email protected] "Copyright (C) 2014 liuy0711 \at 163 \dotcom">[email protected] "The following targets are support:">[email protected]>[email protected] " all             - (==make) compile and link">[email protected] " obj             - just compile, without link">[email protected] " clean           - clean target">[email protected] " distclean       - clean target and other information">[email protected] " tags            - create ctags for vim editor">[email protected] " help            - print help information">[email protected]>[email protected] "To make a target, do 'make [target]'">[email protected] "========================= Version 1.1=======================" # clean targetclean:>---$(RM) $(SRC_OBJ) $(SRC_BIN) $(SRC_BIN).exe distclean:>---$(RM) $(SRC_OBJ) $(SRC_BIN) $(SRC_BIN).exe tags *~ .PHONY : all obj tag help clean disclean

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.