makefile - 如何获取Makefile当前的相对目录?

标签 makefile gnu-make working-directory

我在应用程序特定目录中有几个 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)。 或者 shellnotdir 结合使用,如果您不需要绝对路径: 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 run cd /; make -f /home/username/project/Makefile, the current_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/

相关文章:

bash - Bash 如何在不同的目录上下文中执行命令?

c++ - Makefile 模式规则无法匹配

c++ - 对 Makefile.win 中函数的 undefined reference

Pandoc的Makefile和引用文件

makefile - 如何在 gnumake 文件中强制出现错误

c++ - 系统 ("c:\\sample\\startAll.bat") 由于工作目录而无法运行?

c - 在 makefile 中安装依赖项

performance - 如何加快启用 CMake 的 C++ 项目的编译时间?

c++ - 多个子目录中目标文件的通用 makefile 目标

linux - 使用自定义环境运行脚本。变种。和没有额外外壳的工作目录