特定于目标的变量作为Makefile中的先决条件

Nim*_*adi 14 dependencies build-process makefile build

我正在尝试编写一个GNU make Makefile,它有一些类似的目标,其中构建命令在它们之间略有不同.我正在尝试使用特定目标的变量来表示这些变化.其中一些变量值指的是我想用作先决条件的文件.例如:

target_1:special_filename=target1_prereq
target_2:special_filename=target2_prereq

target_1 target_2: common_filename $(special_filename)
    do_something common_filename --a-weird-option=$(special_filename)
Run Code Online (Sandbox Code Playgroud)

当我调用'make target_1'时,如果它不存在,我想让它成为target1_prereq.目前,即使使用正确的参数调用构建命令(do_something),它似乎也不使用target1_prereq作为先决条件.

我正在使用GNU Make 3.80.


编辑:真实系统的一些复杂问题.一些变量本身基于其他变量的值.手动指定先决条件将无法扩展.一个稍微复杂的例子:

target_1:special_filename_base=target1_prereq
target_2:special_filename_base=target2_prereq

some_filename_a = $(special_filename_base).exta
some_filename_b = $(special_filename_base).extb

target_1 target_2: common_filename $(special_filename_b) $(special_filename_a)
    do_something common_filename --a-weird-option=$(special_filename_a) --second=$(special_filename_b)
Run Code Online (Sandbox Code Playgroud)

Bet*_*eta 4

特定于目标的变量仅在目标的命令中(或其他特定于目标的分配中)定义;它不能用作目标的先决条件之一。我认为没有一种干净的方法可以在 Make 中完成您想要的操作,但是有几种笨拙的方法,例如以下:

扩展名 = .exta .extb
target_1:$(addprefix target1_prereq,$(扩展))
target_2:$(addprefix target2_prereq,$(扩展))

target_1 target_2: 通用文件名
    do_something common_filename --a-weird-option=$(filter %.exta,$^) --second=$(filter %.extb,$^)

  • 为了其他偶然发现它的人的利益而复活这个线程。[更优雅的解决方案](http://stackoverflow.com/questions/9311743/make-using-target-specific-variables-in-precessions)使用二次扩展。 (4认同)