Setting environment variables in cmd has no effect

6,466

Solution 1

Yes, you can extract environment to file:

cmd /C "C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\VC\Auxiliary\Build\vcvars64.bat" && set > d:\t.txt

Then you can include this file in makefile directly or use other option that fit your needs.

Solution 2

It's because the Batch files it runs are using Set to set the environment variables (C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\amd64\vcvars64.bat is what gets run when you run vcvarsall x64).

But Set only sets variables for the current command line session. You need to use Setx to have it "stick" between command line sessions.

For more info, perhaps check out these other SU questions and answers:

Edit after comments:

"But the set commands were in the default script"

VCVarsAll is intended to be run to setup the environment for the single build/action you are about to do, not to set them permanently.

Visual Studio installs specific "command prompt" entries for accessing the tools (From your link: "...and then choose one of the native-tool or cross-tool command prompts."), and those special command prompt shortcuts run a/the batch file to ensure things are set right when you use those shortcuts.

You can replicate those shortcuts with your own...

Make a new shortcut to cmd /k "C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\vcvarsall.bat" x64, which will run the batch file (using x64 as the environment option), and then keep the command-line window open.

Then use that shortcut to open the command prompt when you want to use the tools.

Share:
6,466

Related videos on Youtube

riv
Author by

riv

Updated on September 18, 2022

Comments

  • riv
    riv over 1 year

    I'm trying to set up paths to VS tools to run them in command line, as per https://msdn.microsoft.com/en-us/library/x4d2c09s.aspx

    I run vcvarsall x64 from elevated command prompt and check the contents of the INCLUDE variable:

    enter image description here

    Then I close command prompt, open it again and check the INCLUDE variable:

    enter image description here

    As you can see, the changes were lost when I closed CMD. How do I ensure that they make it to the system environment?

    I'm using Win 8.1 if it matters.

  • riv
    riv over 8 years
    But the set commands were in the default script.. you mean I have to edit the whole script to make it work (setx also has a slightly different syntax it seems)? Trying to run it locally is also problematic because I'm using FAR manager and it runs bat scripts in their own process so all changes are lost after the script finishes, so I have to use raw CMD for all my work, which is very inconvenient.