我尝试在 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
环境变量 - 正如错误消息所暗示的那样 - 为时已晚,因为您的生成器的选择在第一次尝试时就被记住了。
我看到了两种可能的方法:
- 通过
cmake.exe -G "MinGW Makefiles"..
给出正确的生成器选择(正如@Guillaume 链接的答案所暗示的那样) - 删除项目的二进制输出目录(包括
CMakeCache.txt
)并在添加编译器的bin
文件夹后执行cmake.exe ..
到您的PATH
环境。
引用文献
关于c++ - Windows 上的 CMake,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35869564/