将选项传递给 makefile

将选项传递给 makefile

生成文件

my_test:
ifdef $(toto)
        @echo 'toto is defined'
else
        @echo 'no toto around'
endif

预期行为

$ make my_test
no toto around

$ make my_test toto
toto is defined

目前的行为

$ make my_test
no toto around

$ make my_test toto
no toto around
make: *** No rule to make target `toto'.  Stop.

当我运行时,make my_test我按预期得到了 else 文本no toto around。然而

make my_test toto
no toto around
make: *** No rule to make target `toto'.  Stop.

生成文件版本

 $ make -v
   GNU Make 3.81

SLE版本

$ cat /etc/*release
  VERSION_ID="11.4"
  PRETTY_NAME="SUSE Linux Enterprise Server 11 SP4"

聚苯乙烯

重点是要make my_test详细说明 if toto,如果toto没有给出,那么命令将静默运行

答案1

您需要删除 toto 周围的美元,并以不同的方式从命令行传递 toto

命令行

make toto=1  my_test

生成文件

my_test:
ifdef toto
        @echo 'toto is defined'
else
        @echo 'no toto around'
endif

答案2

你可以使用这些Makefile内容,技巧是filter功能

my_test:
ifeq (toto, $(filter toto,$(MAKECMDGOALS)))
        @echo 'toto is defined'
else
        @echo 'no toto around'
endif
        @echo run command $(if $(filter toto,$(MAKECMDGOALS)),--verbose,--normally)

%:
        @:

结果:

$ make my_test
no toto around
run command --normally

$ make my_test toto
toto is defined
run command --verbose

$ make toto my_test
toto is defined
run command --verbose

$ make my_test totofofo
no toto around
run command --normally

答案3

Makefile 可能是个诡计,通常我会做一些类似的事情

# turn them into do-nothing targets
$(eval toto:;@:)

my_test:
ifeq (toto, $(filter toto,$(MAKECMDGOALS)))
        @echo 'toto is defined'
else
        @echo 'no toto around'
endif

相关内容