tha*_*yne 4 g++ setuptools linker-errors fpic pybind11
我看过很多关于解决此类链接器错误的帖子,在大多数情况下,人们只是忘记使用 进行编译-fPIC,有时人们在使用内联函数时遇到麻烦,等等,但这里的情况并非如此。我正在尝试使用Pybind11包装 python 的 c++ 库。在此过程中,我想将一些静态库(其中之一是newmat11)链接到 .so 文件中。
我使用 automake 系统构建 newmat11 库-fPIC(这是一些输出......)
...
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
Run Code Online (Sandbox Code Playgroud)
事实上,当我运行时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
...
Run Code Online (Sandbox Code Playgroud)
到目前为止一切似乎都正常,但是当我运行时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
Run Code Online (Sandbox Code Playgroud)
如上所述,我正在构建静态库,-fPIC并且可以在.a文件中看到重定位符号。是否有什么东西R_X86_64_PC32使它与共享对象不兼容?我想我需要让它生成R_X86_64_PLT32重定位符号,但我真的不确定。我可以R_X86_64_PLT32在库中看到其他重定位符号,但看不到相关符号。另外,郑重声明,我并不熟悉 setuptools 添加到构建中的所有标志,也许其中之一给我带来了麻烦?
感谢所有帮助。
我找到了解决方案。
虽然我的构建使用 -fPIC 作为依赖库,但我注意到我没有--with-pic在我的configurefor automake 中使用。我添加它只是为了看看有什么区别。
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
Run Code Online (Sandbox Code Playgroud)
它具有R_X86_64_REX_GOTP重定位符号类型。我还发现我的链接器错误已得到解决。
添加后我看不到编译标志有任何区别--with-pic,因此在编译器级别,我不确定有什么区别。无论如何,希望这能帮助遇到类似问题的人。
| 归档时间: |
|
| 查看次数: |
4004 次 |
| 最近记录: |