gpt4 book ai didi

python - 使用 python 扩展时链接到其他库(例如 boost)

转载 作者:行者123 更新时间:2023-11-28 06:00:24 24 4
gpt4 key购买 nike

我正在使用以下 setup.py - 文件从 python 调用 C++ 代码。 (在使用 swig 生成包装器之后)。

#!/usr/bin/env python

from distutils.core import setup, Extension

example_module = Extension(
'_example',
sources=['example_wrap.cxx', 'example.cpp'],
swig_opts=['-c++', '-py3'],
extra_compile_args =['-lboost_math ','-lboost_system ','-Wno-unused-local-typedef'],
include_dirs = ['/usr/local/include'],
library_dirs = ['/usr/local/include'],
)

setup (name = 'example',
version = '0.1',
ext_modules = [example_module],
py_modules = ["example"],
)

但是,当我尝试包含其他库(在本例中为 boost)时,出现以下错误。

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/pathToExample/example.py", line 28, in <module>
_example = swig_import_helper()
File "/pathToExample/example.py", line 24, in swig_import_helper
_mod = imp.load_module('_example', fp, pathname, description)
File "/some_path/lib/python3.4/imp.py", line 243, in load_module
return load_dynamic(name, filename, file)
ImportError: dlopen(/pathToExample/_example.so, 2): Symbol not found: __ZN5boost4math12sph_hankel_1IiiEESt7complexINS0_6detail13bessel_traitsIT_T0_NS0_8policies6policyINS7_14default_policyES9_S9_S9_S9_S9_S9_S9_S9_S9_S9_S9_S9_EEE11result_typeEES5_S6_
Referenced from: /pathToExample/_example.so
Expected in: dynamic lookup

这可能是链接问题?我必须如何更改 setup.py?

编辑 1:将 -lboost_system 添加到 extra_compile_args

Edit2:这是我的 C++ 代码:

#include <vector>
#include <complex>
#include </usr/local/include/boost/math/special_functions/bessel.hpp>

using namespace std;

vector<float> Test(int n){
vector<float> a(2);
complex<double> b = boost::math::sph_hankel_1(0, 1);
a[1] = real(b);
return a;
}

编辑 3:这是我用来生成包装器的 swig 代码

/* File: example.i */
%module example
%include "std_vector.i"

%{
#define SWIG_FILE_WITH_INIT
#include "example.h"
%}

namespace std
{
%template(FloatVector) vector<float>;
}

std::vector< float > Test(int n);

编辑 4:为了完整起见,还有我的头文件:

/* File: example.h*/

#include <vector>
#include <complex>
#include </usr/local/include/boost/math/special_functions/bessel.hpp>

std::vector<float> Test(int n);

最佳答案

是的,这是错误指示的链接问题:

Symbol not found: __ZN5boost4math12sph_hankel_ ...

您缺少 Boost Math。

您需要通过将 -lboost_math 添加到选项 extra_compile_args 来更改您的 setup.py 文件。

关于python - 使用 python 扩展时链接到其他库(例如 boost),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33415669/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com