Python / C++ 绑定,如何使用 distutils link agains static c++ library (portaudio)?
Python / C++ binding, how to link agains static c++ library (portaudio) with distutils?
我正在尝试针对我的 "C++ demo module" 静态 link "c++ portaudio library",这是一个 python 可调用库(模块)。
我正在使用 distutils 执行此操作,为了执行静态 linking,我已将 libportaudio 添加到 extra_objects 参数,如下所示:
module1 = Extension(
"demo",
sources=cppc,
# TODO remove os dependency
extra_compile_args=gccArgs,
# link against shared libraries
#libraries=[""]
# link against static libraries
extra_objects=["./clib-3rd-portaudio/libportaudio.a"]) # << I've added the static lib here
使用 "python setup.py build" 编译会导致以下 linker 错误:
/usr/bin/ld: ./clib-3rd-portaudio/libportaudio.a(pa_front.o): 重定位 R_X86_64_32 对 `.rodata.str1.8' 进行共享时不能使用目的;使用 -fPIC 重新编译
./clib-3rd-portaudio/libportaudio.a: 添加符号时出错:错误值
collect2:错误:ld 返回了 1 个退出状态
所以在这一点上我已经尝试了显而易见的,我已经将 -fPIC 标志添加到 gccArgs(注意上面的 extra_compile_args=gccArgs),如下所示:
gccArgs = [
"-Icsrc",
"-Icsrc/paExamples",
"-Icinc-3rd-portaudio",
"-Icinc-3rd-portaudio/common",
"-Icinc-3rd-portaudio/linux",
"-fPIC"] # << I've added the -fPIC flag here
但是这会导致完全相同的错误,所以我猜 -fPIC 标志不是根本原因。我可能遗漏了一些微不足道的东西,但我在这里有点迷路,希望有人能提供帮助。
如错误消息所述,您应该使用 -fPIC
参数重新编译外部库 libportaudio.a,而不是您自己的代码。这就是为什么将 -fPIC
添加到 extra_compile_args
.
没有帮助的原因
其他几个 posts 建议文件 libportaudio.a
不能用于构建共享库,可能是因为 portaudio
的默认构建设置不包括 -fPIC
.
要正确重新编译 portaudio
,请下载源代码并尝试使用 -shared
选项(或类似的东西)运行 ./configure
。如果找不到合适的选项,则修改 Makefile 并将 -fPIC
附加到额外的编译选项。您也可以手动编译每个目标文件并将它们打包成 libportaudio.a.
由于您的目标文件 (libdemo.so) 是一个 共享库 ,您必须确保其中包含的任何目标代码都是使用 -fPIC
选项编译的。要了解为什么需要此选项,请参阅:
What does -fPIC mean when building a shared library? and Position Independent Code (PIC) in shared libraries
我正在尝试针对我的 "C++ demo module" 静态 link "c++ portaudio library",这是一个 python 可调用库(模块)。
我正在使用 distutils 执行此操作,为了执行静态 linking,我已将 libportaudio 添加到 extra_objects 参数,如下所示:
module1 = Extension(
"demo",
sources=cppc,
# TODO remove os dependency
extra_compile_args=gccArgs,
# link against shared libraries
#libraries=[""]
# link against static libraries
extra_objects=["./clib-3rd-portaudio/libportaudio.a"]) # << I've added the static lib here
使用 "python setup.py build" 编译会导致以下 linker 错误:
/usr/bin/ld: ./clib-3rd-portaudio/libportaudio.a(pa_front.o): 重定位 R_X86_64_32 对 `.rodata.str1.8' 进行共享时不能使用目的;使用 -fPIC 重新编译 ./clib-3rd-portaudio/libportaudio.a: 添加符号时出错:错误值 collect2:错误:ld 返回了 1 个退出状态
所以在这一点上我已经尝试了显而易见的,我已经将 -fPIC 标志添加到 gccArgs(注意上面的 extra_compile_args=gccArgs),如下所示:
gccArgs = [
"-Icsrc",
"-Icsrc/paExamples",
"-Icinc-3rd-portaudio",
"-Icinc-3rd-portaudio/common",
"-Icinc-3rd-portaudio/linux",
"-fPIC"] # << I've added the -fPIC flag here
但是这会导致完全相同的错误,所以我猜 -fPIC 标志不是根本原因。我可能遗漏了一些微不足道的东西,但我在这里有点迷路,希望有人能提供帮助。
如错误消息所述,您应该使用 -fPIC
参数重新编译外部库 libportaudio.a,而不是您自己的代码。这就是为什么将 -fPIC
添加到 extra_compile_args
.
其他几个 posts 建议文件 libportaudio.a
不能用于构建共享库,可能是因为 portaudio
的默认构建设置不包括 -fPIC
.
要正确重新编译 portaudio
,请下载源代码并尝试使用 -shared
选项(或类似的东西)运行 ./configure
。如果找不到合适的选项,则修改 Makefile 并将 -fPIC
附加到额外的编译选项。您也可以手动编译每个目标文件并将它们打包成 libportaudio.a.
由于您的目标文件 (libdemo.so) 是一个 共享库 ,您必须确保其中包含的任何目标代码都是使用 -fPIC
选项编译的。要了解为什么需要此选项,请参阅:
What does -fPIC mean when building a shared library? and Position Independent Code (PIC) in shared libraries