Talguy
Talguy

Reputation: 1095

How do I set an environment variable with NMake?

I'm making a makefile using NMake. I have a few utility programs that run to transform one file into a CPP file. The utility requires a specific windows environment variable to be set during the session. Can I set this variable within the Makefile or do I need to create a batch script that sets the variables before it calls NMake?

Upvotes: 3

Views: 5182

Answers (2)

Sz.
Sz.

Reputation: 3634

Use the !if [set ...] trick:

!if [set myvar=1]
!endif

# Macro values are fine, too, of course:
MYVAL=preprocessed
!if [set myvar=$(MYVAL)]
!endif

# Proof that myvar has really become an env. var. (see the output):
myvar=This is propagated to the env now!
mymacro=(But this isn't, of course.)

# This handy form works, too:
!if [set myA=1] || [set myB=2]
!endif

# But this will *NOT* make myX an env. var., if it hasn't been set yet:
!if [set myX=$(myX)]
!endif
myX=So you'll not see this...

all:
    set my

Output:

D:\tmp>nmake -f test.mak

Microsoft (R) Program Maintenance Utility Version 14.21.27702.2
Copyright (C) Microsoft Corporation.  All rights reserved.

        set my
myA=1
myB=2
myvar=This is propagated to the env now!

Upvotes: 3

Mike Kinghan
Mike Kinghan

Reputation: 61540

An environment variable ENVAR that exists when you invoke nmake is inherited as an nmake macro of the same name. If you change the value of ENVVAR in the makefile, like:

ENVAR=newval

then environment variable ENVAR assumes the value newval for commands that are run by the makefile. The target:

foo:
    echo "%ENVAR%"

will echo newval.

But you can't create an environment variable ENVAR like that. So you'll have to at least create the environment variable you want to use prior to invoking nmake

Upvotes: 0

Related Questions