makefile--pseudo Target (iii)

Source: Internet
Author: User

Original blog, reprint please indicate the source--Zhou Xuewei http://www.cnblogs.com/zxouxuewei/

In general, Makefile will have a clean target that clears the binaries generated during compilation. We used this clean target in the first section of the makefile, which does not have any dependent files and does not produce clean files after the target command executes.

Like this characteristic goal, its rules define a command that is not to create a file, but simply to specify the target through make to execute some specific system commands or the rules whose dependencies are targeted (such as all), called pseudo-targets.

A makefile generally will not have only a pseudo-target, if according to makefile "unspoken rules" and its customary name, in the larger project makefile more commonly used as the goal has these:

All: Performs major compilation work, usually used as the default target, at the front.

Install: Perform the post-compilation installation work, copy the executable files, configuration files, documents, etc. to different installation directories.

Clean: Deletes compiled generated binaries.

Distclean: Remove all intermediate build files except source files, such as configuration files, documents, etc.

Help: Print information about the current makefile, such as which targets can be executed with make designation.

When make processes makefile, it first reads all the rules and establishes a relational dependency graph. It is then executed from the default target (the first target) or the specified target. A goal such as clean,tags is not typically a default target, nor does it have any dependencies on the default target, so make cannot generate its dependencies and decide whether it will be executed. So to perform such a goal, you must display the specified make that target. Just like in the middle of the binary file we've already produced, we need to show the Execute command: Make clean.

A pseudo-target can also be a default target, such as all, and you can specify a dependent file for it.

We will first makefile the version 1.0, we can add help information, tags and other functions.

    # A Common Makefile for C programs, version 1.1 # Copyright (c) shallnew \at 163 \dot com CFLAGS + =-g-wall-werror-o2 cppflags +-I.-i./inc ldflags +-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) liuy0711 \at 163 \dotcom ">[email protected]" The following targets is support: ">[email protected"             ;-[email protected] "All-(==make) Compile and link" >[email protected] "obj -Just compile, withoutLink ">[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&nb Sp;protected] "to make a target, does ' make [target] '" >[email protected] "========================= Version 1.             1======================= "# Clean target clean: >---$ (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 do not want to print the command on the screen, only display the command results, directly before the command with the symbol "@" can be implemented. As with the help target above, only the command results are displayed. In general we will output "compiling xxx.c ..." When make, do not output the compile-time command. We can imitate when we write makefile in the back.

If a file with the same name as the pseudo target exists in the current directory (such as clean), the following results appear if you execute the command make clean:

    # Touch Clean # do clean make: ' Clean ' was up to      date.      #  
This is because the clean file is not dependent on the file, and make thinks the target clean is up-to-date and does not execute the command corresponding to the rule. To solve this problem, we can explicitly declare the target as a pseudo target. Declaring a target as a pseudo-target requires it to be a special target. Phony "dependence. as follows:
. Phony:clean  

This rule is written in clean: the rule behind the line can also play to declare clean is the role of pseudo-target

Modify the previous makefile so that all pseudo-targets are used. Phony's dependence:

. Phony:all obj tag help clean Disclean  

This executes when file clean exists in the current directory:

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

Find problem resolution.

Finally, give the final makefile today:

        # A Common Makefile for C programs, version 1.1 # Copyright (C) shallnew \at 163 \do                     t com CFLAGS + =-g-wall-werror-o2 Cppflags + =-I.-i./inc ldflags + =-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 Commo          n Makefile for cprograms============== ">[email protected" "Copyright (C) liuy0711 \at 163 \dotcom" >[email p rotected] "The following targets is support:" >[email protected] >[email protected] " All-(==make) Compile and link ">[email protected]" Obj-just compile, Withou T link ">[email protected]" Clean-clean target ">[email protected]" distcl Ean-clean Target and other information ' >[email protected] ' tags-create ctags for V Im editor ">[email protected" "Help-print Help Information" >[email protec Ted] >[email protected] "to make a target, does ' make [target] '" >[email protected] "===== ==================== Version 1.1======================= "# clean target clean: >- --$ (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

makefile--Pseudo-Target (iii)

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.