0

So, I have a makefile that works great. In it I set a variable LD_LIBRARY_PATH_VAL to contain all the paths to all the libraries that the executable needs.

Now, for convenience I want a rule that sets the LD_LIBRARY_PATH environment variable by doing: make set_ld_lib_path, such that I get the following output:

> echo $LD_LIBRARY_PATH
>       (empty)
> make set_ld_lib_path
> echo $LD_LIBRARY_PATH
> path/to/lib1:path/to/lib2

Here is my rule as it is at the moment:

# Set the LD_LIBRARY_PATH needed to find all dependant libs - for convinience
LD_LIBRARY_PATH_VAL := test123
.PHONY: set_ld_lib_path
set_ld_lib_path: export LD_LIBRARY_PATH := $(LD_LIBRARY_PATH_VAL)
set_ld_lib_path:
    @$(ECHO) "$(COLOUR_ACT)LD_LIBRARY_PATH:$(COLOUR_RST) $$LD_LIBRARY_PATH"

Note: I put the line LD_LIBRARY_PATH_VAL := test123 just to make this a complete example - so I would want to get the output for LD_LIBRARY_PATH to be test123, but in my real makefile this would print a long list of lib paths.

So I have read lots of ways to set / export variables, but I have not found a way to do what I want here. I think it may not be possible because I can't update the variable of the calling shell... but in case it is possible I am asking the question :)

Info update I have gnu make version:

GNU Make 4.1 Built for x86_64-pc-linux-gnu Copyright (C) 1988-2014 Free Software Foundation, Inc. Licence GPLv3+: GNU GPL version 3 or later http://gnu.org/licenses/gpl.html This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law.

code_fodder
  • 15,263
  • 17
  • 90
  • 167
  • 1
    Possible duplicate of [How to set environment variable in Makefile](https://stackoverflow.com/q/23843106/608639) and [Setting environment variable in Makefile](https://stackoverflow.com/q/8022586/608639) – jww Dec 19 '18 at 00:24
  • One possibility consists in adapting your Makefile to echo `export LD_LIBRARY_PATH=` in the recipe of `set_ld_lib_path` and then, from the shell, invoking `eval $(make set_ld_lib_path)`. It's this same trick that is used by other commands like, for instance, `ssh-agent`: they echo other shell commands to be evaluated by the parent shell. – Renaud Pacalet Dec 19 '18 at 08:11
  • 2
    I have no doubt that this is a duplicate of _some_ question on SO, but it is definitely **not** a duplicate of the questions linked here. Both of those discuss how to set an environment variable for children processes that make invokes. This question is asking how to get make to set an environment variable in the process that invoked make. These are completely different things. – MadScientist Dec 19 '18 at 13:17
  • @MadScientist: I cannot agree more. This question is probably a duplicate of [this one](https://stackoverflow.com/questions/52114730/is-it-possible-to-set-environment-variables-in-makefile-to-be-used-after) but not of the other questions referred to by those who voted to close. – Renaud Pacalet Dec 19 '18 at 13:42
  • @MadScientist yes thanks for that - I agree!. I could not find this particular question - you would think it was all over the internet, but not that I could find - hence my question : ) ... but as usual, the slightest whiff of a similar question and the close-police are all over it : ( – code_fodder Dec 19 '18 at 13:42
  • @RenaudPacalet - yes, I would agree with that, it does duplicate that question and I would be happy to take the answer from that question (and up-voted in that question). – code_fodder Dec 19 '18 at 13:44
  • @RenaudPacalet (part 2) - in answer to your first comment (and via the link you posted) that would be good enough for my purpose, thanks – code_fodder Dec 19 '18 at 13:46

1 Answers1

3

Yeah, you cannot do that. It's a fundamental aspect of all Unix systems for 50 years that a child process cannot modify the environment of its parent. Make can do many things, but it cannot subvert that basic restriction.

MadScientist
  • 92,819
  • 9
  • 109
  • 136