为什么会出现以下Android Studio错误:“使用-fPIC重新编译"? [英] Why am I getting this Android Studio error: "recompile with -fPIC"?

查看:321
本文介绍了为什么会出现以下Android Studio错误:“使用-fPIC重新编译"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用NDK 18,并使用x86_64 NDK独立工具链分别编译静态库.我可以成功链接它,但是当我尝试以一种不平凡的方式访问该库时,在生成诸如以下内容时会出现很多错误:

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?

遵循此处的建议:用"-fPIC"重新编译;即使添加了-fPIC编译标志,错误仍然存​​在,但我确保使用-fPIC编译静态库(并且使用VERBOSE = 1的CMake确认使用-fPIC),但是仍然出现错误.据我所知,如果不侵入CMake文件,就无法将其编译为共享库.

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.

此处有一些答案:如何使用-fPIC重新编译建议添加-fPIC直接在编译器之后.我也尝试过,但没有成功.

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

示例输出编译静态库(确认cmake使用-fPIC):

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

错误:

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.

推荐答案

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

Your compile step includes the following:

-fPIC -fPIE

-fPIC -fPIE

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

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

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

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

这篇关于为什么会出现以下Android Studio错误:“使用-fPIC重新编译"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆