如何调试 cmake 失败并显示“编译器无法编译简单的测试程序”的原因。

oar*_*ish 1 macos cmake

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

\n

作为一个例子,我尝试编译 OpenCV:

\n
$ cmake ..\n-- The CXX compiler identification is AppleClang 12.0.0.12000032\n-- The C compiler identification is AppleClang 12.0.0.12000032\n-- Detecting CXX compiler ABI info\n-- Detecting CXX compiler ABI info - failed\n-- Check for working CXX compiler: /usr/local/opt/ccache/libexec/c++\n-- Check for working CXX compiler: /usr/local/opt/ccache/libexec/c++ - broken\nCMake Error at /usr/local/Cellar/cmake/3.20.5/share/cmake/Modules/CMakeTestCXXCompiler.cmake:59 (message):\n  The C++ compiler\n\n    "/usr/local/opt/ccache/libexec/c++"\n\n  is not able to compile a simple test program.\n\n  It fails with the following output:\n\n    Change Dir: custom_dep_location/downloads/opencv-4.1.1/build/CMakeFiles/CMakeTmp\n    \n    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\n    Building CXX object CMakeFiles/cmTC_35974.dir/testCXXCompiler.cxx.o\n    /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\n    clang: warning: argument unused during compilation: '-Lcustom_dep_location/lib' [-Wunused-command-line-argument]\n    Linking CXX executable cmTC_35974\n    /usr/local/Cellar/cmake/3.20.5/bin/cmake -E cmake_link_script CMakeFiles/cmTC_35974.dir/link.txt --verbose=1\n    /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 \n    ld: warning: directory not found for option '-Lcustom_dep_location/lib'\n    clang: error: linker command failed with exit code 1 (use -v to see invocation)\n    make[1]: *** [cmTC_35974] Error 1\n    make: *** [cmTC_35974/fast] Error 2\n    \n    \n\n  \n\n  CMake will not be able to correctly generate this project.\nCall Stack (most recent call first):\n  CMakeLists.txt:108 (project)\n\n\n-- Configuring incomplete, errors occurred!\nSee also "custom_dep_location/downloads/opencv-4.1.1/build/CMakeFiles/CMakeOutput.log".\nSee also "custom_dep_location/downloads/opencv-4.1.1/build/CMakeFiles/CMakeError.log".\n
Run Code Online (Sandbox Code Playgroud)\n

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

\n
    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\n
Run Code Online (Sandbox Code Playgroud)\n

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

\n
\n

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

\n

Ale*_*ing 5

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

传递--debug-trycompilecmake。来自文档

不要删除try_compile()构建树。try_compile()一次仅对一个有用。

不要删除为try_compile()呼叫创建的文件和目录。这在调试失败的 try_compile 时很有用。但是,它可能会更改尝试编译的结果,因为先前尝试编译的旧垃圾可能会导致不同的测试错误地通过或失败。此选项最适合一次尝试编译一次,并且仅在调试时使用。