使用MSBuild构建Visual Studio 2015 C++项目(v140)失败 - 找不到v140。

11

我已经同时安装了Visual Studio 2013和2015。我刚刚将合并的解决方案(C#,C ++,C ++ / CLI)升级到2015,并将平台工具集设置为v140。

从Visual Studio构建可以正常工作,但是无法从MSBuild编译。我使用的是MSBuild 14,MSBuild抱怨找不到v140,但这是无稽之谈,因为v140已安装在计算机上的“c:\ Program Files(x86)\ MSBuild \ Microsoft.Cpp \ v4.0 \ V140”中。

关于升级解决方案的建议无法执行,因为解决方案已经升级,解决方案上下文菜单中没有这样的项目。

如果将项目的平台工具切换为v120,则可以工作。

详细信息:

c:\git\hewerMaster>"c:\Program Files (x86)\MSBuild\14.0\Bin\MSBuild.exe"  c:\git
\hewerMaster\Hewer.sln /property:Configuration=Debug /property:Platform=x86
Microsoft (R) Build Engine version 14.0.23107.0
Copyright (C) Microsoft Corporation. All rights reserved.

Building the projects in this solution one at a time. To enable parallel build,
please add the "/m" switch.
Build started 9/12/2015 4:00:25 PM.
Project "c:\git\hewerMaster\Hewer.sln" on node 1 (default targets).
ValidateSolutionConfiguration:
  Building solution configuration "Debug|x86".
ValidateProjects:
  The project "HewerDotNetWrapper" is not selected for building in solution con
  figuration "Debug|x86".
  The project "VisualHewer" is not selected for building in solution configurat
  ion "Debug|x86".
  The project "UnitTestWrapper" is not selected for building in solution config
  uration "Debug|x86".
  The project "ParamsFormatConverter" is not selected for building in solution
  configuration "Debug|x86".
  The project "UnitTestGenerator" is not selected for building in solution conf
  iguration "Debug|x86".
  The project "HewerUnitTestDotNet" is not selected for building in solution co
  nfiguration "Debug|x86".
Project "c:\git\hewerMaster\Hewer.sln" (1) is building "c:\git\hewerMaster\Hewe
rRedirect\HewerRedirect.csproj" (2) on node 1 (default targets).
CoreCompile:
Skipping target "CoreCompile" because all output files are up-to-date with resp
ect to the input files.
_CopyAppConfigFile:
Skipping target "_CopyAppConfigFile" because all output files are up-to-date wi
th respect to the input files.
CopyFilesToOutputDirectory:
  HewerRedirect -> c:\git\hewerMaster\anyCPU\Debug\pbrain-hewer.exe
Done Building Project "c:\git\hewerMaster\HewerRedirect\HewerRedirect.csproj" (
default targets).

Project "c:\git\hewerMaster\Hewer.sln" (1) is building "c:\git\hewerMaster\Hewe
r\Hewer.vcxproj" (3) on node 1 (default targets).
C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V120\Microsoft.Cpp.Platform.t
argets(64,5): error MSB8020: The build tools for v140 (Platform Toolset = 'v140
') cannot be found. To build using the v140 build tools, please install v140 bu
ild tools.  Alternatively, you may upgrade to the current Visual Studio tools b
y selecting the Project menu or right-click the solution, and then selecting "U
pgrade Solution...". [c:\git\hewerMaster\Hewer\Hewer.vcxproj]
Done Building Project "c:\git\hewerMaster\Hewer\Hewer.vcxproj" (default targets
) -- FAILED.

Done Building Project "c:\git\hewerMaster\Hewer.sln" (default targets) -- FAILE
D.


Build FAILED.

更新:

  • 有一些项目被跳过了 Debug X86,这是有意的。
  • 这些项目最初在VS2008中创建,然后升级到VS2013,再升级到VS2015。
  • 该计算机安装了两个Visual Studios(2013社区版和2015社区版)。 2013年首先安装,2015年稍后安装。

注册表设置如下: enter image description here

MS Build registry settings

更新2: Minimal, Complete, and Verifiable example

我在Visual Studio 2015中创建了一个空的C ++控制台应用程序:

https://dl.dropboxusercontent.com/u/53387801/ConsoleApplication_v140.zip

#include "stdafx.h"

int main()
{
    return 0;
}

我可以在Visual Studio中构建它,但是我在MSBuild中遇到了相同的问题:

c:\>"c:\Program Files (x86)\MSBuild\14.0\Bin\MSBuild" "C:\temp\vs2015_simplest\C
onsoleApplication1\consoleApplication1.sln"
Microsoft (R) Build Engine version 14.0.23107.0
Copyright (C) Microsoft Corporation. All rights reserved.

Building the projects in this solution one at a time. To enable parallel build,
please add the "/m" switch.
Build started 9/16/2015 10:16:20 AM.
Project "C:\temp\vs2015_simplest\ConsoleApplication1\consoleApplication1.sln" o
n node 1 (default targets).
ValidateSolutionConfiguration:
  Building solution configuration "Debug|x64".
Project "C:\temp\vs2015_simplest\ConsoleApplication1\consoleApplication1.sln" (
1) is building "C:\temp\vs2015_simplest\ConsoleApplication1\ConsoleApplication1
\ConsoleApplication1.vcxproj" (2) on node 1 (default targets).
C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V120\Microsoft.Cpp.Platform.t
argets(64,5): error MSB8020: The build tools for v140 (Platform Toolset = 'v140
') cannot be found. To build using the v140 build tools, please install v140 bu
ild tools.  Alternatively, you may upgrade to the current Visual Studio tools b
y selecting the Project menu or right-click the solution, and then selecting "U
pgrade Solution...". [C:\temp\vs2015_simplest\ConsoleApplication1\ConsoleApplic
ation1\ConsoleApplication1.vcxproj]
Done Building Project "C:\temp\vs2015_simplest\ConsoleApplication1\ConsoleAppli
cation1\ConsoleApplication1.vcxproj" (default targets) -- FAILED.

Done Building Project "C:\temp\vs2015_simplest\ConsoleApplication1\consoleAppli
cation1.sln" (default targets) -- FAILED.


Build FAILED.

"C:\temp\vs2015_simplest\ConsoleApplication1\consoleApplication1.sln" (default
target) (1) ->
"C:\temp\vs2015_simplest\ConsoleApplication1\ConsoleApplication1\ConsoleApplica
tion1.vcxproj" (default target) (2) ->
(PlatformPrepareForBuild target) ->
  C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V120\Microsoft.Cpp.Platform
.targets(64,5): error MSB8020: The build tools for v140 (Platform Toolset = 'v1
40') cannot be found. To build using the v140 build tools, please install v140
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
"Upgrade Solution...". [C:\temp\vs2015_simplest\ConsoleApplication1\ConsoleAppl
ication1\ConsoleApplication1.vcxproj]

    0 Warning(s)
    1 Error(s)

Time Elapsed 00:00:00.14

c:\>

已解决:

环境变量 VisualStudioVersion 被设置为 12.0 而不是 14.0。将该值更改为 14.0 可以解决问题。

简单的控制台应用程序在更改了环境变量 VisualStudioVersion 并注销登录 Windows 后立即开始工作。

更改环境变量 VisualStudioVersion 后,原始项目仍然无法构建。在 Visual Studio 2015 中出现了一个重新定向解决方案上下文菜单。因此,我重新定向了解决方案。重定向后,解决方案仍无法编译(找不到 v140)。最后,我从v140(未安装)更改为Visual Studio 2015(v140)平台工具集。现在可以从 Visual Studio 和 MSBuild 编译该解决方案。


1
很多人已经使用v140工具集构建了C++项目。如果不可能,VS 2015也不会发布。你的情况似乎很特殊,我认为没有足够的信息可以解决它。你可以尝试创建一个MCVE来看看。 - Ross Ridge
我已经测试了MCVE,我遇到了相同的问题。提供zip文件链接。 - Tomas Kubes
1
请检查您是否定义了环境变量“VisualStudioVersion”,或者您的解决方案和项目文件是否将其重新定义为14.x.x.x以外的其他内容。 - Ross Ridge
2个回答

12
您的问题在于将环境变量VisualStudioVersion设置为12.0。这告诉MSBuild使用Visual Studio 2013(V120)工具,不支持Visual Studio 2015(V140)构建工具。如果您取消设置此环境变量,或向MSBuild传递/p:VisualStudioVersion=14.0选项,则应该能够编译您的项目(或至少解决此错误)。
您可以在MSDN博客文章Visual Studio项目兼容性和VisualStudioVersion中找到有关VisualStudioVersion变量的更多信息。

顺便说一下,我没有自己设置变量VisualStudioVersion为12.0。这可能是VS2015安装或其他什么问题。它只是发生了。 :-( - Tomas Kubes
@qub1n,VS 2015的安装不会将VisualStudioVersion环境变量设置为12.0。设置它为12.0的是运行Visual Studio 2013 vcvars.bat脚本之一。 - Ross Ridge

2

一个解决方案是你可以简单地更改系统中可用的平台工具集,然后它就可以成功构建了。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接