使用 CMake 为 VS 2017 生成解决方案的问题

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

我的 VS 安装是使用 Game development with C++ 工作流程 + 一些其他组件:

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

在 VS 2015 中,我能够从正常的命令提示符处运行 cmake . 以获得解决方案.

在 VS 2017 中,工作流程发生了变化 - 我已阅读

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

参考文献

  • ??CMake 错误CMakeLists.txt:30(项目):找不到 CMAKE_C_COMPILER
  • CXX 编译器标识未知
  • VS2010 和 CMake:'rc' 未被识别为内部或外部命令

So I installed Visual Studio 2017 yesterday. I also installed CMake 3.7.2 which supports VS 2017.

My VS installation is with the Game development with C++ workflow + a few other components:

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).

With VS 2015 I was able to just run cmake . from a normal command prompt for a solution.

With VS 2017 the workflow changes - I've read this post from Microsoft.

So I tried the following:

  • I opened the Developer Command Prompt for VS 2017 and from it I ran cmake . -G "NMake Makefiles". Then running cmake --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!
    

I also tried setting up the environment using vswhere.exe and running vcvarsall.bat like this:

"C:Program Files (x86)Microsoft Visual Studio2017CommunityVCAuxiliaryBuildvcvarsall.bat" amd64

and again I could only generate NMake files and not a solution.

So how can I get a solution?

And why does cl.exe report Version 19.10.25017 when it's in VCToolsMSVC14.10.25017in?

解决方案

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).

You can take a look into CMakeFilesCMakeError.log (relative to your binary output directory), the error reason should be in there.

Two possible reasons I came across so far:

  1. 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.

  2. 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 SDKsWindowsv[some version]inRC.Exe
    

Visual Studio 2017 Installation

Please note the Visual Studio may not install 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).

Here is which selection worked for me (VS2017 Community Edition, Windows 10):

If you have projects using MFC/ATL libraries you need to add it under SDKs, libraries, and frameworks subcategory:

References

  • CMake Error at CMakeLists.txt:30 (project): No CMAKE_C_COMPILER could be found
  • The CXX compiler identification is unknown
  • VS 2010 and CMake: 'rc' is not recognized as an internal or external command

相关文章