8

似乎 GNU make 解释的 basename 函数与 bash 的 basename 不同。前者会去掉后缀,而后者也会去掉路径。如何在我的 makefile 中获取文件夹的基本名称?

另外,他们为什么要改变它?(我花了 20 分钟才找到错误的根源)

4

4 回答 4

4

我猜这个basename(1)命令有两个正交的功能——剥离足够,剥离前导目录部分——并且 GNU 使作者希望能够分别调用每个功能。当然,只有一个概念可以得到名称basename

当然,在 makefile 中,能够将foo/bar/baz.c转换为foo/bar/baz是很有用的,这样您就可以在末尾添加一个新的后缀,以便在与源文件相同的目录中构造一个相关的文件名。

@ire_and_curses 回答说明上的评论$(notdir $(CURDIR))不足以满足您的目的,因为(作为目录)CURDIR 可能已指定为

CURDIR = /foo/bar/

并且notdir由于尾部斜杠而剥离了整个事物。为了允许以这种方式编写目录路径,您需要使用 eg 显式去除尾部斜杠$(notdir $(CURDIR:%/=%))

于 2013-08-30T05:13:29.667 回答
3

是的,这很奇怪。您可以通过链接notdir 和 basename来获得您想要的行为:

$(notdir names...)
    Extracts all but the directory-part of each file name in names... For example,    
              $(notdir src/foo.c hacks)

    produces the result ‘foo.c hacks’. 

...

$(basename names...)
    Extracts all but the suffix of each file name in names. If the file name
    contains a period, the basename is everything starting up to (and not
    including) the last period... For example,

              $(basename src/foo.c src-1.0/bar hacks)

    produces the result ‘src/foo src-1.0/bar hacks’. 

因此,例如,您可以通过链接这样的函数来转换/home/ari/src/helloworld.c为:helloworld.html

SRC=/home/ari/src/helloworld.c
TARGET=$(addsuffix .html, $(notdir $(basename $(SRC))))
于 2012-09-14T06:07:50.780 回答
2

你仍然可以使用 bash 的版本:

SHELL := /bin/bash
basename := $(shell basename /why/in/gods/name)
于 2012-09-14T08:07:39.920 回答
2

怎么样$(dir /path/to/file.txt)

https://www.gnu.org/software/make/manual/html_node/File-Name-Functions.html

于 2014-11-24T01:17:33.083 回答