45

As the Visual Studio installer is new from Visual Studio 2017 version, I cannot located the Visual C++ component, explained here.

How do I proceed to get the vsvars32.bat in VS2017?

Community
  • 1
  • 1
Tobias Moe Thorstensen
  • 8,861
  • 16
  • 75
  • 143

6 Answers6

56

VS2017 suffers from very seriously brain-damaged install path location choices. Most damning dumb thing they did is to make the edition name (Professional, Enterprise, probably Community) part of the path. This makes it quite difficult to find tools back reliably from one machine to another.

There is one environment variable that I think can solve the problem, the VSAPPIDDIR variable stores the path to the folder where the IDE is installed (devenv.exe). So if you want to run vcvars32.bat from a build event then you'd use

   call "%vsappiddir%..\..\VC\Auxiliary\Build\vcvars32.bat" x86

Note that it is vc, not vs, vsvars32.bat no longer exists. You could possibly favor the "Developer Command Prompt:

   call "%vsappiddir%..\tools\vsdevcmd.bat"

But judging from your link, you actually want to run the editbin.exe utility:

   "%vsappiddir%..\..\VC\Tools\MSVC\14.10.25017\bin\HostX86\x86\editbin.exe" args...

The 14.10.25017 version number is no joy whatsoever either, no real insight in how that is going to change from one update to the next. It probably will.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • 5
    Alternatively, you could use the [vswhere](https://github.com/Microsoft/vswhere) utility to find the "appdir". – Christian.K Mar 16 '17 at 08:31
  • 27
    Just installed VS 2017, and %VSAPPIDDIR% is not defined. Bummer. – Eddie Paz May 17 '17 at 17:47
  • 12
    Fyi: If you are looking for the **editbin.exe** you´ll find it in the visual studio installer package named: **VC++ 2017 v141-Toolset (x86,x64)** – Sebastian Schumann Jul 12 '17 at 12:45
  • this doesn't worked for me on VS 2017, @Hans can you please update your answer with the valid command ? – Ehsan Sajjad Oct 11 '17 at 12:19
  • Nobody tells me why it doesn't work for them. I'm guessing that they are not trying to do this from a build event. If you don't do this from a build event then simply use the Developer Command Prompt shortcut to get the environment setup correctly. – Hans Passant Oct 11 '17 at 12:25
  • 4
    _"make the edition name (Professional, Enterprise, probably Community) part of the path"_ - that is to allow multiple editions to coexist on the same machine. Previously, this was impossible because of the identical paths - they were breaking each other in unpredictable ways. – ivan_pozdeev Oct 21 '17 at 22:56
  • 2
    @Vera: Variable %VSAPPIDDIR% is NOT a *system* environment variable and only available when script is called as `PreBuildEvent`. @Hans: It seems equivalent to `set DEVENVDIR=%~5` – Jack Miller Nov 16 '17 at 10:08
  • Using Visual Studio 2019 the version number changed to 14.21.27702, so `..\..\VC\Tools\MSVC\14.21.27702\bin\Hostx64\x64\editbin.exe` – Wollmich Jul 15 '19 at 08:49
21

I know the question is (well) answered, but I would like to share how I solved the problem hoping it will help people googling for a solution

@echo off

set INSTALLPATH=

if exist "%programfiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe" (
  for /F "tokens=* USEBACKQ" %%F in (`"%programfiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe" -version 15.0 -property installationPath`) do set INSTALLPATH=%%F
)

echo INSTALLPATH is "%INSTALLPATH%"

REM Save current dir for later
pushd %CD%

if NOT "" == "%INSTALLPATH%" (
  call "%INSTALLPATH%\Common7\Tools\VsDevCmd.bat"
) else (
  goto ERROR_NO_VS15
)

:WORK
REM Retrieve the working dir and proceed
popd
echo Doing work in %CD%
svcutil this_is_just_an_example
goto END

:ERROR_NO_VS15
echo Visual Studio 2017 Tools Not Available!

:END
echo Processing ends.

A small explanation of the above script.

vswhere.exe is a single-file, native executable you can download or redistribute with your build and deployment environments to locate Visual Studio or other products installed with the new installer for Visual Studio 2017 (from the vswhere wiki)

Starting with Visual Studio 15.2 (26418.1 Preview) vswhere.exe is installed in %ProgramFiles(x86)%\Microsoft Visual Studio\Installer (use %ProgramFiles% in a 32-bit program prior to Windows 10). This is a fixed location that will be maintained (as noted here)

This allows developers to query for several important features of a Visual Studio 2017 (and above) installation. Furthermore, the tool was designed to allow different flavors of Visual Studio (Community Edition, Professional, ...) to be installed on the same machine.

You can find several usage examples here.

As for the script, the first relevant part

if exist "%programfiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe" (
  for /F "tokens=* USEBACKQ" %%F in (`"%programfiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe" -version 15.0 -property installationPath`) do set INSTALLPATH=%%F
)

Queries vswhere for the installation Path of Visual Studio 2017 (-version 15.0) and sets a variable named INSTALLPATH.

It should be noted that on a 32 bits OS, you should use %programfiles% instead of %programfiles(x86)%.

Due to this issue, the script saves the current directory for later retrieval.

The script then proceeds to test the contents of the INSTALLPATH variable. If the variable is not empty, it appends "Common7\Tools\VsDevCmd.bat" to it (which is a well known relative path where one can find development tools for the corresponding Visual Studio installation). Otherwise, the script jumps to an error message and exits (you may opt to return a non-zero error code).

If all went well, you have now a full fledge Visual Studio development environment at your disposal.

The script now proceeds to retrieve the original directory and execute something, in this case a dummy call to svcutil.

jamars
  • 311
  • 2
  • 6
  • 1
    It could do with a comment as to what this does and when we should run it. – Steve Smith Apr 03 '18 at 09:32
  • 1
    @SteveSmith, I added some comments on script usage and referenced some resources that helped me. I hope this makes the contribution more useful for others. – jamars Apr 05 '18 at 18:29
  • 1
    This is excellent. Thank you. This should be the accepted answer. – Webreaper Jul 27 '18 at 10:10
  • 3
    Great answer. For those who like to be terse, the following will suffice: `for /F "tokens=* USEBACKQ" %%F in (\`"%PROGRAMFILES(x86)%\Microsoft Visual Studio\Installer\vswhere.exe" -property installationPath\`) do set VSPATH=%%F` followed by `call "%VSPATH%\Common7\Tools\VsDevCmd.bat"` – Neo Feb 26 '19 at 08:04
  • What if you have multiple instances of VS installed, how could I modify this for loop to detect the "latest" version? – rollsch Nov 14 '19 at 04:08
  • 1
    @rolls, to detect the latest version you could try and run something like the following: `vswhere.exe" -legacy -latest -version [11.0,16.0) -property installationPath`. This would give you the install path for the latest Visual Studio version, including legacy ones, from 11.0 (inclusive) to 16.0 (exclusive). If you just need the "latest", without worrying about legacy versions, simply run `vswhere.exe" -latest -property installationPath`. – jamars Nov 14 '19 at 10:09
  • Perfect, thank you. I couldn't find the "any version between x and y" that you posted in the documentation (maybe I missed it). – rollsch Nov 15 '19 at 00:12
  • @jamars that doesn't work if you only have a VS2019 preview installed. Preview is version 16.0_dfkgjhjdfkjgh and some guid on the end. It seems vswhere is bugged and can't match a preview version so on machines that only have preview installed, I've had to hard code the path. Not sure if there is a better way. – rollsch Dec 04 '19 at 03:21
  • @rolls, I never had a preview installed so I don't have any way to test this, but [here](https://github.com/Microsoft/vswhere/issues/158) someone mentioned using a `-prerelease`switch – jamars Dec 04 '19 at 12:33
14

Just change "vsvars32.bat" to "vsdevcmd.bat". This is compatible at least back to VS2015.

call "$(DevEnvDir)..\tools\vsdevcmd.bat"
editbin /largeaddressaware "$(TargetPath)"
flatline
  • 42,083
  • 4
  • 31
  • 38
  • 3
    DevEnvDir is set when built through VisualStudio, but not when built from the command line – BrianH Aug 15 '17 at 21:10
  • If you build from the command line then simply run vsdevcmd.bat yourself with the shortcut provided in the Start menu. The easy way. It is only complicated if you need to do this in a build event. – Hans Passant Apr 08 '18 at 10:40
5

Microsoft changes the vcvars32.bat to VsDevCmd.bat in VS 2017 link

I use vcvars32.bat in pré-build (project properties->build events), so for me, I've changed:
"$(DevEnvDir)..\..\VC\bin\vcvars32.bat"
to
$(DevEnvDir)\..\Tools\VsDevCmd.bat"
and it worked!

Edit: In Visual Studio 15.9 the path was changed (as Fütemire said in the comment), so I had to change:
$(DevEnvDir)\..\Tools\VsDevCmd.bat"
to:
C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\Tools\VsDevCmd.bat

*You can create an environment variable to store the path. I couldn't find an already defined.

fsbflavio
  • 664
  • 10
  • 22
  • The path you mentioned seems to be from Visual Studio 2015 (`Microsoft Visual Studio 14.0`). – Roi Danton Aug 02 '18 at 12:13
  • Prefer `call "$(DevEnvDir)\..\Tools\VsDevCmd.bat"` -- otherwise you've got a "works on my machine" solution. – Nigel Touch Sep 03 '18 at 16:08
  • Unfortunately as of VS 2017 v15.9 the `Tools` directory was moved up one level so $(DevEnvDir) macro no longer works, it's too far down the path. I really wish they would stop moving these directories around. Better yet, they should just make an env variable and macro straight to the tools directory, wherever they decide to put it next. – Fütemire Jan 15 '19 at 18:53
0

None of the mentioned solutions worked for me. After getting my head warmed up from the "vsvars32.bat missing" error - I went through every single line and there was too 'svcutil.exe' was missing.

I thought of fixing that and it worked. I got the following path in my machine for SvcUtil.exe:

C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin

I set the path in System Environment Variable and restarted my Visual Studio for just in case and it worked.

Hope this helps someone!

Edit: Very Strange - it works when I have "Lightweight solution load" enabled. As soon I'ml disabling "Lightweight Solution Load" - it starts giving me same error!

Sunny Sharma
  • 4,688
  • 5
  • 35
  • 73
  • 3
    Could you explain how `SvcUtil.exe` or the path to it helps finding `vsvars32.bat`? I do not see the link between question and your answer... – Jack Miller Nov 16 '17 at 09:40
0

In Visual Studio 2017 (inkl. C++ Build Tools installed) I do it in this manner in the post build events to check also for x86:

if not $(PlatformName) == x64 (
if exist "$(DevEnvDir)..\tools\vsdevcmd.bat" (
echo setting largeaddressaware to get 4gb access in 32 bit

call "$(DevEnvDir)..\tools\vsdevcmd.bat"
editbin /largeaddressaware "$(TargetPath)"
)
)
howardButcher
  • 331
  • 1
  • 9