简体   繁体   English

使用转换器提升 Python 问题 - static 链接

[英]Boost Python issue with converters - static linking

I have a question regarding the below code.我对以下代码有疑问。

It's an example how to pass a custom class via shared_ptr to embedded python code and it works when boost is dynamically linked.这是一个如何通过 shared_ptr 将自定义 class 传递给嵌入式 python 代码的示例,并且它在动态链接提升时有效。

Unfortunately the same code with statically linked boost doesn't work with the following error message:不幸的是,具有静态链接提升的相同代码不适用于以下错误消息:

"No to_python (by-value) converter found for C++ type: class boost::shared_ptr". “没有找到 C++ 类型的 to_python(按值)转换器:class boost::shared_ptr”。

I don't understand why a different linking can affect type recognition of a registered converter.我不明白为什么不同的链接会影响已注册转换器的类型识别。 What am I missing?我错过了什么?

Can anybody help me out?有人可以帮帮我吗?

Thanks,谢谢,

Dominik多米尼克

Example from here .示例来自这里

#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>
#include <boost/python.hpp>
#include <string>
#include <iostream>

namespace bp = boost::python;

struct Foo{
    Foo(){}
    Foo(std::string const& s) : m_string(s){}
    void doSomething() {
        std::cout << "Foo:" << m_string << std::endl;
    }
    std::string m_string;
};

typedef boost::shared_ptr<Foo> foo_ptr;

BOOST_PYTHON_MODULE(hello)
{
    bp::class_<Foo, foo_ptr>("Foo")
        .def("doSomething", &Foo::doSomething)
    ;
};

int main(int argc, char **argv)
{
    Py_Initialize();
    try {
        PyRun_SimpleString(
            "a_foo = None\n"
            "\n"
            "def setup(a_foo_from_cxx):\n"
            "    print 'setup called with', a_foo_from_cxx\n"
            "    global a_foo\n"
            "    a_foo = a_foo_from_cxx\n"
            "\n"
            "def run():\n"
            "    a_foo.doSomething()\n"
            "\n"
            "print 'main module loaded'\n"
        );

        foo_ptr a_cxx_foo = boost::make_shared<Foo>("c++");

        inithello();
        bp::object main = bp::object(bp::handle<>(bp::borrowed(
            PyImport_AddModule("__main__")
        )));

        // pass the reference to a_cxx_foo into python:
        bp::object setup_func = main.attr("setup");
        setup_func(a_cxx_foo);

        // now run the python 'main' function
        bp::object run_func = main.attr("run");
        run_func();
    }
    catch (bp::error_already_set) {
        PyErr_Print();
    }

    Py_Finalize();

    return 0;
}

I far as I understand the documentation about Boost Python linkage, it seems that the conversion registry used for automatic conversion of Python object into C++ object is not available when statically linked. I far as I understand the documentation about Boost Python linkage, it seems that the conversion registry used for automatic conversion of Python object into C++ object is not available when statically linked. I'm facing the same issue and that's a pity it is actually the case.我面临同样的问题,很遗憾实际上是这样。 I would have imagined at least the required converter to be bundle but I'm afraid it is not the case for some reason.我会想象至少需要捆绑所需的转换器,但恐怕由于某种原因并非如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM