Original blog, reprint please indicate the source--Zhou Xuewei http://www.cnblogs.com/zxouxuewei/
In this section we'll talk about the Make function, which has been described in the previous chapters for several functions: wildcard, PATSUBST, Notdir, Shell, and so on. The calling format of the general function is as follows:
$ (funcname arguments) or $ (funcname arguments)
Where FuncName is the function name that needs to call the function, it should be the make inline function, arguments is the function parameter, the argument and functions are separated by a space, if there are more than one argument, the parameters are separated by commas "". The function call begins with "$", using paired parentheses or curly braces to enclose the function name and parameters, usually using parentheses.
Here's a look at some of the commonly used functions:
1. Get the matching mode filename function-wildcard.
Usage: $ (wildcard PATTERN)
This function lists all file names in the current directory that conform to the pattern "pattern" format. Returns a space-delimited file name that exists for all conforming pattern "pattern" in the current directory.
For example:
Src_files = $ (wildcard src/*.c)
Returns a list of all. c files in the SRC directory.
2. String substitution function-subst.
Usage: $ (subst from,to,text)
This function replaces the "from" character in the string "TEXT" with "to", returning the new string after replacement.
3. Mode replacement function-patsubst.
Usage: $ (patsubst pattern,replacement,text)
This function searches for words separated by spaces in "TEXT" and replaces the conforming pattern "Tattern" with "replacement". The pattern wildcard "%" can be used in the parameter "pattern" to represent several characters in a single word. If the parameter "replacement" contains a "%", then "%" in "replacement" will be the string represented by the "%" in "Tattern".
For example:
Src_obj = $ (Patsubst%.c,%.O, $ (src_files))
Replace All. c files in the src_files with. O to return to the variable src_obj.
This function is similar to the previously mentioned variable substitution, http://blog.csdn.net/shallnet/article/details/37529935
The variable substitution format is "$ (var:a=b)" or "${var:a=b}", which means replacing all "a" in the variable "var" with "a" in the "a" string to "B" string.
For example, we have a variable that represents all. C files. Defined as "Src_files = A.C B.C c.c".
In order to get these. c files corresponding to the. o source file. The same results can be obtained for the following two uses:
$ (OBJECTS:.C=.O) $ (patsubst%.c,%.o,$ (src_files))
4. Filter function-filter.
Usage: $ (filter pattern...,text)
This function filters out all words in the string "TEXT" that do not conform to the pattern "pattern" and preserves all words that conform to this pattern. You can use multiple modes. The pattern character "%" is generally required in the mode. When there are multiple schemas, the pattern expression is separated by a space. Returns all strings in the "TEXT" string that match the pattern "pattern" in a space split.
5. Anti-filtering function-filter-out.
Usage: $ (filter-out pattern...,text)
And the function implemented by the "filter" function is reversed. Filters out all words in the string "TEXT" that conform to the pattern "pattern", preserving all words that do not conform to this pattern. There can be multiple modes. When there are multiple schemas, the pattern expression is separated by a space.
6. Take the directory function-dir.
Usage: $ (dir NAMES ...)
Remove the directory portion of each file name from the file name sequence "NAMES ...". The directory part of the file name is the part that precedes the last slash ("/") (including the slash) in the file name. Returns the directory portion of each file in a space-delimited sequence of filenames "NAMES ...". If there is no slash in the file name, it is considered a file under the current directory ("./").
7. Take the filename function--notdir.
Usage: $ (notdir NAMES ...)
Remove the non-directory part from the file name sequence "NAMES ...". The Table of Contents section refers to the last slash ("/") (including the slash) before the section. Delete all the directories in the file name section, leaving only the non-directory part. The non-directory portion of each file in the filename sequence "NAMES ...".
8. Take the suffix function-suffix.
usage: $ (suffix NAMES ...)
The function extracts the suffix of each file name from the file name sequence "NAMES ...". The suffix is the last point in the file name, "." The beginning (including dot) section, or null if the file name does not contain a dot number. Returns the suffix sequence for each file in a space-delimited sequence of file names, "NAMES ...".
9. Take the prefix function-basename.
Usage: $ (basename NAMES ...)
Remove the prefix portion of each file name from the file name sequence "NAMES ..." (the part after the dot). The prefix section refers to the part of the file name that precedes the last dot number. Returns the prefix sequence for individual files in the file name sequence "NAMES ..." of a space split. If the file does not have a prefix, an empty string is returned.
Here are just a few of the commonly used functions, some of which are not mentioned and can be used to turn over the makefile manual.
In general, make will print out the currently executing command at compile time, and when the compile Link option is long, it will output a lot of things on the screen, and if I don't want to see a lot of things on the screen, we can add @ to the command before the command is output to the screen. We try to modify the following:
# make make[1]: Entering directory '/HOME/MYPROJECTS/EXAMPLE_MAKE/VERSION-3.1/SRC/IPC ' make[1]: Leaving Directory '/HOME/MYPROJECTS/EXAMPLE_MAKE/VERSION-3.1/SRC/IPC ' make[1]: Entering directory '/home/myprojects/ Example_make/version-3.1/src/tools ' make[1]: Leaving directory '/home/myprojects/example_make/version-3.1/src /tools ' make[1]: Entering directory '/home/myprojects/example_make/version-3.1/src/main ' make[1]: Leaving Directory '/home/myprojects/example_make/version-3.1/src/main ' #
It is found that only the display of the directory and exit directory is entered, so it is difficult to know the current compilation process. We can actually add a line to the rule command like print:
@echo "Do something ..."
This will output what is currently being done, without outputting the executing command. Now change the rules to the following:
$ (objdir): >[email protected] "MKDIR $ (Notdir [email protected]) ..." >[email protec Ted]-P [email protected] Ifneq ($ (Src_bin),) $ (BINDIR)/$ (Src_bin): $ (src_obj) >[email Protected] "LINK $ (Notdir [email protected]) ..." >[email protected]$ (CC)-O [email protected] $^ $ (ldflags) endif ifneq ($ (src_lib),) $ (libdir)/$ (src_lib): $ (src_obj) >[email protecte D] "ARCHIVE $ (Notdir [email protected]) ..." >[email protected]$ (AR) RCS [email protected] $^ >[email protected] "COPY [email protected] to $ (src_base)/libs" >[email protected] [email protected] $ (src_base)/libs endif $ (src_obj): $ (objdir)/%.O:%.c >[email protected] " COMPILE $ (notdir $<) ... ">[email protected]$ (CC)-C $ (CFLAGS) $ (cppflags) $<-O [email protect Ed
The compiled output is as follows:
# make make[1]: Entering directory '/HOME/MYPROJECTS/EXAMPLE_MAKE/VERSION-3.1/SRC/IPC ' COMPILE ipc.c ... ARCHIVE libipc.a ... COPY.. /.. /BUILD/UNIX_DBG/LIB/LIBIPC.A to. /.. /libs make[1]: Leaving directory '/HOME/MYPROJECTS/EXAMPLE_MAKE/VERSION-3.1/SRC/IPC ' make[1]: Entering Directory '/home/myprojects/example_make/version-3.1/src/tools ' COMPILE base64.c ... COMPILE md5.c ... COMPILE tools.c ... ARCHIVE LIBTOOLS.A ... COPY.. /.. /BUILD/UNIX_DBG/LIB/LIBTOOLS.A to. /.. /libs make[1]: Leaving directory '/home/myprojects/example_make/version-3.1/src/tools ' make[1]: Entering Directory '/home/myprojects/example_make/version-3.1/src/main ' COMPILE main.c ... LINK target_bin ... MAKE[1]: Leaving directory '/home/myprojects/example_make/version-3.1/src/main ' #
$ (BUILDDIR): >[email protected] "Create directory [email protected] ..." >---mkdir-p $ ( BUILDDIR)/bin $ (BUILDDIR)/lib $ (MODULES): >[email protected]$ (Make)-C $ (DIR)/[email protecte D] [email protected]--no-print-directory main:tools IPC Clean: >[email protected ] SubDir in $ (MODULES); \ >---do $ (make)-C $ (DIR)/$ $subdir module=$ $subdir [email protected]--no-print-directory; \ >---Done compile output: # Make COMPILE ipc.c ... ARCHIVE libipc.a ... COPY.. /.. /BUILD/UNIX_DBG/LIB/LIBIPC.A to. /.. /libs COMPILE base64.c ... COMPILE md5.c ... COMPILE tools.c ... ARCHIVE LIBTOOLS.A ... COPY.. /.. /BUILD/UNIX_DBG/LIB/LIBTOOLS.A to. /.. /libs COMPILE main.c ... LINK target_bin ... # make clean rm-f. /.. /BUILD/UNIX_DBG/OBJ/IPC/IPC.O. /.. /BUILD/UNIX_DBG/LIB/LIBIPC.A rm-f. /.. /BUILD/UNIX_DBG/OBJ/MAIN/MAIN.O. /.. /build/unix_dbg/bin/target_bin rm-f. /.. /BUILD/UNIX_DBG/OBJ/TOOLS/BASE64.O. /.. /BUILD/UNIX_DBG/OBJ/TOOLS/MD5.O. /.. /BUILD/UNIX_DBG/OBJ/TOOLS/TOOLS.O. /.. /BUILD/UNIX_DBG/LIB/LIBTOOLS.A #
This looks like the output is much fresher. In fact, we can also use Make-s to completely prohibit the display of commands.
Makefile---Make inline function and make command display (ix)