针对armeabi-v7a的Android NDK链接器失败:“ PLT偏移量太大,请尝试使用--long-plt进行链接”

时间:2018-10-26 21:49:31

标签: android cmake android-ndk llvm-clang

尝试构建已签名的APK时,它会失败,并重复大约100行:

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

我在参数中添加了--long-plt

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

但是它似乎并没有改变任何东西。

它可与非签名(调试)apk生成一起使用,并与arm64-v8a一起使用。

我正在处理> 1GB的本地代码,所以我猜这是主要原因。

似乎几乎没有与此相关的文档或搜索结果。

是否需要将--long-plt放在其他地方?如果不是,是否还有其他可以更改的设置?还是将代码拆分为单独的库会有帮助?

这里是CMakeLists.txt供参考:

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}
                     )

1 个答案:

答案 0 :(得分:-1)

我同意,即使我们能找到一些文档,互联网上也没有太多文档。

首先,尝试修改您的配置文件:

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

让我知道,如果它有任何改变吗?