链接python扩展模块

问题描述

我有两个扩展模块,分别命名为foobarbar由C ++函数和围绕它的简单包装组成

#include <pybind11/pybind11.h>

int add_numbers(int a,int b)
{
    return a + b;
}

int bar_add_numbers(int  a,int b)
{
    return add_numbers(a,b);
}

PYBIND11_MODULE(_bar,m)
{
    m.def("add_numbers",&bar_add_numbers);
}

foo具有相同的包装,但没有定义。

#include <pybind11/pybind11.h>

int add_numbers(int,int);

int foo_add_numbers(int a,int  b)
{
    return add_numbers(a,b);
}

PYBIND11_MODULE(_foo,&foo_add_numbers);
}

使用setuptools构建这些文件之后

import os
from setuptools import setup,Extension
import pybind11

os.environ['CC'] = 'clang++'
os.environ['CXX'] = 'clang++'

libbar = Extension('foo._bar',sources = ['bar.cpp'],extra_compile_args=['-std=c++14'],include_dirs = [pybind11.get_include()]
)

libfoo = Extension('foo._foo',sources = ['foo.cpp'],include_dirs = [pybind11.get_include()]
)


setup(name='foo',version='1.0.0',ext_modules = [libbar,libfoo]
)

我的期望(希望)是,假设foo._foo是在 foo._bar之后导入的,则add_numbers应该从任一包装程序中正确解析。在OSX上似乎确实如此。

>>> from foo import _bar
>>> from foo import _foo
>>>  _foo.add_numbers(40,2)
42

但是,在Ubuntu盒子上,失败并显示undefined symbol: _Z11add_numbersii。就在我以为我可能几乎掌握动态链接的工作知识。

要在Linux(或更具体地,基于debian的发行版)上需要做些什么?

编辑:这两个.so文件nm输出

$ nm _bar.cpython-37m-x86_64-linux-gnu.so | grep "add_numbers"
0000000000004a50 T _Z11add_numbersii
0000000000004a60 T _Z15bar_add_numbersii

$ nm _foo.cpython-37m-x86_64-linux-gnu.so | grep "add_numbers"
                 U _Z11add_numbersii
00000000000048c0 T _Z15foo_add_numbersii

然后是编译器标志

clang++ -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,relro -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)