Makefile:根据名称不同地运行二进制文件

时间:2011-10-06 17:09:27

标签: makefile

我的Makefile中有这个:

BINS = $(shell echo *.bin)

.PHONY: $(BINS)
run: $(BINS)

*.bin:
    ./$@

我将其作为make -j 8

运行

这样它会查找以.bin结尾的所有文件,并使用make的-j选项(Makefile run processes in background)并行运行它们

我需要修改makefile,使其运行mpi * .bin类型为mpirun -np 2 ./mpi*.bin的所有文件,其余可执行文件为./<filename>.bin

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

以下是我用来测试答案的简单示例:

touch {a,b,c,d}.bin mpi{a,b,c,d}.bin

创建一些空的测试文件,我的Makefile基于你的:

BINS = $(shell echo *.bin)

.PHONY: $(BINS)
run: $(BINS)

*.bin:
    echo "bin file: " ./$@

mpi*.bin:
    echo "mpi file: " ./$@

关键在于,前缀文件的规则遵循非前缀规则。如果没有,则将覆盖带前缀的规则。

这似乎可用于区分前缀和非前缀文件,但提供以下输出:

~/tmp$ make
Makefile:10: warning: overriding commands for target `mpia.bin'
Makefile:7: warning: ignoring old commands for target `mpia.bin'
Makefile:10: warning: overriding commands for target `mpib.bin'
Makefile:7: warning: ignoring old commands for target `mpib.bin'
Makefile:10: warning: overriding commands for target `mpic.bin'
Makefile:7: warning: ignoring old commands for target `mpic.bin'
Makefile:10: warning: overriding commands for target `mpid.bin'
Makefile:7: warning: ignoring old commands for target `mpid.bin'
echo "bin file: " ./a.bin
bin file:  ./a.bin
echo "bin file: " ./b.bin
bin file:  ./b.bin
echo "bin file: " ./c.bin
bin file:  ./c.bin
echo "bin file: " ./d.bin
bin file:  ./d.bin
echo "mpi file: " ./mpia.bin
mpi file:  ./mpia.bin
echo "mpi file: " ./mpib.bin
mpi file:  ./mpib.bin
echo "mpi file: " ./mpic.bin
mpi file:  ./mpic.bin
echo "mpi file: " ./mpid.bin
mpi file:  ./mpid.bin

我确信有一种方法可以抑制这些警告或做得更好,但这种做法似乎有效。