C++ 使用 CMake 为 VS 2017 生成解决方案的问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42701019/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Problems generating solution for VS 2017 with CMake
提问by onqtam
So I installed Visual Studio 2017 yesterday. I also installed CMake 3.7.2 which supports VS 2017.
所以我昨天安装了 Visual Studio 2017。我还安装了支持 VS 2017 的 CMake 3.7.2。
My VS installation is with the Game development with C++
workflow + a few other components:
我的 VS 安装是使用Game development with C++
工作流 + 一些其他组件:
I've also added the CMake stuff (but I don't think I even needed it - since I'm using CMake as a standalone tool to just generate the VS solutions) and MSBuild (I had msbuild.exe
even before adding that component - so not sure what exactly does that additional component do).
我还添加了 CMake 的东西(但我认为我什至不需要它 - 因为我使用 CMake 作为一个独立的工具来生成 VS 解决方案)和 MSBuild(我msbuild.exe
什至在添加该组件之前就有- 所以不需要确定该附加组件究竟做了什么)。
With VS 2015 I was able to just run cmake .
from a normal command prompt for a solution.
使用 VS 2015,我能够cmake .
从正常的命令提示符运行以获取解决方案。
With VS 2017 the workflow changes - I've read this post from Microsoft.
在 VS 2017 中,工作流程发生了变化——我已经阅读了 Microsoft 的这篇文章。
So I tried the following:
所以我尝试了以下方法:
- I opened the
Developer Command Prompt for VS 2017
and from it I rancmake . -G "NMake Makefiles"
. Then runningcmake --build .
compiled everything properly. When I tried the following in the prompt:
cmake . -G "Visual Studio 15 2017 Win64"
to force the creation of a solution I got the following errors:-- 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!
- 我打开了
Developer Command Prompt for 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!
I also tried setting up the environment using vswhere.exeand running vcvarsall.bat
like this:
我还尝试使用vswhere.exe设置环境并vcvarsall.bat
像这样运行:
"C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvarsall.bat" amd64
"C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvarsall.bat" amd64
and again I could only generate NMake files and not a solution.
再次我只能生成 NMake 文件而不是解决方案。
So how can I get a solution?
那么我怎样才能得到解决方案呢?
And why does cl.exe
report Version 19.10.25017
when it's in VC\Tools\MSVC\14.10.25017\bin
?
为什么在它进入时cl.exe
报告?Version 19.10.25017
VC\Tools\MSVC\14.10.25017\bin
采纳答案by Florian
Turning my comments into an answer
把我的评论变成答案
The error -- The CXX compiler identification is unknown - No CMAKE_CXX_COMPILER could be found.
basically means that CMake wasn't able to compile a simple test program (which it always does as part of identifying/validating the compiler).
该错误-- The CXX compiler identification is unknown - No CMAKE_CXX_COMPILER could be found.
基本上意味着 CMake 无法编译一个简单的测试程序(它总是作为识别/验证编译器的一部分来执行)。
You can take a look into CMakeFiles\CMakeError.log
(relative to your binary output directory), the error reason should be in there.
您可以查看CMakeFiles\CMakeError.log
(相对于您的二进制输出目录),错误原因应该在那里。
Two possible reasons I came across so far:
到目前为止我遇到的两个可能的原因:
Missing administrator rights. You can try running this again from a shell that has administrative rights to crosscheck if your Visual Studio was setup with the need for administrator rights.
Missing Windows SDK. Verify your SDK installation e.g. check that you have any Resource Compiler installed. It should be in a path similar to:
C:\Program Files (x86)\Microsoft SDKs\Windows\v[some version]\bin\RC.Exe
缺少管理员权限。您可以尝试从具有管理权限的 shell 再次运行此程序,以交叉检查您的 Visual Studio 是否设置为需要管理员权限。
缺少 Windows SDK。验证您的 SDK 安装,例如检查您是否安装了任何资源编译器。它应该位于类似于以下内容的路径中:
C:\Program Files (x86)\Microsoft SDKs\Windows\v[some version]\bin\RC.Exe
Visual Studio 2017 Installation
Visual Studio 2017 安装
Please note the Visual Studio may notinstall all necessary C++ packages even when you select one of the C++ pre-defined packages (as I have e.g. used Desktop development with C++
and then added more packages under the Individual Components
tab).
请注意,即使您选择了 C++ 预定义包之一,Visual Studio 也可能不会安装所有必需的 C++ 包(因为我已经使用过Desktop development with C++
,然后在Individual Components
选项卡下添加了更多包)。
Here is which selection worked for me (VS2017 Community Edition, Windows 10):
以下是对我有用的选择(VS2017 社区版,Windows 10):
If you have projects using MFC/ATL libraries you need to add it under SDKs, libraries, and frameworks
subcategory:
如果您有使用 MFC/ATL 库的项目,则需要将其添加到SDKs, libraries, and frameworks
子类别下:
References
参考
回答by onqtam
I'm using Windows 7.... And after @Florian told me in the comments to look into CMakeFiles/CMakeError.log
I managed to fix the problem!
我正在使用 Windows 7 .... 在@Florian 在评论中告诉我要调查之后,CMakeFiles/CMakeError.log
我设法解决了这个问题!
Here is the first 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.8.0-rc2\CompilerIdC\CompilerIdC.vcxproj" on node 1 (default targets).
C:\Program Files (x86)\Microsoft Visual Studio17\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.8.0-rc2\CompilerIdC\CompilerIdC.vcxproj]
Done Building Project "D:\doctest\build\CMakeFiles.8.0-rc2\CompilerIdC\CompilerIdC.vcxproj" (default targets) -- FAILED.
Build FAILED.
"D:\doctest\build\CMakeFiles.8.0-rc2\CompilerIdC\CompilerIdC.vcxproj" (default target) (1) ->
(Desktop_PlatformPrepareForBuild target) ->
C:\Program Files (x86)\Microsoft Visual Studio17\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.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.8.0-rc2\CompilerIdCXX\CompilerIdCXX.vcxproj" on node 1 (default targets).
C:\Program Files (x86)\Microsoft Visual Studio17\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.8.0-rc2\CompilerIdCXX\CompilerIdCXX.vcxproj]
Done Building Project "D:\doctest\build\CMakeFiles.8.0-rc2\CompilerIdCXX\CompilerIdCXX.vcxproj" (default targets) -- FAILED.
Build FAILED.
"D:\doctest\build\CMakeFiles.8.0-rc2\CompilerIdCXX\CompilerIdCXX.vcxproj" (default target) (1) ->
(Desktop_PlatformPrepareForBuild target) ->
C:\Program Files (x86)\Microsoft Visual Studio17\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.8.0-rc2\CompilerIdCXX\CompilerIdCXX.vcxproj]
0 Warning(s)
1 Error(s)
Time Elapsed 00:00:00.13
It seemed I needed Windows SDK version 8.1
so I installed it as a component (had only version 10 installed). But then there was another error:
似乎我需要,Windows SDK version 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.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 Studio17\Community\VC\Tools\MSVC.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 Studio17\Community\VC\Tools\MSVC.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.8.0-rc2\CompilerIdC\CompilerIdC.vcxproj]
Done Building Project "D:\doctest\build\CMakeFiles.8.0-rc2\CompilerIdC\CompilerIdC.vcxproj" (default targets) -- FAILED.
Build FAILED.
"D:\doctest\build\CMakeFiles.8.0-rc2\CompilerIdC\CompilerIdC.vcxproj" (default target) (1) ->
(Link target) ->
LINK : fatal error LNK1104: cannot open file 'ucrtd.lib' [D:\doctest\build\CMakeFiles.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.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 Studio17\Community\VC\Tools\MSVC.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 Studio17\Community\VC\Tools\MSVC.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.8.0-rc2\CompilerIdCXX\CompilerIdCXX.vcxproj]
Done Building Project "D:\doctest\build\CMakeFiles.8.0-rc2\CompilerIdCXX\CompilerIdCXX.vcxproj" (default targets) -- FAILED.
Build FAILED.
"D:\doctest\build\CMakeFiles.8.0-rc2\CompilerIdCXX\CompilerIdCXX.vcxproj" (default target) (1) ->
(Link target) ->
LINK : fatal error LNK1104: cannot open file 'ucrtd.lib' [D:\doctest\build\CMakeFiles.8.0-rc2\CompilerIdCXX\CompilerIdCXX.vcxproj]
0 Warning(s)
1 Error(s)
Time Elapsed 00:00:00.60
So LINK : fatal error LNK1104: cannot open file 'ucrtd.lib'
sounds a lot like I needed to install more components - and so I did:
所以LINK : fatal error LNK1104: cannot open file 'ucrtd.lib'
听起来很像我需要安装更多的组件-我也这样做:
Visual C++ runtime for UWP
Windows Universal CRT SDK
Visual C++ runtime for UWP
Windows Universal CRT SDK
And after that the problem is gone!
之后问题就解决了!
回答by Mark Ingram
If you have the Windows 10 Creator's Update SDK installed, it doesn't install the desktop binaries by default, in order to keep the installation size down. CMake will always try to use the latest SDK when it compiles, which will fail as it will be missing binaries such as "gdi32.lib" (that's the first error that occurs for me).
如果您安装了 Windows 10 Creator's Update SDK,默认情况下它不会安装桌面二进制文件,以减少安装大小。CMake 在编译时将始终尝试使用最新的 SDK,这将失败,因为它将丢失二进制文件,例如“gdi32.lib”(这是我遇到的第一个错误)。
Microsoft list this as a "Known Issue", see Visual C++ Desktop Known Issues.
Microsoft 将此列为“已知问题”,请参阅Visual C++ 桌面已知问题。
The Windows 10 Creators Update SDK has been refactored to reduce installation footprint by default. When you Install this SDK via the UWP workload, it will not install the headers/libs required for Win32 C++ Desktop Projects.
默认情况下,Windows 10 创意者更新 SDK 已被重构以减少安装占用空间。当您通过 UWP 工作负载安装此 SDK 时,它不会安装 Win32 C++ 桌面项目所需的头文件/库。
In order to fix the problem, you need to modify the Visual Studio installation to include the Windows 10 SDK (10.0.15063.0)
for Desktop component.
为了解决该问题,您需要修改 Visual Studio 安装以包含Windows 10 SDK (10.0.15063.0)
for Desktop 组件。
回答by DennisWu
I encountered a similar problem.
我遇到了类似的问题。
It was solved by first un-checking "Desktop Development with C++", and then re-checking "Desktop Development with C++".
解决方法是首先取消选中“Desktop Development with C++”,然后重新选中“Desktop Development with C++”。
回答by slinkin
Windows 10.0.17134 Build 17134; Visual Studio 2017 Community (15.9.7)
Windows 10.0.17134 内部版本 17134;Visual Studio 2017 社区 (15.9.7)
All required components are installed
安装了所有必需的组件
In my case (Wireshark building), an error was in the wrong toolset. CMake determines v141while the actual version is v140.
就我而言(Wireshark 构建),错误出现在错误的工具集中。CMake 确定v141而实际版本是v140。
cmake -G "Visual Studio 15 2017" -T v140,host=x64 ..
回答by Alfishe
Can tell my experience. After reading CMakeFiles/CMakeError.log found an error
可以说说我的经历。阅读 CMakeFiles/CMakeError.log 后发现错误
LINK : fatal error LNK1104: cannot open file 'MSVCRTD.lib'
and what's more important - warning
更重要的是 - 警告
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.
Option 1: I have no intention to use "Spectre-mitigated" libraries and want just to restore previous build behavior.
选项 1:我无意使用“Spectre-mitigated”库,只想恢复以前的构建行为。
That means that we need to turn off generating Visual Studio project with correspondent compilation flags on by default since CMake does that to check whether compiler exists and operational.
这意味着我们需要在默认情况下关闭生成带有相应编译标志的 Visual Studio 项目,因为 CMake 这样做是为了检查编译器是否存在和运行。
Luckily found a thread on support forum: https://developercommunity.visualstudio.com/content/problem/348985/installing-wdk-1809-enabled-spectre-mitigation-fla.html
幸运的是在支持论坛上找到了一个帖子:https: //developercommunity.visualstudio.com/content/problem/348985/installing-wdk-1809-enabled-spectre-mitigation-fla.html
Solution is not very elegant, but works. Put/copy file with name 'Directory.Build.props' to /buildfolder with content:
解决方案不是很优雅,但有效。将名称为“ Directory.Build.props”的文件放入/复制到/build文件夹,其中包含以下内容:
<Project>
<PropertyGroup Label="Configuration">
<SpectreMitigation>false</SpectreMitigation>
</PropertyGroup>
</Project>
That overrides default VS behavior and disables /QSpectre compiler switch.
这会覆盖默认的 VS 行为并禁用 /QSpectre 编译器开关。
Now CMake's compiler test run passes and no other issues found.
现在 CMake 的编译器测试运行通过,没有发现其他问题。
Option 2:ensure that Spectre mitigation libraries installed
选项 2:确保安装了 Spectre 缓解库
See:
看:
回答by aboutqx
For me ,I installed vs2015 before,and the c compiler 2015 work before,when i install vs2017, get the same error can't find compiler
.
And when i run cmake ..
in the vs2017 developer command,it works.
And i can specify generetor to vs2015 or vs2017,it all works.
对我来说,我之前安装了vs2015,并且c编译器2015之前可以工作,当我安装vs2017时,得到同样的错误can't find compiler
。当我cmake ..
在 vs2017 developer 命令中运行时,它可以工作。我可以将生成器指定为 vs2015 或 vs2017,一切正常。