Wednesday, 31 October 2018

Android NDK Linker failure for armeabi-v7a: "PLT offset too large, try linking with --long-plt"

When trying to build a signed APK, it fails with ~100 lines repeating:

Library/Android/sdk/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64/lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/bin/ld: error: PLT offset too large, try linking with --long-plt

I've added --long-plt to the arguments:

externalNativeBuild {
    cmake {
        ...
        arguments '-DANDROID_STL=c++_static', '-Wl,--long-plt'
        cppFlags "-frtti -fexceptions", "-DANDROID_TOOLCHAIN=clang", "-DANDROID_STL=c++_shared"
    }
}

But it doesn't seem to change anything.

It works with non-signed (debug) apk generation and works with arm64-v8a.

I'm dealing with >1GB of native code, so I'm guessing that's the main reason.

It seems there is almost no documentation or search result regarding this.

Does --long-plt need to be put somewhere else? If not, is there another setting that can be changed? Or would splitting the code into separate libraries help?

Here's the CMakeLists.txt for reference:

string(REPLACE "." "/" JAVA_GEN_SUBDIR ${JAVA_GEN_PACKAGE})
set(JAVA_GEN_DIR ${Project_SOURCE_DIR}/src/main/java/${JAVA_GEN_SUBDIR}/../../../../../generated)

# configure import libs
set(distribution_DIR ${PROJECT_SOURCE_DIR}/distribution)

# Sets the minimum version of CMake required to build the native library.

cmake_minimum_required(VERSION 3.4.1)

# Note: One could use a 'GLOB' here, but newly added source files won't auto-regen build files
# After adding files, you'd need to 'touch' the CMakeLists.txt to re-gen

# SWIG required for build. Easy install is "brew install swig"
#Site: http://swig.org
find_package(SWIG REQUIRED)
include(${SWIG_USE_FILE})

# Remove old java files, in case we don't need to generate some of them anymore
#file(REMOVE_RECURSE ${JAVA_GEN_DIR})

# Ensure file recognized as C++ (otherwise, exported as C file)
set_property(SOURCE src/main/cpp/${LIBRARY_NAME}.i PROPERTY CPLUSPLUS ON)

# Setup SWIG flags and locations
set(CMAKE_SWIG_FLAGS -c++ -package ${JAVA_GEN_PACKAGE})
set(CMAKE_SWIG_OUTDIR ${JAVA_GEN_DIR})

# Export a wrapper file to Java, and link with the created C++ library
swig_add_module(${LIBRARY_NAME}_Wrapper java ${SWIG_I_FILE})
swig_link_libraries(${LIBRARY_NAME}_Wrapper ${LIBRARY_NAME})



# Include a location to the header files
include_directories(
    src/main/cpp
    ${NativeLibPath}
    ${LUAPATH}
    )

set(LUAPATH ${NativeLibPath}/lua)

    file(GLOB ALL_SOURCE_FILES
        "src/main/cpp/*.cpp"
        "${NativeLibPath}/*.cpp"
        "${LUAPATH}/*.c"
    )

# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.
add_library( # Sets the name of the library.
             ${LIBRARY_NAME}

             # Sets the library as a shared library.
             SHARED

             # Provides the list of files to compile.
             ${ALL_SOURCE_FILES} )

target_include_directories(${LIBRARY_NAME} PRIVATE)

# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.

find_library( # Sets the name of the path variable.
              log-lib

              # Specifies the name of the NDK library that
              # you want CMake to locate.
              log )

# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.
target_link_libraries(
                       # Specifies the target library.
                       ${LIBRARY_NAME}
                       android
                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib}
                     )



from Android NDK Linker failure for armeabi-v7a: "PLT offset too large, try linking with --long-plt"

No comments:

Post a Comment