Over in Apache REEF, we needed to make our solution compile in both Visual Studio 2013 and 2015. The main impediment to that was the C++ code in the project, which needs to be compiled with the “right” compiler, depending on the version of Visual Studio used. Below, I describe how we created a project file that can be compiled uniformly across Visual Studio versions.

If we wanted to only support Visual Studio 2015, life would have been simple: Right click the offending project in Visual Studio 2015, and have it converted. However, this creates a project file that can’t be compiled in Visual Studio 2013. Instead, we changed the project file support both Visual Studio 2013 and 2015.

When opening .vcxproj file created with Visual Studio 2013, you typically find something like this inside:

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
    <PlatformToolset>v120</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
    <PlatformToolset>v120</PlatformToolset>
</PropertyGroup>    

For Visual Studio 2015, you will find almost the same:

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
    <PlatformToolset>v140</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
    <PlatformToolset>v140</PlatformToolset>
</PropertyGroup>    

The only difference is the <PlatformToolset>, which needs to be set to v120 for Visual Studio 2013 and v140 for Visual Studio 2015. Hence, we can set the <PlatformToolset> based on the version of Visual Studio used to execute this build. That version is found in $(VisualStudioVersion). It is 12.0 for Visual Studio 2013 and 14.0 for Visual Studio 2015.

Putting it all together allowed us to follow a very simple approach. We removed the <PlatformToolset> elements from these <PropertyGroup>s and instead added a new one, that sets it based on the version of Visual Studio used.

<!--
    Switch the PlatformToolset based on the Visual Studio Version
-->
<PropertyGroup>
    <!-- Assume Visual Studio 2015 / 14.0 as the default -->
    <VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
</PropertyGroup>
<!-- Visual Studio 2013 (12.0) -->
<PropertyGroup Condition="'$(VisualStudioVersion)' == '12.0'">
    <PlatformToolset>v120</PlatformToolset>
</PropertyGroup>
<!-- Visual Studio 2015 (14.0) -->
<PropertyGroup Condition="'$(VisualStudioVersion)' == '14.0'">
    <PlatformToolset>v140</PlatformToolset>
</PropertyGroup>
<!--
    End of: Switch the PlatformToolset based on the Visual Studio Version
-->

With this code in place, both Visual Studio 2013 and 2015 were able to compile the REEF code.