我正在尝试使用 pybind 创建虚拟类的 python 绑定。我一直在关注 pybind11 文档,这就是我所拥有的
测试场.h
class AbstractTestField
{
public:
virtual ~AbstractTestField() = default;
virtual double test() const = 0;
};
class TestField : public AbstractTestField
{
public:
~TestField() = default;
double test() const final;
};
} // namespace VNCS
测试场.cpp
double VNCS::TestField::test() const
{
PYBIND11_OVERLOAD_PURE(double, /* Return type */
VNCS::TestField, /* Parent class */
test /* Name of function in C++ (must match Python name) */
);
}
绑定.cpp
#include <pybind11/pybind11.h>
#include "TestField.h"
namespace py = pybind11;
PYBIND11_MODULE(VNCSSofaPlugin, m)
{
m.doc() = "Test bindings";
m.attr("__version__") = "dev";
py::class_<AbstractTestField, TestField>(m, "TestField")
.def(py::init<>())
.def("test", &AbstractTestField::test);
}
我使用 CMake 作为我的构建系统,并创建了两个目标。一个用于 C++ 代码,一个用于绑定。
对于 C++ 我有这样的东西
set(SOURCES TestField.cpp)
set(PUBLIC_HEADERS TestField.h)
add_library(VNCSSofaPlugin SHARED ${SOURCES} ${HEADERS})
...
对于绑定我有这个
set(SOURCES bindings.cpp)
pybind11_add_module(VNCSSofaPluginBinding ${SOURCES})
target_link_libraries(VNCSSofaPluginBinding PUBLIC VNCSSofaPlugin)
这编译得很好,它会生成两个库:libVNCSSofaPlugin.so
和VNCSSofaPluginBinding.cpython-38-x86_64-linux-gnu.so
但是,当我尝试在 python 中导入绑定时,出现以下问题
Python 3.8.6 (default, Sep 30 2020, 04:00:38)
[GCC 10.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import VNCSSofaPluginBinding
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: /mnt/D/jjcasmar/projects/VNCSSofaPlugin/build/Debug/lib/VNCSSofaPluginBinding.cpython-38-x86_64-linux-gnu.so: undefined symbol: _ZTIN4VNCS9TestFieldE
我不明白为什么找不到构造函数。我究竟做错了什么?