Windows 上的 CMake

2021-12-26 00:00:00 c cmake c++

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

I am trying to run CMake on Windows, and I get the following error:

-- 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"环境变量!

However my "CC" environment variable is set!

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

推荐答案

因为 CMake 的错误信息在这里具有误导性,我认为它值得更详细的答案.

Because CMake's error message is misleading here, I think it warrants a little more detailed answer.

简而言之,您遇到了鸡与蛋类型的问题.

In short, you ran into a chicken-and-egg kind of a problem.

CMake 的编译器检测是强大的,但是因为 - 在第一次尝试期间 -

CMake's compiler detection is mighty, but since - during the first try -

  • 您没有提供任何显式生成器 与 -G
  • 一起使用
  • 找不到安装的 Visual Studio
  • 在您的 PATH 环境中找不到任何 C/C++ 编译器
  • 找不到用编译器的完整路径定义的CC环境变量
  • you didn't give any explicit generator to use with -G
  • it couldn't find a Visual Studio installed
  • it couldn't find any C/C++ compiler in your PATH environment
  • it couldn't find a CC environment variable defined with the full path to a compiler

默认为nmake.

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

Now here comes the problem: it does remember your implicit generator/compiler choice in it's variable cache (see CMAKE_GENERATOR in CMakeCache.txt). What is a very useful feature, if you have multiple compilers installed.

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

But if you then declare the CC environment variable - as the error message suggests - it's too late since your generator's choice was remembered in the first try.

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

I see two possible ways out of this:

  1. 通过使用 cmake.exe -G "MinGW Makefiles" .. 给出正确的选项来否决生成器的选择(正如@Guillaume 所建议的答案所暗示的那样)
  2. 删除您项目的二进制输出目录(包括CMakeCache.txt)并在添加编译器的bin文件夹后执行cmake.exe ..到您的 PATH 环境.
  1. Overrule the generator choice by given the right one with cmake.exe -G "MinGW Makefiles" .. (as the answer linked by @Guillaume suggests)
  2. Delete your project's binary output directory (including CMakeCache.txt) and do cmake.exe .. after you added your compiler's bin folder to your PATH environment.

参考资料

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

相关文章