Make

將選項傳遞給生成文件

  • August 27, 2021

生成文件

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沒有給出,那麼命令將靜默執行

您需要刪除 toto 周圍的美元,並以不同的方式從命令行傳遞 toto

命令行

make toto=1  my_test

生成文件

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

您可以使用這些 Makefile 內容,技巧是filter-function

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

引用自:https://unix.stackexchange.com/questions/389717