4

Makefile

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

Expected behavior

$ make my_test
no toto around

$ make my_test toto
toto is defined

Current behavior

$ make my_test
no toto around

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

When I run make my_test I get the else text as expected no toto around. However

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

Makefile version

 $ make -v
   GNU Make 3.81

SLE version

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

PS

The point is to make make my_test verbose if toto, if toto not given then the command will run silently

  • 1
    Seems to work without error messages when using GNU make 4.2.1. – Kusalananda Sep 1 '17 at 9:28
  • @Kusalananda this should work in many servers, so it should be make 3.81 compliant too – smarber Sep 1 '17 at 9:37
  • 3
    In what way does make “behave weirdly”? You ask to build toto, there's no rule to build toto, make tells you that there's no rule to build toto. What else did you expect? – Gilles Sep 1 '17 at 21:59
  • If you want Make to check if toto is defined then you'll need to define it - make my_test toto=1 might be what you're after? – garethTheRed Sep 2 '17 at 5:44
  • 1
    @smarber You can't pass arguments to make without make interpreting them as its own command line flags, targets or variable assignments. – Kusalananda Sep 2 '17 at 18:26
6

You need to remove the dollar around toto, and also pass toto from the command line differently

Command line

make toto=1  my_test

Makefile

my_test:
ifdef toto
        @echo 'toto is defined'
else
        @echo 'no toto around'
endif
  • Actually I wanted to use ifdef because the value of toto is not interesting in my case, I only need to know if it's here or not – smarber Sep 1 '17 at 9:52
  • 1
    @smarber So is toto supposed to be a target or not? Please edit your question to clarify what you want to do. – Gilles Sep 1 '17 at 22:00
  • @Gilles toto is juste an option that I want to pass to make my_test, if toto is given I do some extra actions – smarber Sep 2 '17 at 11:11
  • @smarber Ok, then amisax's answer is what you need. – Gilles Sep 2 '17 at 11:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.