17

I want to write a simple batch script that loads the Visual Studio build environment using vcvars32.bat and then continue with the build, using vcbuild. However, my script won't execute past the invocation of vcvars32.bat. The last output I get is:

Setting environment for using Microsoft Visual Studio 2008 x86 tools.

As you can see I'm using Visual Studio 2008. Here is my simplest batch script:

@echo off
"C:\Program Files\Microsoft Visual Studio 9.0\VC\bin\vcvars32.bat"
vcbuild
Jared Oberhaus
  • 14,547
  • 4
  • 56
  • 55

3 Answers3

30

You have to use call in your batch script, or the termination of vcvars32.bat will terminate your own batch script. Therefore your script should be:

@echo off
call "C:\Program Files\Microsoft Visual Studio 9.0\VC\bin\vcvars32.bat"
vcbuild
Jared Oberhaus
  • 14,547
  • 4
  • 56
  • 55
  • 6
    This happens even without an exit statement in vcvars32.bat. Calling a batch file without 'call' runs the batch file and exits when it gets to the end of the 2nd batch file. (See also Raymond Chen's recent blog article on this subject: http://blogs.msdn.com/oldnewthing/archive/2009/04/16/9551818.aspx) – Simon Nickerson Apr 17 '09 at 23:19
  • 1
    You should use %VCINSTALLDIR% in place of the Program files reference which will make it portable for other build machines or if you happen to install VS somewhere else. – Paul Alexander Jan 15 '10 at 01:05
4

You'll also want to check that the script hasn't run already or you'll start running out of memory if you invoke your script over and over in the same console.

IF '%VSINSTALLDIR%' NOT EQU '' THEN EXIT 0
Paul Alexander
  • 31,970
  • 14
  • 96
  • 151
2

The exact program files path depends on whether you have a 32 or 64 bit OS and where you installed Visual Studio. Use the VS100COMNTOOLS environment variable which Visual Studio sets up at install time to solve this problem generically.

call "%VS100COMNTOOLS%\..\..\VC\bin\vcvars32.bat"
...

Note that each version of Visual Studio has a specific environment variable based on its underlying version number.

Visual Studio 2005    VS80COMNTOOLS
Visual Studio 2008    VS90COMNTOOLS
Visual Studio 2010    VS100COMNTOOLS
Visual Studio 2012    VS110COMNTOOLS
Visual Studio 2013    VS120COMNTOOLS
Visual Studio 2014    VS130COMNTOOLS
Visual Studio 2015    VS140COMNTOOLS
Visual Studio 2016    VS150COMNTOOLS
Visual Studio 2017    VS160COMNTOOLS

You get the idea.

SSpoke
  • 5,656
  • 10
  • 72
  • 124
Jim Balkwill
  • 328
  • 5
  • 12