Why am I getting this Android Studio error: "recompile with -fPIC"?

Why am I getting this Android Studio error: "recompile with -fPIC"?

我正在使用 NDK 18 并使用 x86_64 NDK 独立工具链单独编译静态库。我可以 link 它成功但是当我尝试以一种非常重要的方式访问库时,我在构建时遇到了很多错误,比如:

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

有关我的构建文件的详细信息,请参阅我之前的问题:

按照此处的建议: 我确保使用 -fPIC 编译静态库(并使用 VERBOSE=1 的 CMake 确认使用了 -fPIC),但我仍然得到错误。据我所知,如果不侵入它的 CMake 文件,我无法将其编译为共享库。

这里有一些回答:How to recompile with -fPIC建议在编译器后面直接加上-fPIC。我也尝试过但没有成功。

编译静态库的示例输出(确认 cmake 正在使用 -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

错误:

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.

您的编译步骤包括以下内容:

-fPIC -fPIE

我相信正在发生的事情是后者压倒了前者。 PIE 仅对可执行文件有效,对库无效。

我认为 -fPIE 标志可能来自您的构建脚本? NDK CMake 工具链文件确实附加 -fPIE,但仅适用于可执行文件。