zoukankan      html  css  js  c++  java
  • makefile学习之函数

    GNU make支持内置函数以及用户自定义函数,下面结合例子简单介绍一下。

    gnu make版本: 4.1

    一、用户自定义函数

    格式: $(call macro-name{, param1 ···})

    解析: macro-name可以是任意宏或变量,macro-name之后是宏的参数,并以逗号为分隔符。

    例子: 

    1 define test-call
    2     echo "call has two parameters: $1, $2"
    3 endef
    4 
    5 .PTHONY: simple-test
    6 simple-test:
    7     @$(call test-call,one,two)

    运行结果:

      make simple-test

      call has two parameters: one, two

    二、内置函数

    字符串函数

    1、filter

    格式: $(filter pattern ···text)

    解析: filter函数会将text视为一系列被空格隔开的单词,与pattern比较之后接着会返回相符者。

    例子:

    words :=  GNU is not unix and linux is not unix
    
    .PTHONY: simple-test
    simple-test:
        @echo words: $(words) 
        @echo unix matches: $(filter unix, $(words))

    运行结果:

      make simple-test

      words: GNU is not unix and linux is not unix
      unix matches: unix unix

    2、filter-out

    格式: $(filter-out patern...,text)

    解析:这个函数功能与filter刚好相反

    例子:

    words :=  GNU is not unix and linux is not unix
    
    .PTHONY: simple-test
    simple-test:
        @echo words: $(words) 
        @echo unix matches: $(filter-out unix, $(words))

    运行结果:

      make simple-test

      words: GNU is not unix and linux is not unix

      unix matches: GNU is not and linux is not

    3、findstring

    格式: $(findstring string...,text)

    解析: 此函数将会在text里面搜索string。如果该字符被找到了,此函数就会返回string,否则,它会返回空值。

    例子:

    words :=  GNU is not unix and linux is not unix
    
    .PTHONY: simple-test
    simple-test:
        @echo words: $(words) 
        @echo unix matches: $(findstring unix, $(words))

    运行结果:

      make simple-test
      words: GNU is not unix and linux is not unix
      unix matches: unix

    4、subst

    格式: $(subst search-string,replace-string, text)

    解析:这是一个不具通配符能力的”搜索和替换“函数。它最常被用来在文件名列表将一个扩展名替换成另一个扩展名

    例子:

    sourcelist :=  GNU.c is.c not.c unix.c and.c linux.c is.c not.c unix.c
    
    .PTHONY: simple-test
    simple-test:
        @echo sourcelist: $(sourcelist) 
        @echo unix matches: $(subst .c,.o,$(sourcelist))

    运行结果:

      make simple-test
      sourcelist: GNU.c is.c not.c unix.c and.c linux.c is.c not.c unix.c
      unix matches: GNU.o is.o not.o unix.o and.o linux.o is.o not.o unix.o

    这可以将在soucelist里面所有出现.c字样的地方都替换成.o。

    5、pathsubst

    格式: $(pathsubst search-pattern,replace-pattern,text)

    解析: 这是一个具有通配符能力的”搜索和替换“函数。

    例子:

    sourcelist :=  GNU.c is.c not.c unix.c and.c linux.c is.c not.c unix.c
    
    .PTHONY: simple-test
    simple-test:
        @echo sourcelist: $(sourcelist) 
        @echo unix matches: $(patsubst %nix.c, UNIX,$(sourcelist))

    运行结果:

      make simple-test
      sourcelist: GNU.c is.c not.c unix.c and.c linux.c is.c not.c unix.c
      unix matches: GNU.c is.c not.c UNIX and.c linux.c is.c not.c UNIX

    6、words

    格式: $(words text)

    解析:此函数会返回text中单词的数量

    例子:

    sourcelist :=  GNU.c is.c not.c unix.c and.c linux.c is.c not.c unix.c
    
    .PTHONY: simple-test
    simple-test:
        @echo sourcelist: $(sourcelist) 
        @echo unix matches: $(words $(sourcelist))

    运行结果:

      make simple-test
      sourcelist: GNU.c is.c not.c unix.c and.c linux.c is.c not.c unix.c
      unix matches: 9

    7、words后面带n

    格式:$(words n,text)

    解析: 此函数会返回text中的第n个单词,第一个单词的编号为1。如果n的值大于text中单词的个数,则此函数将会返回空值。

    例子:

    sourcelist :=  GNU.c is.c not.c unix.c and.c linux.c is.c not.c unix.c
    
    .PTHONY: simple-test
    simple-test:
        @echo sourcelist: $(sourcelist) 
        @echo unix matches: $(words 3,$(sourcelist))

    测试结果:

      make simple-test
      sourcelist: GNU.c is.c not.c unix.c and.c linux.c is.c not.c unix.c
      unix matches: 9

    没有返回预想的值,好奇怪。

    8、firstword

    格式: $(firstword text)

    解析: 此函数会返回text中的第一个单词。

    例子:

    sourcelist :=  GNU.c is.c not.c unix.c and.c linux.c is.c not.c unix.c
    
    .PTHONY: simple-test
    simple-test:
        @echo sourcelist: $(sourcelist) 
        @echo unix matches: $(firstword $(sourcelist))

    运行结果:

      make simple-test
      sourcelist: GNU.c is.c not.c unix.c and.c linux.c is.c not.c unix.c
      unix matches: GNU.c

    9、wordlist

    格式: $(wordlist start,end,text)

    解析: 此函数会返回text中范围从start(含)到end(含)的单词。

    例子: 

    sourcelist :=  GNU.c is.c not.c unix.c and.c linux.c is.c not.c unix.c
    
    .PTHONY: simple-test
    simple-test:
        @echo sourcelist: $(sourcelist) 
        @echo unix matches: $(wordlist 1,3,$(sourcelist))

    运行结果:

      make simple-test
      sourcelist: GNU.c is.c not.c unix.c and.c linux.c is.c not.c unix.c
      unix matches: GNU.c is.c not.c

    时间关系,先介绍到这。

  • 相关阅读:
    shell脚本之数组
    shell脚本之函数
    shell脚本之sed
    shell脚本的for循环与read
    shell脚本之if语句
    shell脚本正则表达式
    shell的编程原理
    《梦断代码》阅读笔记03
    12.19学习总结
    《梦断代码》阅读笔记02
  • 原文地址:https://www.cnblogs.com/cheyihaosky/p/11538002.html
Copyright © 2011-2022 走看看