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++ 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 is not recommended.

It is possible to programmatically detect this link conflict from CMake using try_compile.

try_compile(abi_compile
PROJECT abi_check
SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}/abi_check
OUTPUT_VARIABLE abi_output
)

if(abi_output MATCHES "ld: warning: could not create compact unwind for")
  message(WARNING "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}"
  )
endif()

where directory “abi_check” contains a small C program that links with a Fortran program.