Makefile文件获取自己的路径

获取文件当前的路径对很多操作会是一件非常便利的事情。

Makefile 文件获取自己的路径(gnu make)

  在Makefile文件中利用make的builtin variables, builtin functions 获取它自己的路径

THIS_DIR:= \
$(abspath $(dir $(word $(words $(MAKEFILE_LIST)),$(MAKEFILE_LIST))))
  • MAKEFILE_LIST

    MAKEFILE_LIST 是 GNU make 的一个特殊变量。手册上的解释如下:

    Contains the name of each makefile that is parsed by make, in the order in which it was parsed. The name is appended just before make begins to parse the makefile. Thus, if the first thing a makefile does is examine the last word in this variable, it will be the name of the current makefile. Once the current makefile has used include, however, the last word will be the just-included makefile.

   make利用 MAKEFILE_LIST 这个特殊变量按照先后顺序记录了它解析的每一个Makefile, 所以 MAKEFILE_LIST 的最后一个元素总是它当前正在解析的Makfile的名字(像stack栈的工作方式, 先进后出)。

  • words

    words 是 make 的一个 builtin function. 手册上解释如下:

    $(words text)
         Returns the number of words in text. Thus, the last word of text is $(word $(words text),text).

  • word

    word 是 make 的 buitin funtion. 手册解释如下:

    $(word n,text)
    Returns the nth word of text. The legitimate values of n start from 1. If n is bigger than the number of words in text, the value is empty. For example,  $(word 2, foo bar baz)  returns ‘bar’.

  • dir

    dir 是 make 的 builtin function. 手册解释如下:

    $(dir names...)
    Extracts the directory-part of each file name in names. The directory-part of the file name is everything up through (and including) the last slash in it. If the file name contains no slash, the directory part is the string ‘./’. For example,
$(dir src/foo.c hacks) produces the result ‘src/ ./’.

  • abspath

    abspath 是 make 的 builtin function

    $(abspath names...)
    For each file name in names return an absolute name that does not contain any . or .. components, nor any repeated path separators (/). Note that, in contrast to realpath function, abspath does not resolve symlinks and does not require the file names to refer to an existing file or directory. Use the wildcard function to test for existence.

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值