我如何调试为什么 cmake 因 "Compiler is not able to compile a simple test program." 而失败

How can I debug why cmake fails with "Compiler is not able to compile a simple test program."

我知道 cmake 期间初始平台检查失败的几个原因,但我无法弄清楚,主要是因为 cmake 似乎删除了它用于检查的目录。

例如,我尝试编译 OpenCV:

$ cmake ..
-- The CXX compiler identification is AppleClang 12.0.0.12000032
-- The C compiler identification is AppleClang 12.0.0.12000032
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - failed
-- Check for working CXX compiler: /usr/local/opt/ccache/libexec/c++
-- Check for working CXX compiler: /usr/local/opt/ccache/libexec/c++ - broken
CMake Error at /usr/local/Cellar/cmake/3.20.5/share/cmake/Modules/CMakeTestCXXCompiler.cmake:59 (message):
  The C++ compiler

    "/usr/local/opt/ccache/libexec/c++"

  is not able to compile a simple test program.

  It fails with the following output:

    Change Dir: custom_dep_location/downloads/opencv-4.1.1/build/CMakeFiles/CMakeTmp
    
    Run Build Command(s):/usr/bin/make -f Makefile cmTC_35974/fast && /Library/Developer/CommandLineTools/usr/bin/make  -f CMakeFiles/cmTC_35974.dir/build.make CMakeFiles/cmTC_35974.dir/build
    Building CXX object CMakeFiles/cmTC_35974.dir/testCXXCompiler.cxx.o
    /usr/local/opt/ccache/libexec/c++   -Icustom_dep_location/include -Lcustom_dep_location/lib  -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX11.1.sdk -mmacosx-version-min=10.15 -fPIE -MD -MT CMakeFiles/cmTC_35974.dir/testCXXCompiler.cxx.o -MF CMakeFiles/cmTC_35974.dir/testCXXCompiler.cxx.o.d -o CMakeFiles/cmTC_35974.dir/testCXXCompiler.cxx.o -c custom_dep_location/downloads/opencv-4.1.1/build/CMakeFiles/CMakeTmp/testCXXCompiler.cxx
    clang: warning: argument unused during compilation: '-Lcustom_dep_location/lib' [-Wunused-command-line-argument]
    Linking CXX executable cmTC_35974
    /usr/local/Cellar/cmake/3.20.5/bin/cmake -E cmake_link_script CMakeFiles/cmTC_35974.dir/link.txt --verbose=1
    /usr/local/opt/ccache/libexec/c++ -Icustom_dep_location/include -Lcustom_dep_location/lib  -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX11.1.sdk -mmacosx-version-min=10.15 -Wl,-search_paths_first -Wl,-headerpad_max_install_names CMakeFiles/cmTC_35974.dir/testCXXCompiler.cxx.o -o cmTC_35974 
    ld: warning: directory not found for option '-Lcustom_dep_location/lib'
    clang: error: linker command failed with exit code 1 (use -v to see invocation)
    make[1]: *** [cmTC_35974] Error 1
    make: *** [cmTC_35974/fast] Error 2
    
    

  

  CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
  CMakeLists.txt:108 (project)


-- Configuring incomplete, errors occurred!
See also "custom_dep_location/downloads/opencv-4.1.1/build/CMakeFiles/CMakeOutput.log".
See also "custom_dep_location/downloads/opencv-4.1.1/build/CMakeFiles/CMakeError.log".

所以我很自然地想知道到底是什么失败了,可悲的是没有提到。而且似乎用于检查的临时文件夹实际上并不存在。 当我查看

    Run Build Command(s):/usr/bin/make -f Makefile cmTC_35974/fast && /Library/Developer/CommandLineTools/usr/bin/make  -f CMakeFiles/cmTC_35974.dir/build.make CMakeFiles/cmTC_35974.dir/build

我假设目录是相对于从中调用 cmake 的目录(因此 cwd),但它们不存在,因此我无法手动重新运行命令以获取更多错误消息。 它们也不是 /var/… 或我系统上其他任何地方的临时目录


如何让 cmake 保留其临时目录、找到它们并手动执行它正在尝试和失败的命令?

How can I make cmake preserve its temporary directories, find them, and manually execute the commands it is trying and failing with?

--debug-trycompile 传递给 cmake。来自 the docs:

Do not delete the try_compile() build tree. Only useful on one try_compile() at a time.

Do not delete the files and directories created for try_compile() calls. This is useful in debugging failed try_compiles. It may however change the results of the try-compiles as old junk from a previous try-compile may cause a different test to either pass or fail incorrectly. This option is best used for one try-compile at a time, and only when debugging.