Why am I getting this Android Studio error: “recom

2019-08-25 06:37发布

问题:

I am using NDK 18 and compiling a static library separately using the x86_64 NDK standalone toolchain. I can link it successfully but when I try to access the library in a non-trivial way I am getting dozens of errors when building saying things like:

... requires dynamic R_X86_64_PC32 reloc against '_ZZN4seal4util21get_msb_index_genericEPmmE15deBruijnTable64' which may overflow at runtime; recompile with -fPIC

See my previous question for details on my build files: Why am I still getting undefined reference errors linking a static library with CMake in Android NDK?

Following the suggestions here: "Recompile with -fPIC" error persists even after adding -fPIC compile flag I made sure to compile the static library with -fPIC (and CMake with VERBOSE=1 to confirm -fPIC is used), but I still get the errors. From what I can tell there is no way for me to compile this as a shared library without hacking into it's CMake file.

Some answers here: How to recompile with -fPIC suggest adding -fPIC directly after the compiler. I tried this as well without success.

sample output compiling the static library (confirming cmake is using -fPIC):

Building CXX object CMakeFiles/seal.dir/seal/bigpolyarray.cpp.o
/home/user/AndroidStudioProjects/SEALApp/gen-libs/toolchains/x86_64/bin/x86_64-linux-android-clang++  -fPIC   -I/home/user/AndroidStudioProjects/SEALApp/gen-libs/seal/SEAL_2.3.1/SEAL  -fPIC -fPIE -O3 -DNDEBUG   -std=gnu++17 -o CMakeFiles/seal.dir/seal/bigpolyarray.cpp.o -c /home/user/AndroidStudioProjects/SEALApp/gen-libs/seal/SEAL_2.3.1/SEAL/seal/bigpolyarray.cpp

Errors:

Build command failed.
Error while executing process /home/wyoumans/Android/Sdk/cmake/3.6.4111459/bin/cmake with arguments {--build /home/wyoumans/AndroidStudioProjects/SEALApp/app/.externalNativeBuild/cmake/debug/x86_64 --target native-lib}
[1/2] Building CXX object CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o
[2/2] Linking CXX shared library ../../../../build/intermediates/cmake/debug/obj/x86_64/libnative-lib.so
FAILED: : && /opt/android-ndk-r18/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++  --target=x86_64-none-linux-android21 --gcc-toolchain=/opt/android-ndk-r18/toolchains/x86_64-4.9/prebuilt/linux-x86_64 --sysroot=/opt/android-ndk-r18/sysroot -fPIC -fPIC -isystem /opt/android-ndk-r18/sysroot/usr/include/x86_64-linux-android -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -Wa,--noexecstack -Wformat -Werror=format-security -std=c++11 -std=gnu++1z -O0 -fno-limit-debug-info  -Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libatomic.a -nostdlib++ --sysroot /opt/android-ndk-r18/platforms/android-21/arch-x86_64 -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -L/opt/android-ndk-r18/sources/cxx-stl/llvm-libc++/libs/x86_64 -Wl,--no-undefined -Wl,-z,noexecstack -Qunused-arguments -Wl,-z,relro -Wl,-z,now -shared -Wl,-soname,libnative-lib.so -o ../../../../build/intermediates/cmake/debug/obj/x86_64/libnative-lib.so CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o  ../../../../libs/seal/x86_64/lib/libseal.a /opt/android-ndk-r18/platforms/android-21/arch-x86_64/usr/lib64/liblog.so -latomic -lm "/opt/android-ndk-r18/sources/cxx-stl/llvm-libc++/libs/x86_64/libc++_static.a" "/opt/android-ndk-r18/sources/cxx-stl/llvm-libc++/libs/x86_64/libc++abi.a" && :
/opt/android-ndk-r18/toolchains/x86_64-4.9/prebuilt/linux-x86_64/lib/gcc/x86_64-linux-android/4.9.x/../../../../x86_64-linux-android/bin/ld: error: ../../../../libs/seal/x86_64/lib/libseal.a(bigpoly.cpp.o): requires dynamic R_X86_64_PC32 reloc against '_ZZN4seal4util21get_msb_index_genericEPmmE15deBruijnTable64' which may overflow at runtime; recompile with -fPIC
/opt/android-ndk-r18/toolchains/x86_64-4.9/prebuilt/linux-x86_64/lib/gcc/x86_64-linux-android/4.9.x/../../../../x86_64-linux-android/bin/ld: error: ../../../../libs/seal/x86_64/lib/libseal.a(bigpoly.cpp.o): requires dynamic R_X86_64_PC32 reloc against '_ZTCNSt6__ndk119basic_ostringstreamIcNS_11char_traitsIcEENS_9allocatorIcEEEE0_NS_13basic_ostreamIcS2_EE' which may overflow at runtime; recompile with -fPIC
/opt/android-ndk-r18/toolchains/x86_64-4.9/prebuilt/linux-x86_64/lib/gcc/x86_64-linux-android/4.9.x/../../../../x86_64-linux-android/bin/ld: error: ../../../../libs/seal/x86_64/lib/libseal.a(bigpoly.cpp.o): requires dynamic R_X86_64_PC32 reloc against '_ZTVNSt6__ndk119basic_ostringstreamIcNS_11char_traitsIcEENS_9allocatorIcEEEE' which may overflow at runtime; recompile with -fPIC
/opt/android-ndk-r18/toolchains/x86_64-4.9/prebuilt/linux-x86_64/lib/gcc/x86_64-linux-android/4.9.x/../../../../x86_64-linux-android/bin/ld: error: ../../../../libs/seal/x86_64/lib/libseal.a(bigpoly.cpp.o): requires dynamic R_X86_64_PC32 reloc against '_ZTVNSt6__ndk119basic_ostringstreamIcNS_11char_traitsIcEENS_9allocatorIcEEEE' which may overflow at runtime; recompile with -fPIC
/opt/android-ndk-r18/toolchains/x86_64-4.9/prebuilt/linux-x86_64/lib/gcc/x86_64-linux-android/4.9.x/../../../../x86_64-linux-android/bin/ld: error: ../../../../libs/seal/x86_64/lib/libseal.a(bigpoly.cpp.o): requires dynamic R_X86_64_PC32 reloc against '_ZTVNSt6__ndk119basic_ostringstreamIcNS_11char_traitsIcEENS_9allocatorIcEEEE' which may overflow at runtime; recompile with -fPIC
/opt/android-ndk-r18/toolchains/x86_64-4.9/prebuilt/linux-x86_64/lib/gcc/x86_64-linux-android/4.9.x/../../../../x86_64-linux-android/bin/ld: error: ../../../../libs/seal/x86_64/lib/libseal.a(bigpoly.cpp.o): requires dynamic R_X86_64_PC32 reloc against '_ZTVNSt6__ndk119basic_ostringstreamIcNS_11char_traitsIcEENS_9allocatorIcEEEE' which may overflow at runtime; recompile with -fPIC
/opt/android-ndk-r18/toolchains/x86_64-4.9/prebuilt/linux-x86_64/lib/gcc/x86_64-linux-android/4.9.x/../../../../x86_64-linux-android/bin/ld: error: ../../../../libs/seal/x86_64/lib/libseal.a(bigpoly.cpp.o): requires dynamic R_X86_64_PC32 reloc against '_ZTVNSt6__ndk115basic_stringbufIcNS_11char_traitsIcEENS_9allocatorIcEEEE' which may overflow at runtime; recompile with -fPIC
/opt/android-ndk-r18/toolchains/x86_64-4.9/prebuilt/linux-x86_64/lib/gcc/x86_64-linux-android/4.9.x/../../../../x86_64-linux-android/bin/ld: error: ../../../../libs/seal/x86_64/lib/libseal.a(bigpoly.cpp.o): requires dynamic R_X86_64_PC32 reloc against '_ZTVNSt6__ndk115basic_stringbufIcNS_11char_traitsIcEENS_9allocatorIcEEEE' which may overflow at runtime; recompile with -fPIC
/opt/android-ndk-r18/toolchains/x86_64-4.9/prebuilt/linux-x86_64/lib/gcc/x86_64-linux-android/4.9.x/../../../../x86_64-linux-android/bin/ld: error: ../../../../libs/seal/x86_64/lib/libseal.a(bigpoly.cpp.o): requires dynamic R_X86_64_PC32 reloc against '_ZTVNSt6__ndk115basic_streambufIcNS_11char_traitsIcEEEE' which may overflow at runtime; recompile with -fPIC
/opt/android-ndk-r18/toolchains/x86_64-4.9/prebuilt/linux-x86_64/lib/gcc/x86_64-linux-android/4.9.x/../../../../x86_64-linux-android/bin/ld: error: ../../../../libs/seal/x86_64/lib/libseal.a(bigpoly.cpp.o): requires dynamic R_X86_64_PC32 reloc against '_ZTVNSt6__ndk115basic_streambufIcNS_11char_traitsIcEEEE' which may overflow at runtime; recompile with -fPIC
/opt/android-ndk-r18/toolchains/x86_64-4.9/prebuilt/linux-x86_64/lib/gcc/x86_64-linux-android/4.9.x/../../../../x86_64-linux-android/bin/ld: error: ../../../../libs/seal/x86_64/lib/libseal.a(biguint.cpp.o): requires dynamic R_X86_64_PC32 reloc against '_ZZN4seal4util21get_msb_index_genericEPmmE15deBruijnTable64' which may overflow at runtime; recompile with -fPIC
/opt/android-ndk-r18/toolchains/x86_64-4.9/prebuilt/linux-x86_64/lib/gcc/x86_64-linux-android/4.9.x/../../../../x86_64-linux-android/bin/ld: error: ../../../../libs/seal/x86_64/lib/libseal.a(smallmodulus.cpp.o): requires dynamic R_X86_64_PC32 reloc against '_ZZN4seal4util21get_msb_index_genericEPmmE15deBruijnTable64' which may overflow at runtime; recompile with -fPIC
/opt/android-ndk-r18/toolchains/x86_64-4.9/prebuilt/linux-x86_64/lib/gcc/x86_64-linux-android/4.9.x/../../../../x86_64-linux-android/bin/ld: error: ../../../../libs/seal/x86_64/lib/libseal.a(globals.cpp.o): requires dynamic R_X86_64_PC32 reloc against '_ZN4seal4util16global_variables18global_memory_poolE' which may overflow at runtime; recompile with -fPIC
/opt/android-ndk-r18/toolchains/x86_64-4.9/prebuilt/linux-x86_64/lib/gcc/x86_64-linux-android/4.9.x/../../../../x86_64-linux-android/bin/ld: error: ../../../../libs/seal/x86_64/lib/libseal.a(hash.cpp.o): requires dynamic R_X86_64_PC32 reloc against '_ZN4seal4util12HashFunction3rhoE' which may overflow at runtime; recompile with -fPIC
/opt/android-ndk-r18/toolchains/x86_64-4.9/prebuilt/linux-x86_64/lib/gcc/x86_64-linux-android/4.9.x/../../../../x86_64-linux-android/bin/ld: error: ../../../../libs/seal/x86_64/lib/libseal.a(mempool.cpp.o): requires dynamic R_X86_64_PC32 reloc against '_ZTVN4seal4util16MemoryPoolHeadMTE' which may overflow at runtime; recompile with -fPIC
/opt/android-ndk-r18/toolchains/x86_64-4.9/prebuilt/linux-x86_64/lib/gcc/x86_64-linux-android/4.9.x/../../../../x86_64-linux-android/bin/ld: error: ../../../../libs/seal/x86_64/lib/libseal.a(uintarith.cpp.o): requires dynamic R_X86_64_PC32 reloc against '_ZZN4seal4util21get_msb_index_genericEPmmE15deBruijnTable64' which may overflow at runtime; recompile with -fPIC
/opt/android-ndk-r18/toolchains/x86_64-4.9/prebuilt/linux-x86_64/lib/gcc/x86_64-linux-android/4.9.x/../../../../x86_64-linux-android/bin/ld: warning: shared library text segment is not shareable
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.

回答1:

Your compile step includes the following:

-fPIC -fPIE

I believe what is happening is that the latter is overriding the former. PIE is only valid for executables, not libraries.

I think the -fPIE flag is probably coming from your build scripts? The NDK CMake toolchain file does append -fPIE, but only for executables.