CMake with Build Tools for Visual Studio 2019 (Just the CLI not the IDE)

Viewed 470

I am trying to build my code with Cmake and Visual Studio 2019 Build Tools. Note that I'm not using the IDE only the Build Tools CLI. When I try to generate my project like this:

cmake -G "Visual Studio 16 2019" -S . -B .

CMake returns this:

 Generator

    Visual Studio 16 2019

  could not find specified instance of Visual Studio:

    C:/Program Files (x86)/Microsoft Visual Studio/2019/Community

How can I make CMake to look for the Build Tools location and not for the IDE location?

2 Answers

I haven't used Visual Studio in years, but according to CMake's documentation on the Visual Studio 16 2019 generator, you can use a CMake variable or an environment variable to control which instance you use.

VS 2019 supports multiple installations on the same machine. The CMAKE_GENERATOR_INSTANCE variable may be set as a cache entry containing the absolute path to a Visual Studio instance. If the value is not specified explicitly by the user or a toolchain file, CMake queries the Visual Studio Installer to locate VS instances, chooses one, and sets the variable as a cache entry to hold the value persistently.

When CMake first chooses an instance, if the VS160COMNTOOLS environment variable is set and points to the Common7/Tools directory within one of the instances, that instance will be used. Otherwise, if more than one instance is installed we do not define which one is chosen by default.

  1. Make sure you have all of the necessary individual components selected when installing the build tools.

  2. Make sure CMake can find the tools (e.g. add the paths to your Path variable).

As a proof of concept for my case, I added the following to my user Path variable for lib.exe, cl.exe, link.exe, etc...:

C:/Program Files (x86)/Microsoft Visual Studio/2019/BuildTools/VC/Tools/MSVC/14.27.29110/bin/Hostx64/x64

And the following path for mt.exe and rc.exe:

C:/Program Files (x86)/Windows Kits/10/bin/10.0.18362.0/x64
Related