Android: Suppress implicit -lstdc++ linker flag

The chosen STL libraries are already linked explicitly so we shouldn't
let the compiler add its implicit `-lstdc++` (the default) when invoking
the linker.

Fixes: #17863
NDK-Issue: https://github.com/android-ndk/ndk/issues/105
Inspired-by: Tom Hughes <tomtheengineer@gmail.com>
This commit is contained in:
Brad King
2018-04-03 11:31:59 -04:00
parent 12e6796b62
commit 843d55de29
11 changed files with 45 additions and 0 deletions

View File

@@ -1,2 +1,9 @@
include(Platform/Android-Clang)
__android_compiler_clang(CXX)
if(_ANDROID_STL_NOSTDLIBXX)
if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 6)
string(APPEND CMAKE_CXX_STANDARD_LIBRARIES " -nostdlib++")
else()
string(APPEND CMAKE_CXX_STANDARD_LIBRARIES " -nodefaultlibs -lgcc -lc -lm -ldl")
endif()
endif()

View File

@@ -1,2 +1,5 @@
include(Platform/Android-GNU)
__android_compiler_gnu(CXX)
if(_ANDROID_STL_NOSTDLIBXX)
string(APPEND CMAKE_CXX_STANDARD_LIBRARIES " -nodefaultlibs -lgcc -lc -lm -ldl")
endif()

View File

@@ -1,6 +1,7 @@
# <ndk>/sources/cxx-stl/llvm-libc++/Android.mk
set(_ANDROID_STL_RTTI 1)
set(_ANDROID_STL_EXCEPTIONS 1)
set(_ANDROID_STL_NOSTDLIBXX 1)
macro(__android_stl_cxx lang filename)
# Add the include directory.
if(EXISTS "${CMAKE_ANDROID_NDK}/sources/cxx-stl/llvm-libc++/libcxx/include/cstddef")

View File

@@ -1,6 +1,7 @@
# <ndk>/sources/cxx-stl/gabi++/Android.mk
set(_ANDROID_STL_RTTI 1)
set(_ANDROID_STL_EXCEPTIONS 1)
set(_ANDROID_STL_NOSTDLIBXX 1)
macro(__android_stl_gabixx lang filename)
__android_stl_inc(${lang} "${CMAKE_ANDROID_NDK}/sources/cxx-stl/gabi++/include" 1)
__android_stl_lib(${lang} "${CMAKE_ANDROID_NDK}/sources/cxx-stl/gabi++/libs/${CMAKE_ANDROID_ARCH_ABI}/${filename}" 1)

View File

@@ -1,6 +1,7 @@
# <ndk>/sources/cxx-stl/gnu-libstdc++/Android.mk
set(_ANDROID_STL_RTTI 1)
set(_ANDROID_STL_EXCEPTIONS 1)
set(_ANDROID_STL_NOSTDLIBXX 1)
macro(__android_stl_gnustl lang filename)
__android_stl_inc(${lang} "${CMAKE_ANDROID_NDK}/sources/cxx-stl/gnu-libstdc++/${CMAKE_${lang}_ANDROID_TOOLCHAIN_VERSION}/include" 1)
__android_stl_inc(${lang} "${CMAKE_ANDROID_NDK}/sources/cxx-stl/gnu-libstdc++/${CMAKE_${lang}_ANDROID_TOOLCHAIN_VERSION}/libs/${CMAKE_ANDROID_ARCH_ABI}/include" 1)

View File

@@ -1,2 +1,3 @@
set(_ANDROID_STL_NOSTDLIBXX 1)
macro(__android_stl lang)
endmacro()

View File

@@ -1,6 +1,7 @@
# <ndk>/sources/cxx-stl/stlport/Android.mk
set(_ANDROID_STL_RTTI 1)
set(_ANDROID_STL_EXCEPTIONS 1)
set(_ANDROID_STL_NOSTDLIBXX 0)
macro(__android_stl_stlport lang filename)
__android_stl_inc(${lang} "${CMAKE_ANDROID_NDK}/sources/cxx-stl/stlport/stlport" 1)
__android_stl_lib(${lang} "${CMAKE_ANDROID_NDK}/sources/cxx-stl/stlport/libs/${CMAKE_ANDROID_ARCH_ABI}/${filename}" 1)

View File

@@ -1,6 +1,7 @@
# <ndk>/android-ndk-r11c/sources/cxx-stl/system/Android.mk
set(_ANDROID_STL_RTTI 0)
set(_ANDROID_STL_EXCEPTIONS 0)
set(_ANDROID_STL_NOSTDLIBXX 0)
macro(__android_stl lang)
__android_stl_inc(${lang} "${CMAKE_ANDROID_NDK}/sources/cxx-stl/system/include" 1)
endmacro()

View File

@@ -0,0 +1,4 @@
int android_lib()
{
return 0;
}

View File

@@ -0,0 +1,8 @@
if(NOT EXISTS "${file}")
message(FATAL_ERROR "Missing file:\n ${file}")
endif()
execute_process(COMMAND "${objdump}" -p ${file} OUTPUT_VARIABLE out)
if(out MATCHES "NEEDED[^\n]*stdc\\+\\+")
string(REPLACE "\n" "\n " out " ${out}")
message(FATAL_ERROR "File:\n ${file}\ndepends on libstdc++:\n${out}")
endif()

View File

@@ -92,6 +92,23 @@ if(CMAKE_ANDROID_ARCH_ABI STREQUAL "armeabi-v7a")
endif()
add_executable(android_c android.c)
add_executable(android_cxx android.cxx)
add_library(android_cxx_lib SHARED android_lib.cxx)
set(objdump "${CMAKE_CXX_ANDROID_TOOLCHAIN_PREFIX}objdump")
if(NOT EXISTS "${objdump}")
message(FATAL_ERROR "Expected tool missing:\n ${objdump}")
endif()
if(NOT CMAKE_ANDROID_STL_TYPE MATCHES "^(system|stlport_static|stlport_shared)$")
foreach(tgt android_cxx android_cxx_lib)
add_custom_command(TARGET ${tgt} POST_BUILD
COMMAND ${CMAKE_COMMAND}
-Dobjdump=${objdump}
-Dfile=$<TARGET_FILE:${tgt}>
-P ${CMAKE_CURRENT_SOURCE_DIR}/check_binary.cmake
)
endforeach()
endif()
# Test that an explicit /usr/include is ignored in favor of
# appearing as a standard include directory at the end.