我长期以来一直在使用这样的奇怪规则,但突然之间他们正在打破一个新的环境。
有没有一种强有力的方法可以做到这一点?
all: test.1.out
test.%.out: %/test*.out
/bin/cp -f $< $@
在我的盒子(ubuntu)上:
alishan:~/Dropbox/make_insanity> make --version
GNU Make 3.81
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.
This program built for x86_64-pc-linux-gnu
alishan:~/Dropbox/make_insanity> make
/bin/cp -f 1/test.out test.1.out
在其他人的Mac,ubuntu计算机,ubuntu虚拟机上使用此类代码没有问题。不知道他们所有的制作版本,但它似乎是好的代码。
清除后在同一目录下的我的mageia服务器上。
[dushoff@yushan make_insanity]$ make --version
GNU Make 3.82
Built for x86_64-mageia-linux-gnu
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
[dushoff@yushan make_insanity]$ make
make: *** No rule to make target `test.1.out', needed by `all'. Stop.
[dushoff@yushan make_insanity]$
将 %
或*
更改为相应的文字&#34;修正&#34;问题,但当然不会产生所需的普遍性。
答案 0 :(得分:1)
我不是很确定,但我认为你想要
test.<name>.out
由
制作<name>/test.out
(如果存在)。如果那是对的,你可以通过逆向工程来做到这一点 来自目标名称的每个目标的先决条件的名称,例如
targs := test.1.out test.a.out
define src_of =
$(patsubst .%,%,$(suffix $(basename $(1))))/$(basename $(basename $(1)))$(suffix $(1))
endef
all: $(targs)
test.%.out: $(call src_of,test.%.out)
cp -f $< $@
clean:
rm -f *.*.out
不可否认,它确实有点满口。
因此,如果我们预先安排存在先决条件的演示:
$ ls -LR
.:
1 a Makefile
./1:
test.out
./a:
test.out
$ make
cp -f 1/test.out test.1.out
cp -f a/test.out test.a.out
答案 1 :(得分:0)
您可以使用secondary expansion和通配符功能执行此操作。请注意,如果没有这个,它永远不会有效,所以无论你看到什么代码都应该被认为是破碎的。
.SECONDEXPANSION:
all: test.1.out
test.%.out: $$(wildcard $$*/test*.out)
@echo Prerequisites found: $^
请注意,使用$*
而不是%
进行模式匹配(请参阅automatic variables)会被额外的$
转义,整个事情都会设置为展开两次,这样两组值都得到了扩展。
据说这整个布局看起来不对。您使用$<
只匹配先决条件的第一个单词,即使使用通配符,您可能会有很多。 Mike's answer可能是一个更好的选择,只要重新组织你的Makefile实际上是健壮的。