用 pybind11 包装 C++ 抽象 class 时出错
Error by wrapping C++ abstract class with pybind11
我试图做一个简单的例子来用 pybind 包装一个抽象的 C++ class。代码是:
#include <pybind11/pybind11.h>
#include <ostream>
#include <iostream>
namespace py = pybind11;
using namespace std;
class Base
{
public:
virtual void test() = 0;
};
class Derived: public Base
{
public:
void test() {cout << "Test";}
};
PYBIND11_MODULE(example,m) {
py::class_<Base, Derived>(m, "Base")
.def(py::init<>())
.def("test", &Derived::test);
}
而我 运行 下面的命令
c++ -O3 -Wall -shared -std=c++11 -fPIC `python3 -m pybind11 --includes` abstract_test.cpp -o example`python3-config --extension-suffix`\n
我收到错误:
In file included from abstrakt_test.cpp:1:
/home/anaconda3/envs/pybind/lib/python3.8/site-packages/pybind11/include/pybind11/pybind11.h: In instantiation of ‘Return (Derived::* pybind11::method_adaptor(Return (Class::*)(Args ...)))(Args ...) [with Derived = Base; Return = void; Class = Derived; Args = {}]’:
/home/anaconda3/envs/pybind/lib/python3.8/site-packages/pybind11/include/pybind11/pybind11.h:1118:45: required from ‘pybind11::class_<type_, options>& pybind11::class_<type_, options>::def(const char*, Func&&, const Extra& ...) [with Func = void (Derived::*)(); Extra = {}; type_ = Base; options = {Derived}]’
abstrakt_test.cpp:23:36: required from here
/home/anaconda3/envs/pybind/lib/python3.8/site-packages/pybind11/include/pybind11/pybind11.h:1032:19: error: static assertion failed: Cannot bind an inaccessible base class method; use a lambda definition instead
static_assert(detail::is_accessible_base_of<Class, Derived>::value,
^~~~~~
/home/anaconda3/envs/pybind/lib/python3.8/site-packages/pybind11/include/pybind11/pybind11.h:1034:12: error: cannot convert ‘void (Derived::*)()’ to ‘void (Base::*)()’ in return
return pmf;
^~~
您还需要“换行”Base
。否则您将在导入时遇到以下异常:
ImportError: generic_type: type "Derived" referenced unknown base type "Base"
此外,Derived
的换行顺序错误:
py::class_<Derived, Base>(m, "Derived")
完整示例:
PYBIND11_MODULE(example,m) {
py::class_<Base>(m, "Base");
py::class_<Derived, Base>(m, "Derived")
.def(py::init<>())
.def("test", &Derived::test);
}
我试图做一个简单的例子来用 pybind 包装一个抽象的 C++ class。代码是:
#include <pybind11/pybind11.h>
#include <ostream>
#include <iostream>
namespace py = pybind11;
using namespace std;
class Base
{
public:
virtual void test() = 0;
};
class Derived: public Base
{
public:
void test() {cout << "Test";}
};
PYBIND11_MODULE(example,m) {
py::class_<Base, Derived>(m, "Base")
.def(py::init<>())
.def("test", &Derived::test);
}
而我 运行 下面的命令
c++ -O3 -Wall -shared -std=c++11 -fPIC `python3 -m pybind11 --includes` abstract_test.cpp -o example`python3-config --extension-suffix`\n
我收到错误:
In file included from abstrakt_test.cpp:1:
/home/anaconda3/envs/pybind/lib/python3.8/site-packages/pybind11/include/pybind11/pybind11.h: In instantiation of ‘Return (Derived::* pybind11::method_adaptor(Return (Class::*)(Args ...)))(Args ...) [with Derived = Base; Return = void; Class = Derived; Args = {}]’:
/home/anaconda3/envs/pybind/lib/python3.8/site-packages/pybind11/include/pybind11/pybind11.h:1118:45: required from ‘pybind11::class_<type_, options>& pybind11::class_<type_, options>::def(const char*, Func&&, const Extra& ...) [with Func = void (Derived::*)(); Extra = {}; type_ = Base; options = {Derived}]’
abstrakt_test.cpp:23:36: required from here
/home/anaconda3/envs/pybind/lib/python3.8/site-packages/pybind11/include/pybind11/pybind11.h:1032:19: error: static assertion failed: Cannot bind an inaccessible base class method; use a lambda definition instead
static_assert(detail::is_accessible_base_of<Class, Derived>::value,
^~~~~~
/home/anaconda3/envs/pybind/lib/python3.8/site-packages/pybind11/include/pybind11/pybind11.h:1034:12: error: cannot convert ‘void (Derived::*)()’ to ‘void (Base::*)()’ in return
return pmf;
^~~
您还需要“换行”Base
。否则您将在导入时遇到以下异常:
ImportError: generic_type: type "Derived" referenced unknown base type "Base"
此外,Derived
的换行顺序错误:
py::class_<Derived, Base>(m, "Derived")
完整示例:
PYBIND11_MODULE(example,m) {
py::class_<Base>(m, "Base");
py::class_<Derived, Base>(m, "Derived")
.def(py::init<>())
.def("test", &Derived::test);
}