Re: Setting Environment variable in Makefile (5686 Views)
Reply
Frequent Advisor
CA1490051
Posts: 81
Registered: ‎06-07-2007
Message 1 of 7 (5,686 Views)

Setting Environment variable in Makefile

Hi All,

I want to set environment variables in the Makefile.
for this i wrote as below

all : GO

GO :
export HAI=HELLO
echo $HAI

But , it is not working

can some one help me to achieve this.

thanks and regards
Vikram
Honored Contributor
Matti_Kurkela
Posts: 6,271
Registered: ‎12-02-2001
Message 2 of 7 (5,686 Views)

Re: Setting Environment variable in Makefile

I can see two problems.

1.) the "$" character is special to make. To get the shell to see one "$", you must use "$$" in the Makefile.

2.) Environment variables can only be inherited from parent to child processes, not vice versa.

In this case, the parent process is the 'make' that is processing the Makefile. When it reaches the "export HAI=HELLO" line, it will create a child process to run a shell to execute the export statement. The variable is set in the environment of the child process only. After the export statement is completed, the child process exits and its environment, including the new variable, is gone. The "echo $HAI" statement is executed by another child process.


Two ways to solve this problem:

1.) Join the variable initialization and the variable use into a single shell command, so that one child shell will execute both:

all: GO

GO:
export HAI=HELLO; \
echo $$HAI

Not very flexible, but it works.

2.) Use Makefile variables instead of environment variables:

HAI=HELLO

all: GO

GO:
echo $(HAI)

Note the different syntax for variable expansion.

MK
MK
Frequent Advisor
CA1490051
Posts: 81
Registered: ‎06-07-2007
Message 3 of 7 (5,686 Views)

Re: Setting Environment variable in Makefile

Hi MK,


In that case what i should do if i have to set an environment variable initially and use the same variable in the later part of the Makefile.

thanks and regards
Vikram
Honored Contributor
Steven Schweda
Posts: 9,096
Registered: ‎02-23-2005
Message 4 of 7 (5,686 Views)

Re: Setting Environment variable in Makefile

> I want to set environment variables in the
> Makefile.

Can you set the environment variables outside
the "make" file?

var1=value1 [...] make [...]

Or, can you use "make" macros (again, outside
the "make" file) instead of environment
variables?

make var1=value1 [...] [...]

Or, can you use "make" macros to set
environment variables on "make" action lines?

GO :
var1=$(VAR1) actual_command

A more realistic example of what you really
need to do might be helpful.
Frequent Advisor
CA1490051
Posts: 81
Registered: ‎06-07-2007
Message 5 of 7 (5,686 Views)

Re: Setting Environment variable in Makefile

Hi,

Basically i want to link some shared libraries present in the lib directory and build an executable.

so instead of hard coring the Path of these shared libraries in the make file, i wanted to give the relative path.

example -
cc -o abc abc.o xyz.o -L../../lib/lhai

but if i give this in the Makefile it is giving ld errors.

So i thought of setting the path in the
makefile through the environment variable.

Even if i do like below
LIB = -L../../lib/libhai.so \
-L../../lib/libhello.so

all : TARGET

TARGET :
cc cc -o abc $(OBJECTS)$(LIB)

the compiler gives ld PATH error

Should i have to set the SHLIB_PATH or LD_PATH

to compile I i do this can i execute this executable on a separate machine without setting any environment variabls


thanks and regards
vikram





Honored Contributor
Steven Schweda
Posts: 9,096
Registered: ‎02-23-2005
Message 6 of 7 (5,686 Views)

Re: Setting Environment variable in Makefile

> [...] it is giving ld errors.

Which errors?

I really doubt that "ld -L" cares whether
you specify an absolute path or a relative
path, but it probably does require you to
specify the right path. For one thing, a
relative path depends on where you are. You
might throw in a "pwd" near your link
command, to see where you are when you try to
do that link.

> Should i have to set the SHLIB_PATH or
> LD_PATH

That's what "-L" (and its friends) are for,
but you need to specify the right stuff,
which, I'd guess, you are not doing. But I
don't know where you are, or what's in
"../../lib/".

> So i thought of setting the path in the
> makefile through the environment variable.

This is why it's often better to describe the
actual problem than it is to ask how to
implement some poor solution to that problem.
Acclaimed Contributor
Dennis Handly
Posts: 25,277
Registered: ‎03-06-2006
Message 7 of 7 (5,686 Views)

Re: Setting Environment variable in Makefile

See my answers to your other thread:
http://forums12.itrc.hp.com/service/forums/questionanswer.do?threadId=1192582

>So i thought of setting the path in the makefile through the environment variable.

You'll get the same error until you get the path and options straight.

>Even if i do like below
LIB = -L../../lib/libhai.so \
-L../../lib/libhello.so

This isn't the correct syntax. The correct syntax is: -L../../lib -lhai -lhello

Note the shlibs will have to use the .sl suffix, since you are on PA.

>Should i have to set the SHLIB_PATH

SHLIB_PATH is a runtime (dld) path.

>can i execute this executable on a separate machine without setting any environment variables

See your other thread.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.