使用python扩展时链接到其他库(例如boost)
Linking to other libraries (e.g. boost) when using python extensions
我使用以下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版:将-lbostrongystem添加到extra_compile_args
第二版:这是我的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
文件。
相关文章:
- 理解boost::asio-async_read在无需读取内容时的行为
- boost::进程间消息队列引发错误
- 如何运行位于boost/libs/python/example/tutorial目录中的hello.cpp和Jamfil
- cmake如何在fedora工作站中找到boost静态库包
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- 用STD = C 11或其他标准编译Boost,例如Ash C 14
- 如何使用 Boost V2 1.70 日志记录库在第一个索引(例如 logtrail01.txt)保留最新的日志文件名?
- 使用Boost随机基础设施,但如何轻松更换组件(例如底层生成器)
- 在 C++11 中是否有等效的 boost::interprocess::null_mutex(例如 std::null
- 如何声明Boost范围适配器(例如MAP_VALUES)
- Boost图库undirected_Graph:如何指定顶点类型(例如int)
- 是否可以在 boost::ireplace 中将特殊字符视为基本字符?(例如。 'ź'为"z")
- 结合了boost范围算法,例如boost::copy和boost::remove_if
- 使用python扩展时链接到其他库(例如boost)
- 使用Boost program_options指定级别(例如——verbose)
- 我如何从boost库(例如shared_pointer)中获取单个元素
- boost::regex与不区分大小写的UTF-8匹配(例如大写字母与小写字母)
- boost program_options是否支持关闭开关(例如——no-myswitch或-s=no)
- 什么是"visual studio 2008" unique_ptr替代品,没有 3 rd 库(例如 boost)
- boost::msm -一种获取字符串表示的方法(例如:getName)