gpt4 book ai didi

g++ - 当 fPIC 已被使用时,在创建共享对象时不能使用针对符号的重定位 R_X86_64_PC32

转载 作者:行者123 更新时间:2023-12-05 02:49:05 26 4
gpt4 key购买 nike

我看了很多关于解决这种类型的链接器错误的帖子,在大多数情况下,人们只是忘记了使用 -fPIC 进行编译,有时人们在使用内联函数时遇到了麻烦,等等。这不是这里的情况。我正在尝试使用 Pybind11 为 python 包装一个 C++ 库.在这个过程中,我想将一些静态库(其中一个是 newmat11 )链接到一个 .so 文件中。

我使用带有 -fPIC 的 automake 系统构建 newmat11 库(这里有一些输出...)

...
g++ -DHAVE_CONFIG_H -I. -g -O2 -MT newmat8.lo -MD -MP -MF .deps/newmat8.Tpo -c newmat8.cpp -fPIC -DPIC -o newmat8.o
...
ar cru libnewmat11.a bandmat.o cholesky.o evalue.o fft.o jacobi.o hholder.o myexcept.o newfft.o newmat1.o newmat2.o newmat3.o newmat4.o newmat5.o newmat6.o newmat7.o newmat8.o newmat9.o newmatex.o newmatrm.o solution.o sort.o submat.o svd.o

确实,当我运行 readelf --relocs libnewmat11.a 时,我看到了大量重定位符号。这是给我带来麻烦的一个:

$readelf --relocs libnewmat11.a | grep ZTIN6NEWMAT17Sing
000000001d20 013c00000002 R_X86_64_PC32 0000000000000000 _ZTIN6NEWMAT17Singular - 4
Relocation section '.rela.data.rel.ro._ZTIN6NEWMAT17SingularExceptionE' at offset 0x21280 contains 3 entries:
000000005b0b 013c00000001 R_X86_64_64 0000000000000000 _ZTIN6NEWMAT17Singular + 0
0000000009fc 008d00000002 R_X86_64_PC32 0000000000000000 _ZTIN6NEWMAT17Singular - 4
Relocation section '.rela.data.rel.ro._ZTIN6NEWMAT17SingularExceptionE' at offset 0x20b38 contains 3 entries:
000000008280 008d00000001 R_X86_64_64 0000000000000000 _ZTIN6NEWMAT17Singular + 0
...

目前一切正常,但当我运行 python3 setup.py build 时出现此错误:

running build
running build_py
running build_ext
building 'mytest' extension
x86_64-linux-gnu-gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O2 -Wall -g -fstack-protector-strong -Wformat -Werror=format-security -g -fwrapv -O2 -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fPIC -I../.. -I../../mhfe -I/usr/include/python3.8 -I/usr/local/include/newmat11/include -c src/my_python_binding.cpp -o build/temp.linux-x86_64-3.8/src/my_python_binding.o -std=c++11 -DPYBIND11_PYTHON_VERSION=3.8
...
x86_64-linux-gnu-g++ -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-Bsymbolic-functions -Wl,-z,relro -g -fwrapv -O2 -Wl,-Bsymbolic-functions -Wl,-z,relro -g -fwrapv -O2 -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 build/temp.linux-x86_64-3.8/src/my_python_binding.o -L/usr/lib64python3.8 -o build/lib.linux-x86_64-3.8/mytest.cpython-38-x86_64-linux-gnu.so -fPIC -Wl,--whole-archive /usr/local/lib/newmat11/lib/libnewmat11.a -Wl,--no-whole-archive
/usr/bin/ld: /usr/local/lib/newmat11/lib/libnewmat11.a(newmat8.o): relocation R_X86_64_PC32 against symbol `_ZTIN6NEWMAT17SingularExceptionE' can not be used when making a shared object; recompile with -fPIC

如上所述,我正在使用 -fPIC 构建静态库,我可以在 .a 文件中看到重定位符号。 R_X86_64_PC32 是否与共享对象不兼容?我想我需要让它生成 R_X86_64_PLT32 重定位符号,但我真的不确定。我可以在库中看到其他 R_X86_64_PLT32 重定位符号,但不是有问题的符号。另外,郑重声明,我不熟悉 setuptools 添加到构建中的所有标志,也许其中之一给我带来了麻烦?

感谢所有帮助。

最佳答案

我找到了解决方案。

尽管我的构建对依赖库使用了 -fPIC,但我注意到我没有在 configure 中为 automake 使用 --with-pic。我添加它只是为了看看有什么不同。

readelf --relocs 现在显示:

000000001a90  01390000002a R_X86_64_REX_GOTP 0000000000000000 _ZTIN6NEWMAT17Singular - 4
Relocation section '.rela.data.rel.ro._ZTIN6NEWMAT17SingularExceptionE' at offset 0x20f30 contains 3 entries:
000000005a54 013900000001 R_X86_64_64 0000000000000000 _ZTIN6NEWMAT17Singular + 0
0000000009fc 008c0000002a R_X86_64_REX_GOTP 0000000000000000 _ZTIN6NEWMAT17Singular - 4
Relocation section '.rela.data.rel.ro._ZTIN6NEWMAT17SingularExceptionE' at offset 0x20830 contains 3 entries:
0000000082b6 008c00000001 R_X86_64_64 0000000000000000 _ZTIN6NEWMAT17Singular + 0

这有 R_X86_64_REX_GOTP 重定位符号类型。我还发现我的链接器错误已得到解决。

添加 --with-pic 后我看不出编译标志有什么不同,所以在编译器级别,我不确定有什么不同。无论如何,希望这对遇到类似问题的人有所帮助。

关于g++ - 当 fPIC 已被使用时,在创建共享对象时不能使用针对符号的重定位 R_X86_64_PC32,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64162542/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com