Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to concatenate variable in Makefile recipe

I get this Makefile:

LIST = foo bar                                                                  
                                                                                
$(LIST):                                                                        
  echo $@

When I run make targets the outputs works as desired:

$ make foo
echo foo
foo

$ make bar
echo bar
bar

But if I concatenate a string another-, only another-foo works:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

LIST = foo bar

another-$(LIST):
  echo $@
$ make another-foo 
echo another-foo
another-foo

$ make another-bar
make: *** No rule to make target 'another-bar'.  Stop.

How can I concatenate a target to expand to all values in my variable?

>Solution :

The behavior you observe is standard: when writing

another-$(LIST)

make just replaces $(LIST) with its content, yielding another-foo bar.

That is, this has nothing to do with, e.g., a bash brace expression such as another-{foo,bar}.

Yet, you can achieve what you want by doing something like:

LIST = foo bar

$(addprefix another-,$(LIST)):
    echo "$@"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading