feature: LINK_WARNING_AS_ERROR
I have found COMPILE_WARNING_AS_ERROR useful for CI and in try_compile
to detect issues at configure time.
A LINK_WARNING_AS_ERROR property would be useful to catch link-time warnings as errors in CI, to help users debug a build on their system, and in try_compile
to detect anomalous conditions.
Currently, I regex try_compile log to detect linker problems, which is a maintenance burden and shaky.
Example
cmake_minimum_required(VERSION 3.25)
set(CMAKE_EXE_LINKER_FLAGS_INIT -ld64)
# this will generate a linker warning, as an example for AppleClang
project(prop LANGUAGES C CXX)
set(CMAKE_LINK_WARNING_AS_ERROR true) # proposed
set(CMAKE_TRY_COMPILE_PLATFORM_VARIABLES CMAKE_LINK_WARNING_AS_ERROR)
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/main.c "int func();
int main(void) { func(); return 0; }")
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/lib.cxx "extern \"C\" int func(){ return 0; }")
try_compile(res SOURCES ${CMAKE_CURRENT_BINARY_DIR}/main.c ${CMAKE_CURRENT_BINARY_DIR}/lib.cxx)
message(STATUS "try_compile: ${res}")
# when CMAKE_LINK_WARNING_AS_ERROR is working, this would be FALSE
Specific use case
In some cases, using AppleClang with GNU GFortran, C++ exception handling may be broken and is accompanied by linker warnings. That is, when running the target, C++ exceptions may not "catch" and the program halts at runtime instead of using the "catch" C++ user code.
I handle this situation now by using "try_compile()" and have CMake regex try_compile() linker warnings, and/or "try_run()" and warning the user C++ exception handling may not work. A more general approach is the option for linker warnings as errors.