0

I have a common target with setting a bunch of env variables something like this in Makefile

# file common.mk
setup :
    export LD_LIBRARY_PATH=${MY_HOME}/LIBS:${LD_LIBRARY_PATH}
    export MY_VERSION=2.2
    ...

Now I want to use these variables in all make targets

#file Makefile

include ${MY_RUN_DIR}/common.mk
run : setup
    # export all the variables 
    # do domething

Now when I do make run I cannot see the variables being set. It is because all the variables are set only in "setup" target. I have like 100s of Makefile where I will have to add all the export variables and I would prefer to add only one line instead to export all variables.

How can I export variables from one target to all targets wherever I want?

Beta
  • 96,650
  • 16
  • 149
  • 150
Nishikant
  • 67
  • 5
  • Just to point out the obvious, the variables are not even exported inside the `setup` target unless you are using `.ONESHELL` or similar. The variables are exported in *one individual* shell invocation each, and when that shell terminates and the next recipe line executes, they no longer are. This is a common beginner mistake and a common FAQ; see https://stackoverflow.com/questions/42157800/makefile-variable-assignment-error-in-echo – tripleee Sep 23 '20 at 04:01

1 Answers1

1

Just don't put the assignments in a rule:

# file common.mk

export LD_LIBRARY_PATH=${MY_HOME}/LIBS:${LD_LIBRARY_PATH}
export MY_VERSION=2.2
...

and don't bother with the prerequisite setup:

#file Makefile

include ${MY_RUN_DIR}/common.mk
run:
#  do something, such as
    @echo my version is $$MY_VERSION
Beta
  • 96,650
  • 16
  • 149
  • 150