makefilegnutargets

Makefile: If $^ gets all the prerequisites, what could be used to get all the targets


This is the idea:

$(filenameParam)_%.sfx : commun_prereq.per
     mycommand $< (all my $@)

where % can be any one letter, some times e,f some times e,s, some times h,r,t, basically any file with a letter in there.


Solution

  • For a given invocation of a rule there is only ever one target and the name of that target is put into the $@ automatic variable. When make is trying to build a target foo_h.sfx then $@ will be foo_h.sfx; when make is trying to build a target foo_r.sfx then $@ will be foo_r.sfx, etc.

    There is one additional wrinkle: it's possible to have multiple target patterns in a pattern rule. That causes make to build all those targets in a single invocation of the rule. In this case, $@ is the target that make wanted to build that caused this rule to be fired. If you want to get one of the other targets you need to construct it yourself using $* which matches the pattern. So:

    %.x %.y :
            @echo '@ = $@ / * = $*'
    

    will get the output:

    $ make foo.x
    @ = foo.x / * = foo
    
    $ make foo.y
    @ = foo.y / * = foo