Visual studio install path and directory query

Probably a basic question. Through couldn’t find a good answer.
Suppose I am running a cmake project for visual studio build engine.
Sometimes, you need the sdk directory and others VS directories to set the include path in order to find those VS headers.

So if you run everything under a visual studio native command shell then you can get those directories using, for example: $ENV{WindowsSdkDir}.
But if you do not invoke from VS command shell but from a CI system, then what is the recommended way to get all this visual studio environment and directories?

Thanks for your help,
Serge

You should use vcvarsall.bat to load the compiler environment as needed. That’s basically what the developer command prompt does on startup.

There are basically 3 ways to setup an environment for MSVC.

Method 1

As @ben.boeckel correctly pointed out you can use a developer command prompt.

You can search in your taskbar for them once you have installed visual studio and the necessary components.

Make sure to take into account the variants though. In a typical VS installation they can be found here:

C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Visual Studio 2019\Visual Studio Tools\VC

image

  • x64 Native Tools Command Prompt for VS 2019.lnk
  • x64_x86 Cross Tools Command Prompt for VS 2019.lnk
  • x86 Native Tools Command Prompt for VS 2019.lnk
  • x86_x64 Cross Tools Command Prompt for VS 2019.lnk

The one that begin with x86 are basically useless unless you are using a 32 bit OS. Technically you can use them on a 64 bit OS, however they are prone to failure on big enough applications. So I’d recommend only using the first 2.

  • x64 Native Tools Command Prompt for VS 2019.lnk ← Use 64 bit tools to compile for 64 bit systems
  • x64_x86 Cross Tools Command Prompt for VS 2019.lnk ← Use 64 bit tools to compile for 32 bit systems

What do these command prompts do? They setup the necessary environment variables for the MSVC compiler to do it’s job. For example the INCLUDE environment variable can be used to setup system level environment variables.

Method 2

Microsoft has streamlined Method 1 with their integrated CMake support in Visual Studio. This way you can basically let Visual Studio handle it for you.

You can read more about it here:

Method 3

Reverse engineer what the scripts in method 1 are doing and recreate the environment you need by hand.

I don’t recommend option 3.

1 Like

Thank you so much guys for your detailed answer.
I do apreciate .
I think i will go with method 1 which seems the straightforward one.
So i need to find that bat file execute it and extract the environment variable generated and inject those to the cmake process. I’ll check how to do that in python.
Thanks again.

Using PowerShell is probably way easier since it can be “sourced” in the shell directly. If your CI is using cmd anyways, just call C:\path\to\vcvarsall.bat args you may need. CMake uses this PowerShell code and gets its arguments from the CI environment.

Thanks Ben,
not really familiar with powershell.
Is there some example how to use it?

Thanks,
Serge

I’d just look at what shell your CI uses by default and go with it. If it’s cmd, just call the script. If it’s powershell, you already have an environment where you can use it.

From VS2017 onwards, Microsoft has provided the “vswhere.exe” binary that locates versions of Visual Studio for you. They recommend that this be used over alternative methods like searching the registry etc.

I have recently integrated this into our cmake script to find certain Visual Studio runtime files for our installer… This is how I did it:

  find_program(_vswhere_tool 
    NAMES vswhere 
    PATHS "$ENV{ProgramFiles\(x86\)}/Microsoft Visual Studio/Installer")
  if (NOT ${vswhere})
    message(FATAL_ERROR "Could not locate vswhere.exe - unable to source vc redistributable")
  endif()

  execute_process(
    COMMAND "${_vswhere_tool}" -latest -products * -requires Microsoft.VisualStudio.Component.VC.Tools.x86.x64 -property installationPath
    OUTPUT_VARIABLE _vs_install_loc_out
    RESULT_VARIABLE _vs_where_exitcode
    OUTPUT_STRIP_TRAILING_WHITESPACE
  )

  file(TO_CMAKE_PATH "${_vs_install_loc_out}" _vs_install_loc)

I had multiple problems with the VS 2019 build tools when using them exclusively (without a full install of the IDE) in a Windows Server Core environment.

I reported the issue below. The vswhere doesn’t treat the build tools as a valid VS install, it can only recognize IDEs (Community or Professional).

https://gitlab.kitware.com/cmake/cmake/-/issues/22197

This blocks me from moving on from VS2015 build tools if I want a tiny Windows Docker environment, and I do want those, since they boot up faster in Google Cloud (like way faster, the IDE install required makes the docker image huge).

Thanks @eri0. Build Tools only is not a use case that I need right now, but good to know about this issues. However, just going by the comments in Support the BuiltTools SKU by refack · Pull Request #8 · microsoft/vswhere (github.com) they have added support for the Build Tools installations to vswhere?

It’s not detected the same way the IDEs are, making vswhere useless. I haven’t seen anything that depends on vswhere being able to detect vs build tools when they are installed alone. The maintainer of vswhere closes any attempt of changing this. It makes no sense for me, you can use the CMake provided by Microsoft and it will still fail. I am not sure what I will do when they remove the VS 2015 build tools from their website, maybe I will need to figure how to use MSYS2 and MinGW and switch to it.

Thank you guys for the valuable information.
Serge