Pybind11 类型错误
Pybind11 Type Error
我最近一直在尝试用 C++ 编写一个函数,将双精度向量转换为字符串向量。我想从 python 解释器 运行 这个,所以我使用 Pybind11 来连接 C++ 和 Python。这是我目前所拥有的,
#include <pybind11/pybind11.h>
#include <boost/lexical_cast.hpp>
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
std::string castToString(double v) {
std::string str = boost::lexical_cast<std::string>(v);
return str;
}
std::vector<std::vector<std::string> > num2StringVector(std::vector<std::vector<double> >& inputVector) {
//using namespace boost::multiprecision;
std::vector<std::vector<std::string> > outputVector;
std::transform(inputVector.begin(), inputVector.end(), std::back_inserter(outputVector), [](const std::vector<double> &iv) {
std::vector<std::string> dv;
std::transform(iv.begin(), iv.end(), std::back_inserter(dv), &castToString);
return dv;
});
return outputVector;
}
namespace py = pybind11;
PYBIND11_PLUGIN(num2String) {
py::module m("num2String", "pybind11 example plugin");
m.def("num2StringVector", &num2StringVector, "this converts a vector of doubles to a vector of strings.");
m.def("castToString", &castToString, "This function casts a double to a string using Boost.");
return m.ptr();
}
现在编译成共享库,使用命令行中的以下命令:
c++ -O3 -shared -std=gnu++11 -I ../include `python-config --cflags --ldflags --libs` num2StringVectorPyBind.cpp -o num2String.so -fPIC -lquadmath
../include 是 pybind11 include 所在的位置。编译后启动 python 并使用,
import num2String
values = [[10, 20], [30, 40]]
num2String.num2StringVector(values)
我收到以下错误,"Incompatible function arguments. The following argument types are supported"。然后它给了我一个可能类型的列表,这很奇怪,因为我只是想使用一个向量作为我的函数参数,根据 pybind11 的文档,这是一个支持的数据类型:
http://pybind11.readthedocs.io/en/latest/basics.html#supported-data-types
是否我有一个向量的向量(一个二维向量)并且不受支持?
您只需多加一行代码就可以完成这项工作:
#include <pybind11/stl.h>
The following basic data types are supported out of the box (some may require an additional extension header to be included).
在包括 pybind11/stl.h
之前:
>>> import num2String
>>> num2String.num2StringVector([[1, 2], [3, 4, 5]])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: Incompatible function arguments. The following argument types are supported:
1. (std::vector<std::vector<double, std::allocator<double> >, std::allocator<std::vector<double, std::allocator<double> > > >) -> std::vector<std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > >, std::allocator<std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > >
包含 pybind11/stl.h
并重新编译共享库后:
>>> import num2String
>>> num2String.num2StringVector([[1, 2], [3, 4, 5]])
[['1', '2'], ['3', '4', '5']]
我最近一直在尝试用 C++ 编写一个函数,将双精度向量转换为字符串向量。我想从 python 解释器 运行 这个,所以我使用 Pybind11 来连接 C++ 和 Python。这是我目前所拥有的,
#include <pybind11/pybind11.h>
#include <boost/lexical_cast.hpp>
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
std::string castToString(double v) {
std::string str = boost::lexical_cast<std::string>(v);
return str;
}
std::vector<std::vector<std::string> > num2StringVector(std::vector<std::vector<double> >& inputVector) {
//using namespace boost::multiprecision;
std::vector<std::vector<std::string> > outputVector;
std::transform(inputVector.begin(), inputVector.end(), std::back_inserter(outputVector), [](const std::vector<double> &iv) {
std::vector<std::string> dv;
std::transform(iv.begin(), iv.end(), std::back_inserter(dv), &castToString);
return dv;
});
return outputVector;
}
namespace py = pybind11;
PYBIND11_PLUGIN(num2String) {
py::module m("num2String", "pybind11 example plugin");
m.def("num2StringVector", &num2StringVector, "this converts a vector of doubles to a vector of strings.");
m.def("castToString", &castToString, "This function casts a double to a string using Boost.");
return m.ptr();
}
现在编译成共享库,使用命令行中的以下命令:
c++ -O3 -shared -std=gnu++11 -I ../include `python-config --cflags --ldflags --libs` num2StringVectorPyBind.cpp -o num2String.so -fPIC -lquadmath
../include 是 pybind11 include 所在的位置。编译后启动 python 并使用,
import num2String
values = [[10, 20], [30, 40]]
num2String.num2StringVector(values)
我收到以下错误,"Incompatible function arguments. The following argument types are supported"。然后它给了我一个可能类型的列表,这很奇怪,因为我只是想使用一个向量作为我的函数参数,根据 pybind11 的文档,这是一个支持的数据类型: http://pybind11.readthedocs.io/en/latest/basics.html#supported-data-types
是否我有一个向量的向量(一个二维向量)并且不受支持?
您只需多加一行代码就可以完成这项工作:
#include <pybind11/stl.h>
The following basic data types are supported out of the box (some may require an additional extension header to be included).
在包括 pybind11/stl.h
之前:
>>> import num2String
>>> num2String.num2StringVector([[1, 2], [3, 4, 5]])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: Incompatible function arguments. The following argument types are supported:
1. (std::vector<std::vector<double, std::allocator<double> >, std::allocator<std::vector<double, std::allocator<double> > > >) -> std::vector<std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > >, std::allocator<std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > >
包含 pybind11/stl.h
并重新编译共享库后:
>>> import num2String
>>> num2String.num2StringVector([[1, 2], [3, 4, 5]])
[['1', '2'], ['3', '4', '5']]