我使用Makefiles.
我有一个目标run
,它运行构建目标.简化后,它看起来如下:
prog: ....
...
run: prog
./prog
Run Code Online (Sandbox Code Playgroud)
坐下来.我知道这是巧妙的,但不需要起立鼓掌.
现在,我的问题是 - 有没有办法传递参数?以便
make run asdf --> ./prog asdf
make run the dog kicked the cat --> ./prog the dog kicked the cat
Run Code Online (Sandbox Code Playgroud)
谢谢!
我实现了一个配方,以便将所有剩余的字符串传递给命令,例如在此脚本中:
Makefile
run:
# ./bin/run.sh $(filter-out $@,$(MAKECMDGOALS))
@echo $(filter-out $@,$(MAKECMDGOALS))
Run Code Online (Sandbox Code Playgroud)
但是,当我作为例子跑:
>make run my custom input params
my custom input params
make: *** No rule to make target `my'. Stop.
Run Code Online (Sandbox Code Playgroud)
makefile尝试执行剩余的字符串,所以错误:
Run Code Online (Sandbox Code Playgroud)make: *** No rule to make target `my'. Stop.
我怎么能阻止这个?
注意:作为解决方法,我定义了一个虚拟配方:
%:
@echo
Run Code Online (Sandbox Code Playgroud)
所以这将打印一个空字符串而不是错误.
我想避免做类似的事情:
make run-example param="my custom param"
Run Code Online (Sandbox Code Playgroud) 我有一个像这样的makefile:
.SILENT: #don't echo commands as we run them.
###
# here, set $1 (in the latter bash script) to the first make arg.
# $2 to the second one.
# et cetera.
# ($0 being set properly is optional)
###
foo:
echo "you passed to make: $1 $2 $3 $4 $5 $6"
Run Code Online (Sandbox Code Playgroud)
所以我可以做:
make foo
You passed to make: foo
Run Code Online (Sandbox Code Playgroud)
(不要告诉任何人,但我正在创建“给我做三明治”的东西)
我想在 make 退出时执行一个 shell 命令,不管它构建的是什么目标。似乎 make 没有办法直接做到这一点。但是,这里有一个在启动时执行 shell 命令的示例,无论目标如何:
是否有任何类似的 hack 在退出前执行一次 shell 命令?我可以将命令放在每个目标的底部,但是 a) 它会被执行多次,并且 b) 这很丑陋且难以管理。