Android NDK22 with LTO enabled crash as gold linker is no longer available
Android NDK22 removed the gold linker and made lld the default. However, CMake forces the use of gold linker in https://github.com/Kitware/CMake/blob/af695cee7346fa0eee478f71952fd02f0df989cf/Modules/Compiler/Clang.cmake#L81
This is problematic as CMAKE_CXX_LINK_OPTIONS_IPO are appended after any other user flags, overriding any custom linker defined by either android.toolchain.cmake or the user.
This means that Android NDK22 CMake builds with LTO enabled crash, and right now the best work-around is something like:
STRING(REGEX REPLACE "-fuse-ld=gold" "" CMAKE_CXX_LINK_OPTIONS_IPO ${CMAKE_CXX_LINK_OPTIONS_IPO})
But this seems rather suboptimal.
I'm not sure whether this should be fixed in the Android NDK or CMake, but I created a similar issue on the NDK side https://github.com/android/ndk/issues/1444
Edited by Christian Melchior