无论如何,有没有在标头中使用 boost python 包装器?

发布于 2024-10-23 13:04:08 字数 422 浏览 8 评论 0原文

是否有在头文件中使用 BOOST_PYTHON_MODULE ?例如,我希望能够

BOOST_PYTHON_MODULE(Status_Effect)
{
    boost::python::class_<StatusEffect>("StatusEffect")
        .def("GetPriority", &StatusEffect::GetPriority)
        .def("GetDescription", &StatusEffect::GetDescription)
        .def("GetName", &StatusEffect::GetName);
}

在头文件中声明此模块。然而,每当我尝试时,它都会抱怨多个定义。有谁知道在头文件中进行包装的方法吗?

谢谢

Is there anyway to use BOOST_PYTHON_MODULE in a header file? For instance, I'd like to be able to declare this Module

BOOST_PYTHON_MODULE(Status_Effect)
{
    boost::python::class_<StatusEffect>("StatusEffect")
        .def("GetPriority", &StatusEffect::GetPriority)
        .def("GetDescription", &StatusEffect::GetDescription)
        .def("GetName", &StatusEffect::GetName);
}

in a header file. Whenever I try however, it complains about multiple definitions. Does anyone know of a way to do the wrapping in a header file?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

染柒℉ 2024-10-30 13:04:08

解决方法如下:括号内的内容只是普通的 C++ 代码。所以你可以将该部分移动到内联函数中。

例如,您可以将其放入标头中:

inline void DeclareStatusEffect()
{
  boost::python::class_<StatusEffect>("StatusEffect")
    .def("GetPriority", &StatusEffect::GetPriority)
    .def("GetDescription", &StatusEffect::GetDescription)
    .def("GetName", &StatusEffect::GetName);

}

将其放入源文件中:

BOOST_PYTHON_MODULE(Status_Effect)
{
  DeclareStatusEffect();
}

您还可以查看 BOOST_PYTHON_MODULE 宏的作用,也许有一种方法可以将更多内容放入标头中,但是对于未来版本的 Boost.Python 来说,这可能不安全,即使你让它可以工作。

Here's a workaround: What's inside of the parentheses is just ordinary C++ code. So you could move that part into an inline function.

For example, you could put this into the header:

inline void DeclareStatusEffect()
{
  boost::python::class_<StatusEffect>("StatusEffect")
    .def("GetPriority", &StatusEffect::GetPriority)
    .def("GetDescription", &StatusEffect::GetDescription)
    .def("GetName", &StatusEffect::GetName);

}

And this into your source file:

BOOST_PYTHON_MODULE(Status_Effect)
{
  DeclareStatusEffect();
}

You can also look at what the BOOST_PYTHON_MODULE macro does, and maybe there's a way to put even more into the header, but that's probably not safe to do with future versions of Boost.Python, even if you get it to work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文