0

After reading this answer: enter link description here, of using

$(info    text…)
$(info $(var))

I have tried it in makefile:

.PHONY: all

all: ; $(info $(var))

(where the var was set outside in bash), I got output:

make: Nothing to be done for 'all'. So how can I print variables defined outside of makefile, in makefile?

milanHrabos
  • 2,010
  • 3
  • 11
  • 45
  • Please provide a complete set of commands, cut and pasted. For example how was `var` actually set? How did you run make? What is the name of your makefile? I recommend you change your output so that it prints more than just a blank line if the variable is not set, something like `$(info var = '$(var)')` so it's easier to understand what's happening. – MadScientist Oct 15 '20 at 15:20

1 Answers1

0

How to print environment variables from makefile?

You can do exactly as you say in the question. For example:

Here's the Makefile:

.PHONY: all
all: ; $(info VAR='$(VAR)')

Now set the environment variable VAR on the command line before running make:

$ VAR=foo make
VAR='foo'
make: Nothing to be done for 'all'.

Or export it and then run make:

$ export VAR=bar
$ make
VAR='bar'
make: Nothing to be done for 'all'.

So, you need to look elsewhere to find out why this is not working for you.

Note that you don't need to put the $(info) in a recipe; For example here's a one line makefile printing an environment variable:

Makefile:

$(info VAR='$(VAR)')

Set the environment variable VAR and run make:

$ VAR=foo make
VAR='foo'
make: *** No targets. Stop.
Matt Wallis
  • 873
  • 5
  • 25