android-studio - 为什么我会收到此 Android Studio 错误:“使用 -fPIC 重新编译”?
问题描述
我正在使用 NDK 18 并使用 x86_64 NDK 独立工具链单独编译静态库。我可以成功链接它,但是当我尝试以一种不平凡的方式访问库时,我在构建时遇到了几十个错误,例如:
... requires dynamic R_X86_64_PC32 reloc against '_ZZN4seal4util21get_msb_index_genericEPmmE15deBruijnTable64' which may overflow at runtime; recompile with -fPIC
有关我的构建文件的详细信息,请参阅我之前的问题:为什么在 Android NDK 中将静态库与 CMake 链接时仍然出现未定义的参考错误?
遵循此处的建议:“使用 -fPIC 重新编译”错误仍然存在,即使在添加 -fPIC 编译标志后我确保使用-fPIC
(并使用 VERBOSE=1 确认-fPIC
使用 CMake)编译静态库,但我仍然得到错误。据我所知,我无法在不侵入其 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 工具链文件确实 append -fPIE
,但仅适用于可执行文件。
推荐阅读
- angular - 如何修复 angular-cli 中的原理图问题/无法创建新的 Angular 应用程序
- xml - 如何将xml文本作为简单文本
- laravel - Laravel 在哪里没有得到任何结果?
- angular - Angular - 带有样式的 InnerHtml
- python - ttk.Entry:如果文本右对齐,则看不到插入符号
- node.js - 反对 js:无法使用连接表额外属性更新多对多关系。说附加属性无效
- reactjs - 如何设置堆叠条的最小高度(顶点图,堆叠条)
- reactjs - 使用 TypeScript 反应道具类型 - 如何拥有函数类型?
- angular - 使用 NzModalFooterDirective 时测试组件
- javascript - 数组不能用作引用类型,数组是否属于 JavaScript 中的引用数据类型