我一直在寻找,这个问题似乎以各种形式出现了很多.它通常是由缺少编译器引起的,可以说是C和CXX编译器unknown.
然而,在我的情况下,这不是正在发生的事情.我的机器上有C和C++编译器,例如通过Visual Studio,所有编译都很好.cmake然而,通过这种情况:
> cmake .
Run Code Online (Sandbox Code Playgroud)
输出:
-- Building for: Visual Studio 14 2015
-- The C compiler identification is MSVC 19.0.24215.1
-- The CXX compiler identification is MSVC 19.0.24215.1
CMake Error at CMakeLists.txt:12 (project):
No CMAKE_C_COMPILER could be found.
CMake Error at CMakeLists.txt:12 (project):
No CMAKE_CXX_COMPILER could be found.
-- Configuring incomplete, errors occurred!
Run Code Online (Sandbox Code Playgroud)
我有点困惑.它怎么能找到编译器,但后来忘了它?也有在输出日志中没有错误(或者,如果有有,我不认识他们).
这些是最后一行CMakeOutput.log:
Compilation of the CXX compiler identification source "CMakeCXXCompilerId.cpp" produced "CompilerIdCXX.exe"
Compilation of the CXX compiler identification source "CMakeCXXCompilerId.cpp" produced "CompilerIdCXX.vcxproj"
The CXX compiler identification is MSVC, found in "[...]/CMakeFiles/3.9.1/CompilerIdCXX/CompilerIdCXX.exe"
Run Code Online (Sandbox Code Playgroud)
源CMakeLists.txt是现有工作项目的一部分,因此我认为这是本地配置问题.
手动指定这些编译器如下所示将使cmake发现它们并生成项目(这里有意使用真实路径,因为我怀疑路径本身可能是导致此错误的原因,请注意外来字符):
SET(CMAKE_C_COMPILER "D:/Programfájlok (x86)/Microsoft Visual Studio 14.0/VC/bin")
SET(CMAKE_CXX_COMPILER "D:/Programfájlok (x86)/Microsoft Visual Studio 14.0/VC/bin")
Run Code Online (Sandbox Code Playgroud)
注意:这些位于CmakeLists.txt文件的顶部.
现在的问题仍然有效,如为什么不能CMake的自动确定这些路径,当它能够确定正确的发电机.
根据FAQ,不建议在CmakeLists.txt中设置编译器路径,因此另一种方法可以是:
cmake
-D CMAKE_C_COMPILER="path/to/compiler"
-D CMAKE_CXX_COMPILER="path/to/compiler"
..
Run Code Online (Sandbox Code Playgroud)
之前有报道称 CMake 的 Visual Studio 生成器因 unicode 路径而崩溃(此处、此处),虽然此问题似乎已长期修复(CMake 提交),但它可能表明 Unicode 路径在某些情况下可能仍然会造成问题。
根据您的描述,这听起来不像安装问题 - 全新的 Visual Studio 安装,否则可以正常工作。由于CMAKE_CXX_COMPILER手动指定时,构建正在工作,我最好的猜测是可能与涉及 Unicode 路径的 CMake 编译器检测有关。
以上只是有根据的猜测。-DCMAKE_C_COMPILER但是,如果它是您自己的项目,和/或您可以控制 CMake 的调用方式,则使用和指定 CMake 的编译器-DCMAKE_CXX_COMPILER就完全没问题。
它如何找到编译器,然后又忘记它?
调用负责确定编译器版本的宏 CMAKE_DETERMINE_COMPILER_ID(在CMakeDetermineCompilerId.cmake中)。浏览宏代码,有几个定义在正常操作时需要设置,即CMAKE_CXX_COMPILER_ID、CMAKE_CXX_COMPILER_VERSION和CMAKE_CXX_COMPILER_ID_TOOL。在你的情况下,似乎 和CMAKE_CXX_COMPILER_ID已CMAKE_CXX_COMPILER_VERSION设置,但由于某种原因CMAKE_CXX_COMPILER_ID_TOOL没有设置:
# Display the final identification result.
if(CMAKE_${lang}_COMPILER_ID)
if(CMAKE_${lang}_COMPILER_VERSION)
set(_version " ${CMAKE_${lang}_COMPILER_VERSION}")
else()
set(_version "")
endif()
message(STATUS "The ${lang} compiler identification is "
"${CMAKE_${lang}_COMPILER_ID}${_version}")
else()
message(STATUS "The ${lang} compiler identification is unknown")
endif()
# Check if compiler id detection gave us the compiler tool.
if(CMAKE_${lang}_COMPILER_ID_TOOL)
set(CMAKE_${lang}_COMPILER "${CMAKE_${lang}_COMPILER_ID_TOOL}" PARENT_SCOPE)
elseif(NOT CMAKE_${lang}_COMPILER)
set(CMAKE_${lang}_COMPILER "CMAKE_${lang}_COMPILER-NOTFOUND" PARENT_SCOPE)
endif()
Run Code Online (Sandbox Code Playgroud)
CMAKE_CXX_COMPILER_ID_TOOLCMAKE_DETERMINE_COMPILER_ID_BUILD由同一文件中的宏设置。设置涉及相当多的代码CMAKE_CXX_COMPILER_ID_TOOL(运行进程、捕获其输出、正则表达式匹配等),因此理论上可能其中一些代码(甚至调用的 Visual Studio 命令)对于 Unicode 路径表现不佳。