CMake on Windows

CMake on Windows

本文关键字:Windows on CMake      更新时间:2023-10-16

我正在尝试在Windows上运行CMake,但出现以下错误:

-- The C compiler identification is unknown
CMake Error at CMakeLists.txt:3 (PROJECT):
  The CMAKE_C_COMPILER:
    cl
  is not a full path and was not found in the PATH.
  To use the NMake generator with Visual C++, cmake must be run from a shell
  that can use the compiler cl from the command line.  This environment is
  unable to invoke the cl compiler.  To fix this problem, run cmake from the
  Visual Studio Command Prompt (vcvarsall.bat).
  Tell CMake where to find the compiler by setting either the environment
  variable "CC" or the CMake cache entry CMAKE_C_COMPILER to the full path to
  the compiler, or to the compiler name if it is in the PATH.

但是我的"CC"环境变量已设置!

>>echo %CC%
C:Anaconda2MinGWx86_64-w64-mingw32bingcc.exe

因为CMake的错误消息在这里具有误导性,我认为它需要更详细的答案。

简而言之,你遇到了一个先有鸡还是先有蛋的问题。

CMake 的编译器检测功能强大,但由于 - 在第一次尝试期间 -

  • 您没有提供任何显式生成器来与-G一起使用
  • 它找不到已安装的Visual Studio。
  • 在您的PATH环境中找不到任何C/C++编译器
  • 它找不到使用编译器的完整路径定义的CC环境变量

它默认为 nmake .

现在问题来了:它确实记住了你在变量缓存中的隐式生成器/编译器选择(参见CMakeCache.txt中的CMAKE_GENERATOR)。如果您安装了多个编译器,这是一个非常有用的功能。

但是,如果您随后声明CC环境变量 - 正如错误消息所暗示的那样 - 为时已晚,因为您的生成器的选择在第一次尝试中就被记住了。

我看到了两种可能的方法:

  1. 通过给出正确的生成器选择来否决生成器选择cmake.exe -G "MinGW Makefiles" ..(正如@Guillaume链接的答案所暗示的那样)
  2. 删除项目的二进制输出目录(包括 CMakeCache.txt ),并在将编译器的 bin 文件夹添加到PATH环境后执行cmake.exe ..

引用

  • 在 Windows 上运行 CMake
  • Windows中CMake的默认生成器是什么?
  • CMakeList 上的 CMake 错误.txt:30(项目):找不到CMAKE_C_COMPILER
  • CMake:如何指定要使用的Visual C++版本?

我使用cmake -G "MinGW Makefiles" .而不是cmake .成功了!

我遇到了同样的问题,对我有用的是:

  1. 我安装了Visual Studio(按照本教程)。
  • 您将需要安装 2019 版本(现在是 2021 年)——还有一些额外的东西,但没关系。整个东西大约是6.7克。
  • 您实际上不需要pip install dlib部分和其余部分。
  1. 在上述教程的基础上,我将此路径添加到环境变量 ( C:Program Files (x86)Microsoft Visual Studio2019BuildToolsVCToolsMSVC14.29.30037binHostx64x64 .向环境变量添加路径的方法也可以在上面的教程中找到。