我在应用程序特定目录中有几个 Makefile,如下所示:
/project1/apps/app_typeA/Makefile
/project1/apps/app_typeB/Makefile
/project1/apps/app_typeC/Makefile
每个 Makefile 都在此路径中包含一个向上一级的 .inc 文件:
/project1/apps/app_rules.inc
在 app_rules.inc 中,我设置了构建时要放置二进制文件的目标位置。我希望所有二进制文件都位于各自的 app_type
路径中:
/project1/bin/app_typeA/
我尝试使用$(CURDIR)
,如下所示:
OUTPUT_PATH = /project1/bin/$(CURDIR)
但是我把二进制文件埋在整个路径名中,如下所示:(注意冗余)
/project1/bin/projects/users/bob/project1/apps/app_typeA
我该怎么做才能获取执行的“当前目录”,以便我只知道 app_typeX
以便将二进制文件放入各自的类型文件夹中?
最佳答案
shell 函数。
您可以使用shell
函数:current_dir = $(shell pwd)
。
或者 shell
与 notdir
结合使用,如果您不需要绝对路径:
current_dir = $(notdir $(shell pwd))
。
更新。
给定的解决方案仅在您从 Makefile 的当前目录运行 make
时才有效。
正如@Flimm 指出的:
Note that this returns the current working directory, not the parent directory of the Makefile.
For example, if you runcd /; make -f /home/username/project/Makefile
, thecurrent_dir
variable will be/
, not/home/username/project/
.
下面的代码适用于从任何目录调用的 Makefile:
mkfile_path := $(abspath $(lastword $(MAKEFILE_LIST)))
current_dir := $(notdir $(patsubst %/,%,$(dir $(mkfile_path))))
关于makefile - 如何获取Makefile当前的相对目录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18136918/