【问题标题】:How can I debug why cmake fails with "Compiler is not able to compile a simple test program."如何调试 cmake 失败并显示“编译器无法编译简单的测试程序”的原因。
【发布时间】:2021-08-16 10:09:05
【问题描述】:

我知道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

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


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

【问题讨论】:

  • 我会注意到,如果您只想在系统 C++ 编译器中使用ccache,您应该使用CMAKE_CXX_COMPILER_LAUNCHER 而不是包装器。这适用于 make 和 Ninja ...... Xcode 需要包装器(meh)。
  • 不相关,但 OpenCVs cmake 进程本身似乎并没有这样做。

标签: macos cmake


【解决方案1】:

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

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

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

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

【讨论】:

    猜你喜欢
    • 2021-03-18
    • 1970-01-01
    • 2019-09-11
    • 2013-01-13
    • 1970-01-01
    • 2015-08-13
    • 1970-01-01
    • 2017-11-16
    • 2019-05-07
    相关资源
    最近更新 更多