Is there an easy way to get a reproducer when cmake can't build a simple test program?

Sample from the cmake output:

Change Dir: '/home/chriselrod/Documents/progwork/cxx/Math/buildclang/test/CMakeFiles/CMakeScratch/TryCompile-siaNVZ'
    
    Run Build Command(s): /usr/bin/cmake -E env VERBOSE=1 /bin/gmake -f Makefile cmTC_a67a8/fast
    /bin/gmake  -f CMakeFiles/cmTC_a67a8.dir/build.make CMakeFiles/cmTC_a67a8.dir/build
    gmake[1]: Entering directory '/home/chriselrod/Documents/progwork/cxx/Math/buildclang/test/CMakeFiles/CMakeScratch/TryCompile-siaNVZ'
    Building C object CMakeFiles/cmTC_a67a8.dir/testCCompiler.c.o
    /usr/local/bin/clang   -fno-omit-frame-pointer -fsanitize=address,undefined  -MD -MT CMakeFiles/cmTC_a67a8.dir/testCCompiler.c.o -MF CMakeFiles/cmTC_a67a8.dir/testCCompiler.c.o.d -o CMakeFiles/cmTC_a67a8.dir/testCCompiler.c.o -c /home/chriselrod/Documents/progwork/cxx/Math/buildclang/test/CMakeFiles/CMakeScratch/TryCompile-siaNVZ/testCCompiler.c
    Linking C executable cmTC_a67a8
    /usr/bin/cmake -E cmake_link_script CMakeFiles/cmTC_a67a8.dir/link.txt --verbose=1
    /usr/local/bin/clang  -fno-omit-frame-pointer -fsanitize=address,undefined  CMakeFiles/cmTC_a67a8.dir/testCCompiler.c.o -o cmTC_a67a8 
    /bin/ld: /home/chriselrod/.local/stow/llvm/lib/clang/17/lib/x86_64-unknown-linux-gnu/libclang_rt.asan.a(sanitizer_unwind_linux_libcdep.cpp.o): in function `__sanitizer::BufferedStackTrace::UnwindSlow(unsigned long, unsigned int)':
    /home/chriselrod/Documents/libraries/llvm-project/compiler-rt/lib/sanitizer_common/sanitizer_unwind_linux_libcdep.cpp:130:(.text._ZN11__sanitizer18BufferedStackTrace10UnwindSlowEmj+0x40): undefined reference to `_Unwind_Backtrace'

The problem is that
a) Running /usr/local/bin/clang -fsanitize=address;undefined from the command line works. It also works from CMake when not using sanitizers.
b) CMake wipes the CMakeScratch directory after the failure so that I can’t investigate what went wrong.

> ls /home/chriselrod/Documents/progwork/cxx/Math/buildclang/test/CMakeFiles/CMakeScratch/
> 

The fact that the undefined references all come from libunwind make it seem like something is going wrong there from CMake, or perhaps my example when compiling from the command line is too minimal.
I have little idea of what is going on, and finding out would be easier if I had an example.

How dit you set this analizer comiler flags?

You may try to use cmake --debug-trycompile …

Do you know Getting Started — project_options 0.28.0 documentation

1 Like

Thank you, --debug-trycompile is what I was looking for.
Adding -lunwind to link libunwind lets the example compile.

I used https://github.com/StableCoder/cmake-scripts/blob/main/sanitizers.cmake
This works for me when using a system toolchain (both gcc and clang), but not for the toolchain I built from source.

I am using target options for most of the project, but for this I am using

if(((USE_SANITIZER MATCHES "([Aa]ddress)") OR (USE_SANITIZER MATCHES "([Aa]ddress);([Uu]ndefined)"
                                               )
) AND (CMAKE_CXX_COMPILER_ID MATCHES "Clang"))
  set(CMAKE_EXE_LINKER_FLAGS  "${CMAKE_EXE_LINKER_FLAGS} -lunwind -Wno-unused-command-line-argument")
endif()

before adding gtest as a dependency, as I need to get gtest to apply lunwind.
Unfortunately, there are cases where lunwind is not needed, causing errors from the unused cmd line arguments, requiring that flag.
This sounds like a bad way of doing this, but it’s the first solution I came upon that works.