cmake 命令行无法找到编译器,但 cmake-gui 可以

cmake command line fails to find compiler, but cmake-gui does

我有一个 windows 批处理脚本,它调用带有预加载缓存和构建输出目录的 cmake 命令行。这正确识别了我的 Windows SDK 但不是编译器。

当我 运行 使用相同的源/构建目录的 CMake GUI 时,它工作得很好。我一直在疯狂地试图找出这两种方法之间的任何差异,但还没有。

脚本 (完整的脚本相当长,但我相信我已经将其精简到这里的要点)

REM Check for boost dependency and install local to source tree
SET BOOST_TARGET=%BOOST_INSTALL%
SET BOOST=boost-1.63.0
SET LINK=srcrd_party

IF NOT EXIST "%LINK%\%BOOST%" (
  REM There is additional code here to handle path checking, but all safe ...
  xcopy /Q /Y /I /E "%BOOST_TARGET%\%BOOST%" "%LINK%\%BOOST%"
)

SET BUILDDIR=BUILD

REM do cmake part ... Preload.cmake is a separate file with cmake SET commmands
cmake ^
-G "Visual Studio 14 2015 Win64" ^
-C Preload.cmake ^
-B "%BUILDDIR%" -S src

使用相同的构建和源目录配置 CMake-gui 不会导致任何问题,但上面的脚本显示以下错误。

CMakeError.log(节选)

Compiling the C compiler identification source file "CMakeCCompilerId.c" failed.
Compiler:  
Build flags: 
Id flags:  

The output was:
1
Microsoft (R) Build Engine version 15.7.180.61344 for .NET Framework
Copyright (C) Microsoft Corporation. All rights reserved.

Build started 2/21/2022 3:57:30 PM.
Project "C:\myproject\BUILD\CMakeFiles.14.4\CompilerIdC\CompilerIdC.vcxproj" on node 1 (default targets).
PrepareForBuild:
  Creating directory "Debug\".
  Creating directory "Debug\CompilerIdC.tlog\".
InitializeBuildStatus:
  Creating "Debug\CompilerIdC.tlog\unsuccessfulbuild" because "AlwaysCreate" was specified.
ClCompile:
  C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\x86_amd64\CL.exe /c /nologo /W0 /WX- /Od /D _MBCS /Gm- /EHsc /RTC1 /MDd /GS /fp:precise /Zc:wchar_t /Zc:forScope /Zc:inline /Fo"Debug\" /Fd"Debug\vc140.pdb" /Gd /TC /errorReport:queue CMakeCCompilerId.c
  CMakeCCompilerId.c
Link:
  C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\x86_amd64\link.exe /ERRORREPORT:QUEUE /OUT:".\CompilerIdC.exe" /INCREMENTAL:NO /NOLOGO kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /MANIFEST /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /manifest:embed /PDB:".\CompilerIdC.pdb" /SUBSYSTEM:CONSOLE /TLBID:1 /DYNAMICBASE /NXCOMPAT /IMPLIB:".\CompilerIdC.lib" /MACHINE:X64 Debug\CMakeCCompilerId.obj
LINK : fatal error LNK1181: cannot open input file 'C:\myproject\srcrd_party.obj' [C:\myproject\BUILD\CMakeFiles.14.4\CompilerIdC\CompilerIdC.vcxproj]
Done Building Project "C:\myproject\BUILD\CMakeFiles.14.4\CompilerIdC\CompilerIdC.vcxproj" (default targets) -- FAILED.

Build FAILED.

"C:\myproject\BUILD\CMakeFiles.14.4\CompilerIdC\CompilerIdC.vcxproj" (default target) (1) ->
(Link target) -> 
  LINK : fatal error LNK1181: cannot open input file 'C:\myproject\srcrd_party.obj' [C:\myproject\BUILD\CMakeFiles.14.4\CompilerIdC\CompilerIdC.vcxproj]

    0 Warning(s)
    1 Error(s)

Time Elapsed 00:00:00.78

在我减少脚本来说明问题的过程中,我发现了我的问题。

我的脚本设置了一个名为 LINK 的本地环境变量,这显然破坏了 CMake。 我没有进一步调查以了解这是 CMake 的限制还是我的项目,但解决方法现在对我很有帮助。

例如,我的脚本是

SET LINK=C:\path\to\something
REM do something with %LINK%
cmake -G "Visual Studio 14 2015 Win64" -B BUILD -S src

输出会报无法识别编译器,错误指向找不到“C:\path\to\something.obj”。一开始没看到连接。

同样,不确定问题是出在 CMake 上还是 CMake 在我的项目中使用了什么来测试编译器。

我的修复 是使用 LINK 以外的变量名。