Visual Studio: ToolsVersion vs PlatformToolset. Do they need to correspond?

Sergey Kolesnik 1 Reputation point
2022-01-10T11:05:40.23+00:00

The MSDN documentation says that you need to use the Runtime version that correpsonds to the Platform Toolset version, otherwise it leads to UB.
I have a legacy project that was migrated to Visual Studio 2019, and has a <PlatformToolset>v142</PlatformToolset> but <Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">.
I know that there should be ToolsVersion="Current" or ToolsVersion="16.0" which is generated by CMake.
I get no warnings from Visual Studio when building the project, but I am concerned if specifing incompatible versions will lead to linking the wrong CRT version.

When I build with /MD it seems that everything is ok:

   Image has the following dependencies:  
     
       msi.dll  
       MSVCP140D.dll  
       VCRUNTIME140D.dll  
       VCRUNTIME140_1D.dll  
       ucrtbased.dll  
       KERNEL32.dll  

But the legacy project uses /MT and I can't determine which CRT is used from the dumpbin output for .obj file:

   Linker Directives  
      -----------------  
      /FAILIFMISMATCH:_MSC_VER=1900  
      /FAILIFMISMATCH:_ITERATOR_DEBUG_LEVEL=2  
      /FAILIFMISMATCH:RuntimeLibrary=MTd_StaticDebug  
      /DEFAULTLIB:libcpmtd  
      /FAILIFMISMATCH:_CRT_STDIO_ISO_WIDE_SPECIFIERS=0  
      /DEFAULTLIB:uuid.lib  
      /DEFAULTLIB:uuid.lib  
      /DEFAULTLIB:LIBCMTD  
      /DEFAULTLIB:OLDNAMES  

Is ToolsVersion even meaningfull and can it lead to linking the wrong CRT version?
How can I inspect which version of Runtime is linked when compiled with /MT?

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,685 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. RLWA32 44,951 Reputation points
    2022-01-11T15:22:41.567+00:00

    According to the documentation of the Project Element at https://learn.microsoft.com/en-us/visualstudio/msbuild/project-element-msbuild?view=vs-2022 ToolsVersion is an optional attribute that refers to "The version of the Toolset MSBuild uses to determine the values for $(MSBuildBinPath) and $(MSBuildToolsPath)". Drilling down further into the documentation of MSBuild Reserved and Well-known properties at https://learn.microsoft.com/en-us/visualstudio/msbuild/msbuild-reserved-and-well-known-properties?view=vs-2022 $(MSBuildBinPath) is for "The absolute path of the folder where the MSBuild binaries that are currently being used are located (for example, C:\Windows\Microsoft.Net\Framework\<versionNumber>). This property is useful if you have to refer to files in the MSBuild directory." and $(MSBuildToolsPath) is for "The installation path of the MSBuild version that's associated with the value of MSBuildToolsVersion."

    So my understanding from this documentation is that ToolsVersion is not related to the C/C++ runtime.

    1 person found this answer helpful.

  2. YujianYao-MSFT 4,281 Reputation points Microsoft Vendor
    2022-01-11T07:03:01.02+00:00

    Hi @Sergey Kolesnik ,

    How can I inspect which version of Runtime is linked when compiled with /MT?

    The answer in the link solves the problem:

    /DEFAULTLIB:MSVCRTD (module compiled with /MDd)
    or
    /DEFAULTLIB:MSVCRT (module compiled with /MD)
    or
    /DEFAULTLIB:LIBCMT (module compiled with /MT)

    And I recommend you to read this document, it will help you understand /MT and /MD.

    Best regards,

    Elya


    If the answer is the right solution, please click "Accept Answer" and 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.


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.