我有多个子文件夹,每个子文件夹都具有相同的文件/结构
tests
|- test1
|- file1
|- file2
|- test2
|- file1
|- file2
现在我想编写同样适用于
test1
和 test1
中的内容的 Makefile 规则。
到目前为止我所拥有的:
TESTS = test1 test2
.PHONY : all
all: $(TESTS)/my_program
%/my_program : %/file1 %/file2
clang $< -o $@
不幸的是,我收到了错误
No rule to make target
AddStruct/my_program',需要 all'. Stop.
然后我尝试更改构建规则以使用
*
*/my_program : */file1 */file2
clang $< -o $@
但是,
make
将构建所有组合
clang test1/file1 test1/file2 -o test1/my_program
clang test2/file1 test2/file2 -o test1/my_program
...
这不是我的本意。我该如何编写规则以使目标和依赖项具有相同的路径?
这是错误的:
TESTS = test1 test2
all: $(TESTS)/my_program
变量被简单地替换; make 中没有“魔法”可以看到你的变量后面有另一个单词,并以某种方式将该单词附加到变量中的每个单词。以上与写作相同:
all: test1 test2/my_program
这可能不是您想要的。你想要:
all: $(addsufix /my_program,$(TESTS))
使用通配符确实不是您想要的,因为解析 makefile 时这些通配符会被扩展。