【发布时间】:2012-05-25 23:39:19
【问题描述】:
我正在尝试编写一个 c++ 扩展来替换以下 python 函数以加速我的程序
python函数如下所示
def calc_dist(fea1, fea2):
#fea1 and fea2 are two lists with same length
...
我使用 c++ 和 boost python 编写了如下函数:
#include <vector>
#include <boost/python.hpp>
double calc_dist(vector<double>& fea1, vector<double>& fea2)
{
int len = fea1.size();
double s=0;
for(int i=0; i<len;i++){
double p=fea1[i];
double q=fea2[i];
...//calculating..
}
return s;
}
BOOST_PYTHON_MODULE(calc_dist)
{
using namespace boost::python;
def("calc_dist",calc_dist);
}
并将上面的cpp代码编译成.so文件
g++ calc_dist.cpp -shared -fPIC -o calc_dist.so -I /usr/include/python2.6 -lboost_python
并尝试在python程序中使用.so,导入工作正常,表明模块可以成功导入。
但是,每当我将两个列表传递给函数的参数时,python 都会给出类似的错误
ArgumentError: Python argument types in
calc_dist.calc_dist(list, list)
did not match C++ signature:
calc_dist.calc_dist(std::vector<float, std::allocator<float> >,
std::vector<float, std::allocator<float> >)
谁能帮我解决这个问题?即使用 boost 将 python 列表传递给 c++ 扩展?
非常感谢!
【问题讨论】:
-
使用 boost::python::list 见this question
标签: c++ python list boost vector