c++ - Windows 上的 CMake

标签 c++ c cmake

我尝试在 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:\Anaconda2\MinGW\x86_64-w64-mingw32\bin\gcc.exe

最佳答案

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

简而言之,您遇到了 chicken-and-egg 类的问题。

CMake 的编译器检测功能强大,但因为 - 在第一次尝试时 -

  • 你没有给任何 explicit generator 用于 -G
  • 找不到安装的 Visual Studio
  • 在您的 PATH 环境中找不到任何 C/C++ 编译器
  • 找不到使用编译器完整路径定义的 CC 环境变量

它默认为 nmake

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

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

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

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

引用文献

关于c++ - Windows 上的 CMake,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35869564/

相关文章:

c++ - 具有未使用的引用参数的 constexpr 函数——gcc vs clang

c++ - 如何在编译时找出可以表示数字的最小整数类型

c - c语言的dos.h引用在哪里找?

c - 结构体范围访问

cmake - 如何强制 CMake 的 target_include_directories() 使用绝对路径

C++ 静态分析 : finding all places where some member function is called

c++ - 在专用模板函数中使用通用模板类

android - 在未 root 的 Android 设备上运行 native C 程序

c++ - Python 依赖,windows (CMake)

Freebsd 上构造函数核心转储的 C++ 异常,但在 linux 或 mac os x 上则不然