Preliminary Study of linux programming-Makefile: refreshing and slimming

Source: Internet
Author: User
Preliminary introduction to linux programming: the comfort and slimming of Makefile-general Linux technology-Linux programming and kernel information. The following is a detailed description. Make is a magic thing. There are many advanced usage in it. If it is not used, it will be very troublesome to write Makefile. Next we will take a practical example to look at the re-definition of make's (1) implicit pattern rule (2) Automatic dependency generation (3) nested call of make.

1. Assume that a project is divided into two subprojects: gtrans and isgen.

~> Ls project
Gtrans isgen Makefile gtrans isgen // both gtrans and isgen are generated programs.

Project> ls GTRANS/
Makefile findGraph. d findGraph. o gtrans. d isgen. cpp preProcess. d preProcess. o
FindGraph. cpp findGraph. hpp gtrans. cpp gtrans. o preProcess. cpp preProcess. hpp


. D files store the dependency between. o files, and. hpp files store header files.



2. Check GRRANS/Makefile first.

SOURCES = gtrans. cpp findGraph. cpp preProcess. cpp

LIB = "/home/dab/cadlib"

INCLUDE = "/home/dab/cadinc"

VPATH = ..

%. O: %. cpp
$ (CXX)-g3-c $ <-Wall-I $ (INCLUDE)-o $ @

%. D: %. cpp
$ (CXX)-MM-w $ <>@ @; sed's/\ ($ *\)\. o [:] */\ 1.o $ @:/G'-I $ @

Gtrans: $ (SOURCES:. cpp =. o)
$ (CXX) *. o-o ../gtrans-Wl,-R, $ (LIB)-L $ (LIB)-lagraph-lcdt

Include $ (SOURCES:. cpp =. d)

. PHONY: clean
Clean:
Rm ../gtrans *. o


If you do not use the above three functions, Makefile will be very fat. For example, if implicit pattern rule is not redefined, we need to add a compilation command for every *. o: *. cpp rule. The more source files, the more troublesome it will be. If you redefine an Implicit pattern rule (Implicit pattern rule), you will be able to work at a time and enjoy infinite use.

%. O: %. cpp
$ (CXX)-g3-c $ <-Wall-I $ (INCLUDE)-o $ @

This is simple. the default value of the variable CXX is g ++, $ <and $ @ are both automation variables, and $ @ is the file name of the target of the rule; $ <is the name of the first dependency.



3. In Makefile, our dependency may need to contain a series of header files. If it is a relatively large project, you must know which C files contain header files, and when you add or delete header files, you also need to carefully modify the Makefile, this is a very difficult task to maintain. To avoid this heavy and error-prone issue, we can use a C/C ++ compilation function. Most C/C ++ compilers support a "-M" option, that is, to automatically find the header file contained in the source file and generate a dependency. For example

GTRANS> cc-MM gtrans. cpp
Gtrans. cpp: 4: 20: warning: agraph. h: No such file or directory
Gtrans. o: gtrans. cpp findGraph. hpp preProcess. hpp

GTRANS> g ++-MM gtrans. cpp
Gtrans. cpp: 4: 20: warning: agraph. h: No such file or directory
Gtrans. o: gtrans. cpp findGraph. hpp preProcess. hpp


Because gtrans. cpp uses a library file agraph, a warning is prompted. In addition, if a non-standard library is used, two MM options are required. We can use an extreme option-w (the other is-Wall) to disable all warnings.

GTRANS> g ++-MM-w gtrans. cpp
Gtrans. o: gtrans. cpp findGraph. hpp preProcess. hpp

The redefinition of this implicit Pattern Rule is a little complicated because it uses sed, And it will inevitably use Unix regular expressions when sed is used.

%. D: %. cpp
$ (CXX)-MM-w $ <>@ @; sed's/\ ($ *\)\. o [:] */\ 1.o $ @:/G'-I $ @

$ (CXX)-MM-w $ <>$ @; redirects the result. in the d file, use gtrans. d As an example. After redirection, It is gtrans. o: gtrans. cpp findGraph. hpp preProcess. hpp; sed 'script'-I [inputfile] The input file is. d file. The modified results are saved to the file. In the sed script,'s/\ ($ *\)\. o [:] */\ 1.o $ @:/g's/pattern_1/pattern_2/G' indicates replacing all pattern_1 with pattern_2. So \ ($ * \) \. o [:] * and \ 1.o $ @: What are the two patterns respectively?

\ (\) The matching between the two can be saved and used, and a total of nine can be stored. For example, in the above example, one \ 1 is used in the second pattern to use the first \(\) store matched items, that is, the above $ *. $ * Is an automation variable in make. $ * is the stem with which an implicit rule matches, and stem is like %. d. Then this % is stem, matching gtrans. d. Then gtrans are its stem. The character \. Is the character "." (because. is a special character that can match any character). [:] * is an arbitrary number of spaces and.



4. The nested use of make is very simple. Let's take a look at the Makefile example to understand it.

. PHONY: all gtrans isgen cleanall cleanGtrans cleanIsgen

All:
Cd GTRANS & make & cd...; cd ISGEN & make & cd ..

Gtrans:
Cd GTRANS & make

Isgen:
Cd ISGEN & make

Cleanall:
Cd GTRANS & make clean & cd...; cd ISGEN & make clean & cd ..

CleanGtrans:
Cd GTRANS & make clean

CleanIsgen:
Cd ISGEN & make clean
Related Article

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.