It is currently Tue, 17 May 2022 01:54:53 GMT



 
Author Message
 make - exporting shell variable to environment
using bash-2.05, linux, make-3.79.1

I have a makefile which makes a program which runs in either serial mode or
parallel (multithread) mode, according to a flag I edit at the top
of the Makefile (the flag being the number of processes I want to run,
OMP_NUM_THREADS=8 say)
.
The make and compilation goes one way or another according to ifdefs etc.

WHen the program runs it needs to know from the environment, the number
of processes it's to run. So I need to export OMP_NUM_THREADS back to the
environment from which I called make.

The command "export" in a Makefile only passes an environmental variable to
sub-makes.
I can't figure out how to get it back to the original environment.

I tried getting the Makefile to source a file ".profile" in the
same directory as the Makefile. I echo out the values, which are
correct when .profile is sourced, but the values aren't in the environment
when the Makefile executes.

Any ideas anyone?

Thanks Joe

--
Joseph Mack, NA3T, FM05lw EME(B,D)
AZ_PROJ map server at http://www.**-**.com/
mailto:jm...@wm7d.net



 Fri, 05 Mar 2004 04:42:32 GMT   
 make - exporting shell variable to environment

  m> using bash-2.05, linux, make-3.79.1

  m> The command "export" in a Makefile only passes an environmental
  m> variable to sub-makes.  I can't figure out how to get it back to
  m> the original environment.

You can't.

  m> Any ideas anyone?

It is a technical impossibility in UNIX for a subprocess to modify in
any way the environment of its calling process.  This is A Good Thing.

When you invoke make, it's a subprocess of your shell (the parent
process).  There's no way for make to "export" environment modifications
to the parent.

The only possible way this could be done would be for the parent process
to actively participate: for example, the makefile could write the
environment variable to a temporary file, then you could create an alias
or something in your shell which ran make, then sourced that temporary
file.

However, there is no _foolproof_ way to do this: first, it assumes that
all your users will have that alias defined; second, anyone running make
directly rather than through the alias (and there's no way to stop this,
really) will not have the output sourced.  Not to mention having a
_script_ which invokes make will also not do the right thing.

But it's all you can do.  If possible I would re-examine your situation
and try to come up with another alternative altogether.

--
-------------------------------------------------------------------------------
 Paul D. Smith <psm...@gnu.org>          Find some GNU make tips at:
 http://www.gnu.org                      http://www.paulandlesley.org/gmake/
 "Please remain calm...I may be mad, but I am a professional." --Mad Scientist



 Fri, 05 Mar 2004 12:10:11 GMT   
 make - exporting shell variable to environment

Thanks

I thought that might be one of the possibilities. Now I can stop
banging my head against the wall.

I found a way around it that is not what I wanted, but is good enough.

I have a target "run:" in the Makefile

run: executable
        VARIABLE="value" executable

the executable inherits $VARIABLE in its environment.

Thanks Joe

--
Joseph Mack, NA3T, FM05lw EME(B,D)
AZ_PROJ map server at http://www.wm7d.net/azproj.shtml
mailto:jm...@wm7d.net



 Sat, 06 Mar 2004 06:23:59 GMT   
 make - exporting shell variable to environment
<3BA50EB8.F7926...@wm7d.net> divulged:

compile it into the program, e.g.,

  OMP_NUM_THREADS=8
  foo: foo.c
    $(CC) -DOMP_NUM_THREADS=$(OMP_NUM_THREADS) -o $@ $<

where foo.c:

  #include <stdio.h>
  int main(void) {
    printf("%d threads\n", OMP_NUM_THREADS);
    return 0;
  }

--
okay, have a sig then



 Sat, 06 Mar 2004 13:00:33 GMT   
 make - exporting shell variable to environment
those who know me have no need of my name wrote:

I like it. Thanks
Joe
--
Joseph Mack, NA3T, FM05lw EME(B,D)
AZ_PROJ map server at http://www.wm7d.net/azproj.shtml
mailto:jm...@wm7d.net



 Tue, 09 Mar 2004 19:02:11 GMT   
 
   [ 5 post ] 

Similar Threads

1. difference between shell variable and environment variable

2. Difference between shell variables and environment variables!

3. export variables to calling shell in a shell-script

4. export variables to calling shell in a shell-script

5. getting environment variables from c shell using Bourne shell

6. setting/ exporting environment variable in a script file

7. export environment variable

8. Question re. export environment variable

9. Exporting Environment Variables


 
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software