GNU Make:在模式规则宏中使用% [英] GNU Make: Using % in a pattern-rule macro
问题描述
我正在尝试构建一个模式规则,该规则允许我在宏调用中使用%
,但未获得预期的结果.
I'm trying to build a pattern-rule that allows me to use %
in a macro call and I'm not getting the results I expect.
模式规则:
%.o: %.cpp $(%_H)
g++ -o $@ $(FLAGS) -c $<
问题出在$(%_ H)
The problem is with the $(%_H)
由于某些原因,%
没有扩展到定义的位置.
For some reason %
isn't expanding to what it is defined to.
当我打印出依赖项列表($^)
时,仅打印源文件.
When I print out the dependency list ($^)
, only the source file prints.
我有一种非常干净的方式来设置我想使用的依赖项:
I have a very clean way of setting up my dependencies that I would like to use:
# Header Dependencies per object
Geometry_H:=Geometry.h $(Error_H)
Enemy_H:=Enemy.h $(Geometry_H) $(Error_H)
Surface_H:=Surface.h $(Geometry_H)
Player_H:=Player.h $(Geometry_H) $(Surface_H)
SDLWindow_H:=SDLWindow.h $(Surface_H) $(Error_H)
Path_H:=Path.h $(Geometry_H) $(Error_H)
Territory_H:=Territory.h $(Geometry_H)
Board_H:=Board.h $(Territory_H) $(Geometry_H) $(Player_H) $(Path_H) $(Enemy_H) $(Error_H)
Error_H:=Error.h
Diminisher_H:=Diminisher.h $(SDLWindow_H) $(Geometry_H) $(Surface_H) $(Board_H) $(Error_H)
Main_H:=$(Diminisher_H)
另一个人建议我将这些变量更改为依赖项列表.
Another person has suggested that I change these variables into dependency lists.
即 Main_H:= $(Diminisher_H)
I.e. Main_H:=$(Diminisher_H)
成为 Main.o:$(Diminisher_H)
becomes Main.o: $(Diminisher_H)
这是一个很好的解决方案,它可以正常工作.
但是,问题仍然在于$(%_H)
某种程度上是无效的.
This is a good fix, it works.
However, the problem still remains that $(%_H)
is somehow invalid.
我想知道如何(如果可能)使其成为有效的表达式.
I would like to know how (if possible) to make it a valid expression.
我已经尝试过$( $%_H )
,$( $(%)_H )
,$( $(value %)_H )
等.
似乎%
在进行宏调用时就失去了意义.
I've tried $( $%_H )
, $( $(%)_H )
, $( $(value %)_H )
and many more.
It seems like %
just loses its meaning when in a macro call.
无法在宏调用中使用%
吗?
Is there no way to use %
in a macro call?
推荐答案
都可以使用二次扩展:
.SECONDEXPANSION:
%.o: %.cpp $$($$*_H)
g++ -o $@ $(FLAGS) -c $<
或者(IMO,更好)使用依赖项自动生成:
Or (IMO, better) use dependency auto-generation:
- An answer to another SO question
- Advanced Auto-Dependency Generation article
这篇关于GNU Make:在模式规则宏中使用%的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!