关于GNU make内置函数subst二次扩展的问题
Question about GNU make built-in function subst with secondary expansion
我对具有二次扩展功能的 GNU make 有一些疑问。
这是我的项目结构(仅作为示例)
.
├── build
├── Makefile
└── src
└── a.c
这是我启用了二次扩展的 Makefile
.SECONDEXPANSION:
%.o: $$(subst build,src,%.c)
@echo $^
我输入 make build/a.o
,GNU make 抱怨:
make: *** No rule to make target `build/a.o'. Stop.
看起来 make 没有找到先决条件,所以它停止了。
为了进一步调试,我在build
目录中添加a.c
并重新输入make build/a.o
,GNU make输出这些
build/a.c
输出很奇怪,因为我预计输出将是 src/a.c
而不是 build/a.c
内置的文字功能subst
好像不能二次展开
有人知道我的 Makefile 有什么问题吗?
谢谢
这是您要实现的目标吗?
build/%.o: src/%.c
@echo $^
观察:
$ make build/a.o
src/a.c
第二次扩展与模式规则和目录中的文件相结合是一个艰难的过程。我还没有完全理解为什么你的构造不起作用。只是它不起作用。当我完全理解时会更新post。
了解更多信息:
- https://www.gnu.org/software/make/manual/html_node/Text-Functions.html
- https://www.gnu.org/software/make/manual/html_node/Pattern-Intro.html
- https://www.gnu.org/software/make/manual/html_node/Reading-Makefiles.html
- https://www.gnu.org/software/make/manual/html_node/Secondary-Expansion.html
- https://www.gnu.org/software/make/manual/html_node/Implicit-Rule-Search.html
当您执行 .SECONDEXPANSION
时,如果您想操纵先决条件,则需要使用 $$*
而不是 %
,即:
$ cat Makefile
.SECONDEXPANSION:
%.o: $$(subst /build,/src,$$(@D))/$$*.c
@echo Making $@ from $<
输出:
$ make -r OtherDirectory/build/foo.o SomeOtherDirectory/build/bar.o
Making OtherDirectory/build/foo.o from OtherDirectory/src/foo.c
Making SomeOtherDirectory/build/bar.o from SomeOtherDirectory/src/bar.c
我对具有二次扩展功能的 GNU make 有一些疑问。
这是我的项目结构(仅作为示例)
.
├── build
├── Makefile
└── src
└── a.c
这是我启用了二次扩展的 Makefile
.SECONDEXPANSION:
%.o: $$(subst build,src,%.c)
@echo $^
我输入 make build/a.o
,GNU make 抱怨:
make: *** No rule to make target `build/a.o'. Stop.
看起来 make 没有找到先决条件,所以它停止了。
为了进一步调试,我在build
目录中添加a.c
并重新输入make build/a.o
,GNU make输出这些
build/a.c
输出很奇怪,因为我预计输出将是 src/a.c
而不是 build/a.c
内置的文字功能subst
好像不能二次展开
有人知道我的 Makefile 有什么问题吗?
谢谢
这是您要实现的目标吗?
build/%.o: src/%.c
@echo $^
观察:
$ make build/a.o
src/a.c
第二次扩展与模式规则和目录中的文件相结合是一个艰难的过程。我还没有完全理解为什么你的构造不起作用。只是它不起作用。当我完全理解时会更新post。
了解更多信息:
- https://www.gnu.org/software/make/manual/html_node/Text-Functions.html
- https://www.gnu.org/software/make/manual/html_node/Pattern-Intro.html
- https://www.gnu.org/software/make/manual/html_node/Reading-Makefiles.html
- https://www.gnu.org/software/make/manual/html_node/Secondary-Expansion.html
- https://www.gnu.org/software/make/manual/html_node/Implicit-Rule-Search.html
当您执行 .SECONDEXPANSION
时,如果您想操纵先决条件,则需要使用 $$*
而不是 %
,即:
$ cat Makefile
.SECONDEXPANSION:
%.o: $$(subst /build,/src,$$(@D))/$$*.c
@echo Making $@ from $<
输出:
$ make -r OtherDirectory/build/foo.o SomeOtherDirectory/build/bar.o
Making OtherDirectory/build/foo.o from OtherDirectory/src/foo.c
Making SomeOtherDirectory/build/bar.o from SomeOtherDirectory/src/bar.c