Select MSVC build tools version with Visual Studio's CMake / CMakeSettings.json

Patrik Huber 20 Reputation points
2024-04-08T13:16:12.7633333+00:00

Hi,

I'm using Visual Studio's C++ CMake functionality, i.e. "File -> Open -> CMakeLists.txt". I'm on the latest VS 2022 17.9.5. I'd like to specify an exact MSVC build tools version to use, for example MSVC 14.29, 14.30 or 14.39 (installed via the Visual Studio Installer).

If you use CMake outside of VS, there's the cmake-gui -T option where you can at least specify "v142" or "v143" (but also not an exact version). But Visual Studio's CMake, CMakeSettings.json, and the graphical CMakeSettings.json editor don't seem to support even that, yet alone specify an exact version.

The only piece of vaguely relevant docs I could find is https://learn.microsoft.com/en-us/cpp/build/customize-cmake-settings?view=msvc-170#toolset, but the section on "Toolset"/inheritedEnvironments is very unhelpful. Further searching for inheritedEnvironments/inheritEnvironments also leads me to think that you can only specify the target platform with that setting, e.g "x64_x64", but not an actual build tools version.

So how do I specify an exact Build Tools version?

Is the way to do it to manually extract the path to cl.exe (e.g. "C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.39.33519\bin\Hostx64\x64\cl.exe") and then pass that as CMAKE_CXX_COMPILER?

Thanks!

C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,737 questions
0 comments No comments
{count} votes

Accepted answer
  1. Minxin Yu 11,756 Reputation points Microsoft Vendor
    2024-04-09T02:39:33.4766667+00:00

    Hi, @Patrik Huber

    In CMakePresets.json file

    Add "toolset" node.

           "condition": {
             "type": "equals",
             "lhs": "${hostSystemName}",
             "rhs": "Windows"
           }
         },
         {
           "name": "x64-debug",
           "displayName": "x64 Debug",
           "inherits": "windows-base",
           "architecture": {
             "value": "x64",
             "strategy": "external"
           },
           "cacheVariables": {
             "CMAKE_BUILD_TYPE": "Debug"
           },
           "toolset": {
             "value": "host=x64,version=14.39"
           }
         },
    

    User's image

    Best regards,

    Minxin Yu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.