将LibRaw链接到共享对象时出错

问题描述 投票:0回答:1

所以,我实际上试图建立的共享对象包含pybind11生成的python-includable-module。我知道它在CLion中没有语法错误,但是当我尝试对其进行编译时,它出现以下错误:

/usr/bin/ld: //usr/local/lib/libraw.a(utils_libraw.o): relocation R_X86_64_PC32 against symbol `stderr@@GLIBC_2.2.5' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: final link failed: Bad value
collect2: error: ld returned 1 exit status
CMakeFiles/cr3_to_python.dir/build.make:98: recipe for target 'cr3_to_python.cpython-36m-x86_64-linux-gnu.so' failed
make[3]: *** [cr3_to_python.cpython-36m-x86_64-linux-gnu.so] Error 1
CMakeFiles/Makefile2:77: recipe for target 'CMakeFiles/cr3_to_python.dir/all' failed
make[2]: *** [CMakeFiles/cr3_to_python.dir/all] Error 2
CMakeFiles/Makefile2:84: recipe for target 'CMakeFiles/cr3_to_python.dir/rule' failed
make[1]: *** [CMakeFiles/cr3_to_python.dir/rule] Error 2
Makefile:118: recipe for target 'cr3_to_python' failed
make: *** [cr3_to_python] Error 2

使用C ++进行编译时,我得到非常相似的内容:

$ c++ -O3 -Wall -shared -std=c++11 -fPIC -I/usr/include/python3.6m/ -I/usr/local/include/opencv4/ $(python3 -m pybind11 --includes) main.cpp -o cr3_to_python$(python3-config --extension-suffix) -lraw
/usr/bin/ld: //usr/local/lib/libraw.a(utils_libraw.o): relocation R_X86_64_PC32 against symbol `stderr@@GLIBC_2.2.5' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: final link failed: Bad value
collect2: error: ld returned 1 exit status

最让我烦恼的是实际错误:

/usr/bin/ld: //usr/local/lib/libraw.a(utils_libraw.o): relocation R_X86_64_PC32 against symbol `stderr@@GLIBC_2.2.5' can not be used when making a shared object; recompile with -fPIC

因为我没有在代码中包含与stderr相关的任何内容。

main.cpp

CMakeLists.txt

c++ shared-libraries linker-errors pybind11 libraw
1个回答
0
投票

[在Linux和Macos(可能还有其他Unixy操作系统)上编译共享库时,该库中使用的所有代码都需要以“位置无关模式”进行编译,这是因为编译器不知道将在何处加载可执行文件。代码,因此必须生成可以从任何地址运行的代码。

要生成PIC代码,您需要将-fPIC标志传递给GCC。>

请注意,您要编译到共享库中的所有代码都必须以PIC模式编译,这包括静态库中的代码。根据您的情况,您需要在启用libraw.a的情况下重新编译-fPIC

与位置无关的代码可能会稍慢一些,但差异很小,因此即使直接在可执行文件中使用-fPIC,您也可以只编译所有代码。 Apple建议包括可执行文件在内的所有代码都与位置无关。有关性能的更多详细信息,请参见How much overhead can the -fPIC flag add?

© www.soinside.com 2019 - 2024. All rights reserved.