Skip to content

MSVC 2017 with Clang toolset: failed to run MSBuild command to get the value of VCTargetsPath

Hi,

I had installed both MSVC 2015 and MSVC 2017 and noticed that generating project for MSVC 2017 with Clang toolset uses libraries/includes/all environment from MSVC 2015 installation directory. Only recently this leads to linker errors.

After uninstalling MSVC 2015 and re-installing MSVC 2017, for next command line:

cmake -G "Visual Studio 15 2017 Win64" -T LLVM-vs2014 ..

I started getting next error:

CMake Error in CMakeLists.txt:
  Failed to run MSBuild command:

    C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/MSBuild/15.0/Bin/MSBuild.exe

  to get the value of VCTargetsPath:

    Microsoft (R) Build Engine version 15.6.85.37198 for .NET Framework
    Copyright (C) Microsoft Corporation. All rights reserved.
    
    Build started 21-Apr-18 9:51:10 PM.
    Project "C:\dev\cmake_clang\build_clang\CMakeFiles\3.11.1\VCTargetsPath.vcxproj" on node 1 (default targets).
    C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\VC\VCTargets\Microsoft.Cpp.Redirect.14.props(47,3): error MSB4019: The imported project "C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V140\Microsoft.cpp.props" was not found. Confirm that the path in the <Import> declaration is correct, and that the file exists on disk. [C:\dev\cmake_clang\build_clang\CMakeFiles\3.11.1\VCTargetsPath.vcxproj]
    Done Building Project "C:\dev\cmake_clang\build_clang\CMakeFiles\3.11.1\VCTargetsPath.vcxproj" (default targets) -- FAILED.
    
    Build FAILED.
    
    "C:\dev\cmake_clang\build_clang\CMakeFiles\3.11.1\VCTargetsPath.vcxproj" (default target) (1) ->
      C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\VC\VCTargets\Microsoft.Cpp.Redirect.14.props(47,3): error MSB4019: The imported project "C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V140\Microsoft.cpp.props" was not found. Confirm that the path in the <Import> declaration is correct, and that the file exists on disk. [C:\dev\cmake_clang\build_clang\CMakeFiles\3.11.1\VCTargetsPath.vcxproj]
    
        0 Warning(s)
        1 Error(s)
    
    Time Elapsed 00:00:00.02
    

  Exit code: 1



-- Configuring incomplete, errors occurred!
See also "C:/dev/cmake_clang/build_clang/CMakeFiles/CMakeOutput.log".

CMakeOutput.log contains this line:

The system is: Windows - 10.0.16299 - AMD64

From log above, I have C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V140 directory, but there is no Microsoft.cpp.props file inside.

Note - invoking cmake without Clang toolset works fine:

cmake -G "Visual Studio 15 2017 Win64" ..

Here is versions of software I have:

  • Visual Studio Community 2017, Version 15.6.6. cl.exe, Version 19.13.26131.1
  • clang version 7.0.0
  • cmake version 3.11.1

Installing back MSVC 2015 (more precisely, I have Community 2015, Version 14.0.25431.01 Update 3) fixes the problem, but as I said, here is what I have for VC++ Directories (inside MSVC 2017, when Clang toolset was used for genereting the project):

  • $(VC_IncludePath)
C:\Programs\Microsoft Visual Studio 14.0\VC\include
C:\Programs\Microsoft Visual Studio 14.0\VC\atlmfc\include
C:\Program Files (x86)\Windows Kits\10\Include\10.0.16299.0\ucrt
  • $(VC_LibraryPath_x64)
C:\Programs\Microsoft Visual Studio 14.0\VC\lib\amd64
C:\Programs\Microsoft Visual Studio 14.0\VC\atlmfc\lib\amd64
C:\Program Files (x86)\Windows Kits\10\lib\10.0.16299.0\ucrt\x64

Instead of proper ones:

  • $(VC_IncludePath)
C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.13.26128\include
C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.13.26128\atlmfc\include
C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\VS\include
C:\Program Files (x86)\Windows Kits\10\Include\10.0.16299.0\ucrt
C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\VS\UnitTest\include
  • $(VC_LibraryPath_x64)
C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.13.26128\lib\x64
C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.13.26128\atlmfc\lib\x64
C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\VS\lib\x64
C:\Program Files (x86)\Windows Kits\10\lib\10.0.16299.0\ucrt\x64
C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\VS\UnitTest\lib

After latest MSVC 2017 update this leads to linker errors like this one:

error LNK2019: unresolved external symbol __std_reverse_trivially_swappable_4 ...

Also there are few problems with headers when -std=c++17 enabled.

Can you look into the problem please ?

UPD: looking into CMake code in cmGlobalVisualStudio10Generator.cxx, there is FindMSBuildCommand() that looks into hard-coded registry key to find the MSBuild.exe.

But, google says, Microsoft changed the way how things work for MSVC 2017. Now it's a bit more complicated. Here are links [1], [2], [3].

In two words, MSBuild is not in "Program Files (x86)\MSBuild", but in MSVC 2017 installation, like C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0

[1] https://blogs.msdn.microsoft.com/vcblog/2017/03/06/finding-the-visual-c-compiler-tools-in-visual-studio-2017/

[2] https://github.com/Microsoft/vs-setup-samples

[3] https://stackoverflow.com/questions/40694598/how-do-i-call-visual-studio-2017-rcs-version-of-msbuild-from-a-bat-file

UPD: after playing a bit more, there is also problem with proper integration of LLVM-vs2014 toolset by LLVM windows installer itself. Here is what I mean:

  • rename standard folder for MSBuild (C:\Program Files (x86)\MSBuild) to something else, like _MSBuild.

    This will lead to fail in locating MSBuild.exe in FindMSBuildCommand() function, hence msbuild = "MSBuild.exe" will be selected (global one, that should exist in PATH)

  • Invoke MSVC 2017 tools command prompt (Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvars64.bat).

    This will put proper MSBuild.exe into PATH

  • Run CMake command, now it will properly detect Microsoft.cpp.props. But there is another problem: there is no LLVM-vs2014 toolset anymore:

CMake Error at CMakeLists.txt:2 (project):
  Failed to run MSBuild command:

    C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/MSBuild/15.0/Bin/MSBuild.exe

  to get the value of VCTargetsPath:

    Microsoft (R) Build Engine version 15.6.85.37198 for .NET Framework
    Copyright (C) Microsoft Corporation. All rights reserved.

    Build started 22-Apr-18 2:51:42 PM.
    Project "C:\dev\mix\build_clang\CMakeFiles\3.11.1\VCTargetsPath.vcxproj" on node 1 (default targets).
    C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\VC\VCTargets\Microsoft.Cpp.Platform.targets(57,5): error MSB8020: The build tools for LLVM-vs2014 (Platform Toolset = 'LLVM-vs2014') cannot be found. To build using the LLVM-vs2014 build tools, please install LLVM-vs2014 build tools.  Alternatively, you may upgrade to the current Visual Studio tools by selecting the Project menu or right-click the solution, and then selecting "Retarget solution". [C:\dev\mix\build_clang\CMakeFiles\3.11.1\VCTargetsPath.vcxproj]
    Done Building Project "C:\dev\mix\build_clang\CMakeFiles\3.11.1\VCTargetsPath.vcxproj" (default targets) -- FAILED.

    Build FAILED.

    "C:\dev\mix\build_clang\CMakeFiles\3.11.1\VCTargetsPath.vcxproj" (default target) (1) ->
    (PlatformPrepareForBuild target) ->
      C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\VC\VCTargets\Microsoft.Cpp.Platform.targets(57,5): error MSB8020: The build tools for LLVM-vs2014 (Platform Toolset = 'LLVM-vs2014') cannot be found. To build using the LLVM-vs2014 build tools, please install LLVM-vs2014 build tools.  Alternatively, you may upgrade to the current Visual Studio tools by selecting the Project menu or right-click the solution, and then selecting "Retarget solution". [C:\dev\mix\build_clang\CMakeFiles\3.11.1\VCTargetsPath.vcxproj]

        0 Warning(s)
        1 Error(s)

    Time Elapsed 00:00:00.21


  Exit code: 1



-- Configuring incomplete, errors occurred!
See also "C:/dev/mix/build_clang/CMakeFiles/CMakeOutput.log".

This means, that LLVM-vs2014 toolset is installed into "C:\Program Files (x86)\MSBuild" and running LLVM windows installer (when C:\Program Files (x86)\MSBuild folder renamed to _MSBuild) confirms fears - it fails with error:

Installing MSVC integration...
Failed to find MSBuild toolsets directory.
MSVC integration install failed.

Probably LLVM installer should be fixed also to work with proper MSBuild instance :(

UPD: Yeap, need to fix LLVM-vs2014 toolset integration also.

Proper fix require a bit more work (changes to [4]), but hot-fix can be done quickly and applied to LLVM installation. There is %LLVM_installation%\tools\msbuild folder with install.bat. Here is patch for this file:

--- D:\Downloads\install.bat.txt
+++ C:\Programs\LLVM\tools\msbuild\install.bat
@@ -37,6 +37,10 @@
 IF EXIST %D% GOTO FOUND_V140
 SET D="%ProgramFiles(x86)%\MSBuild\Microsoft.Cpp\v4.0\V140\Platforms\%PLATFORM%\PlatformToolsets"
 IF EXIST %D% GOTO FOUND_V140
+
+:TRY_V141
+SET D="%ProgramFiles(x86)%\Microsoft Visual Studio\2017\Community\Common7\IDE\VC\VCTargets\Platforms\%PLATFORM%\PlatformToolsets"
+IF EXIST %D% GOTO FOUND_V141
 
 :TRY_V150
 
@@ -108,8 +112,18 @@
 copy %PLATFORM%\toolset-vs2014_xp.targets %D%\LLVM-vs2014_xp\toolset.targets
 IF NOT %ERRORLEVEL% == 0 GOTO FAILED
 set SUCCESS=1
+GOTO TRY_V141
+
+:FOUND_V141
+REM Routine for installing v141 toolchain.
+IF NOT EXIST %D%\LLVM-vs2014 mkdir %D%\LLVM-vs2014
+IF NOT %ERRORLEVEL% == 0 GOTO FAILED
+copy %PLATFORM%\toolset-vs2014_1.props %D%\LLVM-vs2014\toolset.props
+IF NOT %ERRORLEVEL% == 0 GOTO FAILED
+copy %PLATFORM%\toolset-vs2014_1.targets %D%\LLVM-vs2014\toolset.targets
+IF NOT %ERRORLEVEL% == 0 GOTO FAILED
+set SUCCESS=1
 GOTO TRY_V150
-
 
 :DONE
 echo Done!

It requires changes to toolset-vs2014.props and toolset-vs2014.targets files that are inside x64 and Win32 folders. I did copy and named them as toolset-vs2014_1.props and toolset-vs2014_1.targets. (Whole %LLVM_installation%\tools\msbuild is attached as zip: msbuild.zip)

After changes above %LLVM_installation%\tools\msbuild\install.bat should be invoked with Admin rights. It will install LLVM toolset to C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\VC\VCTargets\Platforms\x64\PlatformToolsets folder.

[4] https://github.com/llvm-mirror/llvm/tree/master/tools/msbuild

So, without too much changes to source code, here is hard-coded workaround that will make Clang & CMake work with latest MSVC 2017:

  1. Have installed MSVC 2017

  2. Have installed LLVM

  3. Rename standard folder for MSBuild (C:\Program Files (x86)\MSBuild) to something else, like _MSBuild.

    This will lead to fail in locating MSBuild.exe in FindMSBuildCommand() function, hence msbuild = "MSBuild.exe" will be selected (global one, that should exist in PATH)

  4. Apply LLVM/Clang installer patch described above.

    This will install LLVM-vs2014 toolset in proper location (C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\VC\VCTargets\Platforms\x64\PlatformToolsets)

  5. Invoke MSVC 2017 tools command prompt (C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvars64.bat).

    This will put proper MSBuild.exe into PATH

  6. Run CMake on your project: cmake -G "Visual Studio 15 2017 Win64" -T LLVM-vs2014 ..

  7. You can check if everything ok looking into $(VC_IncludePath) variable in "VC++ Directories". It should point to MSVC 2017 installation instead of MSVC 2015.

UPD: I found relevant LLVM bug-report [5]. You can also find link to proper fix [6]

[5] https://bugs.llvm.org/show_bug.cgi?id=33672

[6] https://github.com/Farwaykorse/VS_Clang

Edited by grishavanika
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information