c++visual-studiomsbuildbuild-tools

Detect the Visual Studio build tools version at compile time


Is there a way to get the build tools version being used by Visual Studio using a preprocessor flag or any other way such that the code/compiler can make compiler time decisions on how to compile the code?

There are some differences in the header files/code when using build tools v140 vs v143, however to keep it compatible with both versions of the build tools I would like to encapsulate some of the code using a #ifdef or something similar so it can build with either version without making any changes to the code and keep it backward compatible.


Solution

  • Yes you can add the following to your Preprocessor Definitions:

    MSC_PLATFORM_TOOLSET_$(PlatformToolset);

    Then use it like so:

    #ifdef MSC_PLATFORM_TOOLSET_v140 or #if defined ( MSC_PLATFORM_TOOLSET_v140 )

    For example:

    #if defined ( MSC_PLATFORM_TOOLSET_v140 )
    // Do something
    #elif defined ( MSC_PLATFORM_TOOLSET_v143 ) // VS2022 Toolset
    // Do something else
    #endif