简体   繁体   English

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

[英]Linking to other libraries (e.g. boost) when using python extensions

I am calling C++ code from python using the following setup.py - file. 我正在使用以下setup.py-文件从python调用C ++代码。 (After generating a wrapper using swig). (使用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"],
)

However, when I try to include other libraries, in this case boost, I get the following error. 但是,当我尝试包括其他库(在这种情况下为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

Is it maybe a linking problem? 可能是链接问题吗? How do I have to change setup.py? 我该如何更改setup.py?

Edit1: Added -lboost_system to extra_compile_args 编辑1:在Extra_compile_args中添加了-lboost_system

Edit2: Here is my C++ code: 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;    
}

Edit 3: Here the swig code which I use to generate my wrapper 编辑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);

Edit 4: For completeness also my header file: 编辑4:为完整起见我的头文件:

/* File: example.h*/

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

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

Yes, it's a linking problem as indicated by error: 是的,这是一个链接问题,如错误所示:

Symbol not found: __ZN5boost4math12sph_hankel_ ...

You're missing Boost Math. 您缺少Boost Math。

You need to change your setup.py file by adding -lboost_math to option extra_compile_args . 您需要通过在选项extra_compile_args添加-lboost_math来更改setup.py文件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python中机器学习库(例如scikit)中的不对称正则化 - asymmetric regularization in machine learning libraries (e.g. scikit ) in python 用python进行窗口式写入,例如写入NetCDF - Windowed writes in python, e.g. to NetCDF 在 Python 的 for 循环中使用 range(len(x)) 时添加索引有什么影响? 例如范围(len(x [0]) - What is the effect of adding an index when using range(len(x)) in a for loop in Python? e.g. range(len(x[0]) 如何从 Python 中不包含任何图像文件扩展名(例如 png)的 URL 保存图像? - How to save an image from an URL which does not contain any image file extensions (e.g. png) in Python? 模糊正则表达式(例如 {e&lt;=2})在 Python 中的正确用法 - Fuzzy regex (e.g. {e<=2}) correct usage in Python 如何使用计算方法(例如使用Python)找到以下所需概率? - How to find the following desired probability using computational method (e.g. using Python)? 什么时候使用tensorflow的内部API(例如tf.python.ops。*)? - When to use tensorflow's internal API (e.g. tf.python.ops.*)? 在python中调用fortran代码时如何处理全局变量(例如,使用f2py)? - How to deal with global variables when calling fortran code in python (e.g. with f2py)? 当客户端在 NAT 之后如何接收数据,例如路由器 - Python - How to receive data when clients are behind NAT e.g. router - Python Python 'exifread' 模块在不查看图片时返回错误,例如 .mp3 - Python 'exifread' module returns error when going thourg not picture e.g. .mp3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM