【问题标题】:Python / C++ binding, how to link agains static c++ library (portaudio) with distutils?Python / C++ 绑定,如何将静态 C++ 库(portaudio)与 distutils 链接?
【发布时间】:2016-08-21 00:04:28
【问题描述】:

我正在尝试将“c++ portaudio 库”与我的“C++ 演示模块”静态链接,这是一个 python 可调用库(模块)。

我正在使用 distutils 执行此操作,为了执行静态链接,我已将 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”编译会导致以下链接器错误:

/usr/bin/ld: ./clib-3rd-portaudio/libportaudio.a(pa_front.o): relocation R_X86_64_32 against `.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 标志不是根本原因。我可能遗漏了一些微不足道的东西,但我在这里有点迷路,希望有人能提供帮助。

【问题讨论】:

    标签: python c++ distutils


    【解决方案1】:

    正如错误消息所说,您应该使用 -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?Position Independent Code (PIC) in shared libraries

    【讨论】:

    • 谢谢! configure -h 显示所有可用选项,最终我要做的是configure CFLAGS=-fPIC
    猜你喜欢
    • 2013-09-03
    • 2012-04-17
    • 1970-01-01
    • 1970-01-01
    • 2011-11-14
    • 1970-01-01
    • 2011-02-20
    • 1970-01-01
    • 2022-12-23
    相关资源
    最近更新 更多