我使用Makefiles。
我有一个名为run的目标,它运行构建目标。简化后,如下图所示:
prog: ....
...
run: prog
./prog
有传递参数的方法吗?这
make run asdf --> ./prog asdf
make run the dog kicked the cat --> ./prog the dog kicked the cat
我使用Makefiles。
我有一个名为run的目标,它运行构建目标。简化后,如下图所示:
prog: ....
...
run: prog
./prog
有传递参数的方法吗?这
make run asdf --> ./prog asdf
make run the dog kicked the cat --> ./prog the dog kicked the cat
当前回答
我不知道怎么做你想要的,但一个变通的方法可能是:
run: ./prog
./prog $(ARGS)
然后:
make ARGS="asdf" run
# or
make run ARGS="asdf"
其他回答
这个问题已经提了三年了,但是不管怎样……
如果您正在使用GNU make,这很容易做到。唯一的问题是make会将命令行中的非选项参数解释为目标。解决办法是把他们变成什么都不做的目标,这样make就不会抱怨:
# If the first argument is "run"...
ifeq (run,$(firstword $(MAKECMDGOALS)))
# use the rest as arguments for "run"
RUN_ARGS := $(wordlist 2,$(words $(MAKECMDGOALS)),$(MAKECMDGOALS))
# ...and turn them into do-nothing targets
$(eval $(RUN_ARGS):;@:)
endif
prog: # ...
# ...
.PHONY: run
run : prog
@echo prog $(RUN_ARGS)
运行这个函数会得到:
$ make run foo bar baz
prog foo bar baz
下面是我的例子。注意,我是在Windows 7下编写的,使用Dev-Cpp附带的mingw32-make.exe。(我有c:\Windows\System32\make.bat,所以命令仍然被称为“make”。)
clean:
$(RM) $(OBJ) $(BIN)
@echo off
if "${backup}" NEQ "" ( mkdir ${backup} 2> nul && copy * ${backup} )
日常清洁用途:
make clean
在mydir/中清理和创建备份的用法:
make clean backup=mydir
它已经有一段时间了,但我将提供我在生产中使用的版本。
我希望有人会觉得它有用。
例子:
.PHONY: greeting
greeting:
echo $(if $s,$s,)
命令:
使问候s="hello world"
输出:
你好世界
对于标准make,您可以通过这样定义宏来传递参数
make run arg1=asdf
然后像这样使用它们
run: ./prog $(arg1)
etc
制作参考资料 微软的NMake
你可以像下面这样将变量传递给Makefile:
run:
@echo ./prog $$FOO
用法:
$ make run FOO="the dog kicked the cat"
./prog the dog kicked the cat
or:
$ FOO="the dog kicked the cat" make run
./prog the dog kicked the cat
或者使用Beta提供的解决方案:
run:
@echo ./prog $(filter-out $@,$(MAKECMDGOALS))
%:
@:
%: -匹配任何任务名称的规则; @: -空食谱=什么都不做
用法:
$ make run the dog kicked the cat
./prog the dog kicked the cat