AppleClang + GFortran no compact unwind warnings

On macOS when using the default “AppleClang” compiler in a Fortran project where GFortran objects are linked with C / C++ AppleClang objects, the ld linker may emit warnings like:

ld: warning: could not create compact unwind for ...: register .. saved somewhere other than in frame
ld: warning: could not create compact unwind for ...: registers .. and .. not saved contiguously in frame

This is an actual issue because C++ exception handling will not completely work when this warning is emitted from C++ code coupled with Fortran code. In general, using C++ exception handling within C++ code that is linked with Fortran code will work just fine, except when this warning is issued. The solution is to use GNU GCC C++ compiler with GFortran instead of mixing AppleClang with GFortran.

Specifying environment variable:

LDFLAGS="$LDFLAGS -Wl,-no_compact_unwind"

removes the warning, but this also disables C++ exception handling so it is not recommended.

It is possible to programmatically detect this link conflict from CMake using try_compile. In this example, directory “abi_check” contains a small C program that links with a Fortran program.

if(NOT DEFINED ok_compile)

message(CHECK_START "Checking for C++ exception handling with Fortran")

try_compile(ok_compile
PROJECT exception_check
SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}/exception_check
CMAKE_FLAGS -DCMAKE_LINK_WARNING_AS_ERROR:BOOL=on
OUTPUT_VARIABLE _out
)

# _out is used for CMake < 4.0

if(NOT ok_compile OR _out MATCHES "ld: warning: could not create compact unwind for")
  message(CHECK_FAIL "C++ exception handling will not work reliably due to incompatible compilers:
  C++ compiler ${CMAKE_CXX_COMPILER_ID} ${CMAKE_CXX_COMPILER_VERSION}
  Fortran compiler ${CMAKE_Fortran_COMPILER_ID} ${CMAKE_Fortran_COMPILER_VERSION}"
  )
else()
  message(CHECK_PASS "OK")
endif()

endif()