使用CMake生成VS 2017的解决方案时出现问题

28

昨天我安装了Visual Studio 2017。我还安装了支持VS 2017的CMake 3.7.2。

我的VS安装是使用“使用C++进行游戏开发”工作流+其他一些组件:

individual_components

我还添加了CMake(但我甚至不认为我需要它 - 因为我正在使用CMake作为独立工具来生成VS解决方案)和MSBuild(在添加该组件之前我已经有msbuild.exe了,所以不确定该额外组件究竟是做什么的)。

使用VS 2015,我只需从普通命令提示符中运行cmake .就可以为解决方案生成内容。

在VS 2017中,工作流程会发生变化 - 我阅读了Microsoft的这篇文章

因此,我尝试了以下操作:

  • 我打开了“用于VS 2017的开发人员命令提示符”,并从中运行了cmake . -G“NMake Makefiles”。然后运行cmake --build .可以正确地编译所有内容。
  • 当我在提示符中尝试运行以下命令时:cmake . -G“Visual Studio 15 2017 Win64”,以强制创建解决方案时,我收到了以下错误:

    -- The C compiler identification is unknown
    -- The CXX compiler identification is unknown
    CMake Error at CMakeLists.txt:3 (project):
      No CMAKE_C_COMPILER could be found.
    CMake Error at CMakeLists.txt:3 (project):
      No CMAKE_CXX_COMPILER could be found.
    -- Configuring incomplete, errors occurred!
    

我还尝试使用vswhere.exe设置环境并像这样运行vcvarsall.bat

"C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvarsall.bat" amd64

但是我只能生成NMake文件,而不是解决方案。

那么怎么才能得到一个解决方案呢?

为什么cl.exeVC\Tools\MSVC\14.10.25017\bin中时报告的版本是Version 19.10.25017


cl.exe 报告编译器版本。它所在的目录是整个编译器版本。请注意,与 VS15 捆绑的 CMake 被 VS 团队修改,并基于 CMake 3.6,据我所知,它不被支持为官方 CMake 版本。我建议您暂时坚持使用正常的 CMake 3.8,它可以正常地与 VS15 协作,并生成适当的解决方案文件。此外,请注意 VS15 中的此错误 - tambre
@tambre,好的,cmake --version显示3.7.2,这是最新的官方版本(3.8是RC),这是我从VS中单独安装的版本。但现在我会尝试使用3.8。 - onqtam
看起来你正在进行源代码内构建。因此,可能的一种情况是,你正在尝试在同一个目录中从NMake切换到Visual Studio生成器(这是行不通的)。你可以尝试在其中创建一个新的“build”子目录,并从那里运行“cmake ..”(从头开始启动生成过程)吗?或者你可以使用脚本自动化这个过程。 - Florian
1
我认为您不需要重新安装Windows。这可能是由于许多其他原因引起的。其中一个原因可能是管理员权限。您可以尝试从具有管理员权限的shell中再次运行此操作,以交叉检查您的Visual Studio是否设置了需要管理员权限。并且您能否将“CMakeFiles \ CMakeError.log”内容添加到您的问题中?里面必须有一些错误原因。 - Florian
1
@onqtam,你应该将答案发布为回答并接受自己的答案,而不是将其编辑到问题中。回答自己的问题没有任何问题。 - ComicSansMS
显示剩余8条评论
8个回答

25

将我的评论转化为答案

错误 -- The CXX compiler identification is unknown - No CMAKE_CXX_COMPILER could be found. 基本上意味着CMake无法编译一个简单的测试程序(这是它作为识别/验证编译器的一部分必做的)。

你可以查看 CMakeFiles\CMakeError.log(相对于你的二进制输出目录),错误原因应该在里面。

我遇到过两个可能的原因:

  1. 缺少管理员权限。你可以尝试从具有管理员权限的 shell 中再次运行此命令,以核实是否需要使用管理员权限设置 Visual Studio。

  2. 缺少Windows SDK。验证你的SDK安装情况,例如检查你是否安装了任何资源编译器。它应该位于类似以下路径的位置:

    C:\Program Files (x86)\Microsoft SDKs\Windows\v[some version]\bin\RC.Exe
    

Visual Studio 2017安装

请注意,即使您选择了C++预定义软件包之一(例如我使用了桌面开发与C++然后在单个组件选项卡下添加更多软件包),Visual Studio可能仍然不会安装所有必要的C ++程序包。

以下是适用于我的选择(VS2017社区版,Windows 10):

enter image description here

如果您有使用MFC / ATL库的项目,则需要将其添加到SDK、库和框架子类别下:

enter image description here

参考文献


我还是坚持我的答案 :D - onqtam
在最新的安装程序(1.5.30308.1)中,“Windows Universal CRT SDK”和“Windows 8.1 SDK”的选项被捆绑到“Windows 8.1 SDK和UCRT SDK”中。此外,CMake检测VS C和C++编译器不需要“C++/CLI支持”。 - joell
给其他人的一个小提示:如果你已经安装了VS Studio社区版,你仍然可以添加这些软件包:打开VS Studio安装程序,选择VS Community Edition-> 点击修改。然后选择所需的软件包(参见评论),并点击“修改”。 - Maxim
1
谢谢!这是给我未来自己的一条提示:当在VS 2017命令提示符下使用“Visual Studio 14 2015 Win64”代码生成器调用时,CMake找不到C/C++编译器,因为在编译其编译器检测代码时出现了“LNK1104: Cannot open file 'ucrtd.lib'”错误。解决方法是安装“Windows通用CRT SDK”。 - François Beaune

7
我正在使用Windows 7.... 在@Florian在评论中提醒我查看CMakeFiles/CMakeError.log后,我成功解决了问题!
以下是第一个日志:
Compiling the C compiler identification source file "CMakeCCompilerId.c" failed.
Compiler:  
Build flags: 
Id flags:  

The output was:
1
Microsoft (R) Build Engine version 15.1.548.43366
Copyright (C) Microsoft Corporation. All rights reserved.

Build started 3/10/2017 11:05:24 AM.
Project "D:\doctest\build\CMakeFiles\3.8.0-rc2\CompilerIdC\CompilerIdC.vcxproj" on node 1 (default targets).
C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\VC\VCTargets\Platforms\x64\PlatformToolsets\v141\Toolset.targets(36,5): error MSB8036: The Windows SDK version 8.1 was not found. Install the required version of Windows SDK or change the SDK version in the project property pages or by right-clicking the solution and selecting "Retarget solution". [D:\doctest\build\CMakeFiles\3.8.0-rc2\CompilerIdC\CompilerIdC.vcxproj]
Done Building Project "D:\doctest\build\CMakeFiles\3.8.0-rc2\CompilerIdC\CompilerIdC.vcxproj" (default targets) -- FAILED.

Build FAILED.

"D:\doctest\build\CMakeFiles\3.8.0-rc2\CompilerIdC\CompilerIdC.vcxproj" (default target) (1) ->
(Desktop_PlatformPrepareForBuild target) -> 
  C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\VC\VCTargets\Platforms\x64\PlatformToolsets\v141\Toolset.targets(36,5): error MSB8036: The Windows SDK version 8.1 was not found. Install the required version of Windows SDK or change the SDK version in the project property pages or by right-clicking the solution and selecting "Retarget solution". [D:\doctest\build\CMakeFiles\3.8.0-rc2\CompilerIdC\CompilerIdC.vcxproj]

    0 Warning(s)
    1 Error(s)

Time Elapsed 00:00:00.28


Compiling the CXX compiler identification source file "CMakeCXXCompilerId.cpp" failed.
Compiler:  
Build flags: 
Id flags:  

The output was:
1
Microsoft (R) Build Engine version 15.1.548.43366
Copyright (C) Microsoft Corporation. All rights reserved.

Build started 3/10/2017 11:05:24 AM.
Project "D:\doctest\build\CMakeFiles\3.8.0-rc2\CompilerIdCXX\CompilerIdCXX.vcxproj" on node 1 (default targets).
C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\VC\VCTargets\Platforms\x64\PlatformToolsets\v141\Toolset.targets(36,5): error MSB8036: The Windows SDK version 8.1 was not found. Install the required version of Windows SDK or change the SDK version in the project property pages or by right-clicking the solution and selecting "Retarget solution". [D:\doctest\build\CMakeFiles\3.8.0-rc2\CompilerIdCXX\CompilerIdCXX.vcxproj]
Done Building Project "D:\doctest\build\CMakeFiles\3.8.0-rc2\CompilerIdCXX\CompilerIdCXX.vcxproj" (default targets) -- FAILED.

Build FAILED.

"D:\doctest\build\CMakeFiles\3.8.0-rc2\CompilerIdCXX\CompilerIdCXX.vcxproj" (default target) (1) ->
(Desktop_PlatformPrepareForBuild target) -> 
  C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\VC\VCTargets\Platforms\x64\PlatformToolsets\v141\Toolset.targets(36,5): error MSB8036: The Windows SDK version 8.1 was not found. Install the required version of Windows SDK or change the SDK version in the project property pages or by right-clicking the solution and selecting "Retarget solution". [D:\doctest\build\CMakeFiles\3.8.0-rc2\CompilerIdCXX\CompilerIdCXX.vcxproj]

    0 Warning(s)
    1 Error(s)

Time Elapsed 00:00:00.13

看起来我需要Windows SDK 8.1版本,所以我把它作为一个组件安装了进来(只有10版被安装)。但是,接着又出现了另一个错误:

Compiling the C compiler identification source file "CMakeCCompilerId.c" failed.
Compiler:  
Build flags: 
Id flags:  

The output was:
1
Microsoft (R) Build Engine version 15.1.548.43366
Copyright (C) Microsoft Corporation. All rights reserved.

Build started 3/10/2017 11:17:21 AM.
Project "D:\doctest\build\CMakeFiles\3.8.0-rc2\CompilerIdC\CompilerIdC.vcxproj" on node 1 (default targets).
PrepareForBuild:
  Creating directory "Debug\".
  Creating directory "Debug\CompilerIdC.tlog\".
InitializeBuildStatus:
  Creating "Debug\CompilerIdC.tlog\unsuccessfulbuild" because "AlwaysCreate" was specified.
ClCompile:
  C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.10.25017\bin\HostX86\x64\CL.exe /c /nologo /W0 /WX- /diagnostics:classic /Od /D _MBCS /Gm- /EHsc /RTC1 /MDd /GS /fp:precise /Zc:wchar_t /Zc:forScope /Zc:inline /Fo"Debug\\" /Fd"Debug\vc141.pdb" /Gd /TC /errorReport:queue CMakeCCompilerId.c
  CMakeCCompilerId.c
Link:
  C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.10.25017\bin\HostX86\x64\link.exe /ERRORREPORT:QUEUE /OUT:".\CompilerIdC.exe" /INCREMENTAL:NO /NOLOGO kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /MANIFEST /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /manifest:embed /PDB:".\CompilerIdC.pdb" /SUBSYSTEM:CONSOLE /TLBID:1 /DYNAMICBASE /NXCOMPAT /IMPLIB:".\CompilerIdC.lib" /MACHINE:X64 Debug\CMakeCCompilerId.obj
LINK : fatal error LNK1104: cannot open file 'ucrtd.lib' [D:\doctest\build\CMakeFiles\3.8.0-rc2\CompilerIdC\CompilerIdC.vcxproj]
Done Building Project "D:\doctest\build\CMakeFiles\3.8.0-rc2\CompilerIdC\CompilerIdC.vcxproj" (default targets) -- FAILED.

Build FAILED.

"D:\doctest\build\CMakeFiles\3.8.0-rc2\CompilerIdC\CompilerIdC.vcxproj" (default target) (1) ->
(Link target) -> 
  LINK : fatal error LNK1104: cannot open file 'ucrtd.lib' [D:\doctest\build\CMakeFiles\3.8.0-rc2\CompilerIdC\CompilerIdC.vcxproj]

    0 Warning(s)
    1 Error(s)

Time Elapsed 00:00:01.04


Compiling the CXX compiler identification source file "CMakeCXXCompilerId.cpp" failed.
Compiler:  
Build flags: 
Id flags:  

The output was:
1
Microsoft (R) Build Engine version 15.1.548.43366
Copyright (C) Microsoft Corporation. All rights reserved.

Build started 3/10/2017 11:17:22 AM.
Project "D:\doctest\build\CMakeFiles\3.8.0-rc2\CompilerIdCXX\CompilerIdCXX.vcxproj" on node 1 (default targets).
PrepareForBuild:
  Creating directory "Debug\".
  Creating directory "Debug\CompilerIdCXX.tlog\".
InitializeBuildStatus:
  Creating "Debug\CompilerIdCXX.tlog\unsuccessfulbuild" because "AlwaysCreate" was specified.
ClCompile:
  C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.10.25017\bin\HostX86\x64\CL.exe /c /nologo /W0 /WX- /diagnostics:classic /Od /D _MBCS /Gm- /EHsc /RTC1 /MDd /GS /fp:precise /Zc:wchar_t /Zc:forScope /Zc:inline /Fo"Debug\\" /Fd"Debug\vc141.pdb" /Gd /TP /errorReport:queue CMakeCXXCompilerId.cpp
  CMakeCXXCompilerId.cpp
Link:
  C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.10.25017\bin\HostX86\x64\link.exe /ERRORREPORT:QUEUE /OUT:".\CompilerIdCXX.exe" /INCREMENTAL:NO /NOLOGO kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /MANIFEST /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /manifest:embed /PDB:".\CompilerIdCXX.pdb" /SUBSYSTEM:CONSOLE /TLBID:1 /DYNAMICBASE /NXCOMPAT /IMPLIB:".\CompilerIdCXX.lib" /MACHINE:X64 Debug\CMakeCXXCompilerId.obj
LINK : fatal error LNK1104: cannot open file 'ucrtd.lib' [D:\doctest\build\CMakeFiles\3.8.0-rc2\CompilerIdCXX\CompilerIdCXX.vcxproj]
Done Building Project "D:\doctest\build\CMakeFiles\3.8.0-rc2\CompilerIdCXX\CompilerIdCXX.vcxproj" (default targets) -- FAILED.

Build FAILED.

"D:\doctest\build\CMakeFiles\3.8.0-rc2\CompilerIdCXX\CompilerIdCXX.vcxproj" (default target) (1) ->
(Link target) -> 
  LINK : fatal error LNK1104: cannot open file 'ucrtd.lib' [D:\doctest\build\CMakeFiles\3.8.0-rc2\CompilerIdCXX\CompilerIdCXX.vcxproj]

    0 Warning(s)
    1 Error(s)

Time Elapsed 00:00:00.60

所以,LINK : fatal error LNK1104: cannot open file 'ucrtd.lib'听起来很像我需要安装更多组件——于是我就安装了以下组件:

  • 适用于UWP的Visual C++运行时
  • Windows通用CRT SDK

之后问题就解决了!


使用8.1 SDK和常规Win32应用程序,只需添加“Windows通用CRT SDK”即可。另一个组件是为UWP应用程序准备的。 - ceztko
1
仅选择“Windows通用CRT SDK”对我有效。 - Yongwei Wu

2
如果您已安装了Windows 10 Creator's Update SDK,则默认情况下不会安装桌面二进制文件,以减小安装大小。CMake在编译时总是尝试使用最新的SDK,这将失败,因为它将缺少二进制文件,例如"gdi32.lib"(这是我遇到的第一个错误)。
微软将此列为“已知问题”,请参见Visual C++ Desktop Known Issues
引用:

通过UWP工作负载安装此SDK时,默认情况下已对Windows 10 Creators Update SDK进行了重构,以减小安装占用空间。这样做将不会安装Win32 C ++桌面项目所需的头文件/库。

为了解决问题,您需要修改Visual Studio安装,以包括Windows 10 SDK (10.0.15063.0)桌面组件。

Install Creator's Update SDK for Desktop


太棒了,这对我解决问题非常有用。 - nagates

2

我可以告诉你我的经验。在阅读CMakeFiles/CMakeError.log之后,发现了一个错误。

LINK : fatal error LNK1104: cannot open file 'MSVCRTD.lib'

而更重要的是-警告

Common7\IDE\VC\VCTargets\Microsoft.CppBuild.targets(402,5):
warning MSB8038: Spectre mitigation is enabled but Spectre mitigated libraries
are not found.
Verify that the Visual Studio Workload includes the Spectre mitigated libraries.  

选项1:我不打算使用“Spectre-mitigated”库,只想恢复以前的构建行为。

这意味着我们需要关闭默认情况下使用相应编译标志生成Visual Studio项目的功能,因为CMake会检查编译器是否存在并可用。

幸运的是,在支持论坛上找到了一个主题:https://developercommunity.visualstudio.com/content/problem/348985/installing-wdk-1809-enabled-spectre-mitigation-fla.html

解决方法并不是非常优雅,但是有效。将名称为“Directory.Build.props”的文件放入/ build文件夹中,其内容如下:

<Project>
  <PropertyGroup Label="Configuration">
    <SpectreMitigation>false</SpectreMitigation>
  </PropertyGroup>
</Project>

这将覆盖默认的VS行为并禁用/QSpectre编译器开关。

现在,CMake的编译器测试通过,没有发现其他问题。

选项2:确保已安装Spectre缓解库

请参见:


提到 Directory.Build.props 的线程指出该文件应该在"您的项目根目录"中创建。你在哪里找到了"/build文件夹"的解决方案? - nevilad

1

Windows 10.0.17134 Build 17134; Visual Studio 2017 Community (15.9.7)

所有必需组件已安装

在我的情况下(Wireshark building),错误出现在错误的工具集上。CMake确定v141,而实际版本是v140

cmake -G "Visual Studio 15 2017" -T v140,host=x64 ..

1
我遇到了类似的问题。
首先取消勾选 "使用 C++ 进行桌面开发",然后重新勾选 "使用 C++ 进行桌面开发",问题得以解决。

0
对于我来说,我之前安装了vs2015,并且c编译器在2015年之前就可以工作了,当我安装vs2017时,出现了相同的错误" can't find compiler "。 当我在vs2017开发者命令中运行" cmake .. "时,它可以正常工作。 并且我可以指定生成器为vs2015或vs2017,它们都可以正常工作。

0
从我的经验来看,这个CMake工作流问题的“基础”是(在这种情况下)非常“误导性”的错误消息“找不到任何CMAKE_C [XX] _COMPILER”。
CMakeError.log是关键:我在那里看到了“...Cl.exe / c ...”,所以编译器被找到并执行了。
我的问题是缺少ucrt.lib(尽管Win10 SDK已经存在;复制而不是“安装”,是我的错)。

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