
Hi, 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. 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<class Foo>". 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 https://stackoverflow.com/questions/8225934/exposing-a-c-class-instance-to-a-python-embedded-interpreter?noredirect=1&lq=1 |#include<boost/shared_ptr.hpp>#include<boost/make_shared.hpp>#include<boost/python.hpp>#include<string>#include<iostream>namespacebp =boost::python;structFoo{Foo(){}Foo(std::stringconst&s):m_string(s){}voiddoSomething(){std::cout <<"Foo:"<<m_string <<std::endl;}std::stringm_string;};typedefboost::shared_ptr<Foo>foo_ptr;BOOST_PYTHON_MODULE(hello){bp::class_<Foo,foo_ptr>("Foo").def("doSomething",&Foo::doSomething);};| |#include<boost/shared_ptr.hpp>#include<boost/make_shared.hpp>#include<boost/python.hpp>#include<string>#include<iostream>namespacebp =boost::python;structFoo{Foo(){}Foo(std::stringconst&s):m_string(s){}voiddoSomething(){std::cout <<"Foo:"<<m_string <<std::endl;}std::stringm_string;};typedefboost::shared_ptr<Foo>foo_ptr;BOOST_PYTHON_MODULE(hello){bp::class_<Foo,foo_ptr>("Foo").def("doSomething",&Foo::doSomething);};intmain(intargc,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::objectmain =bp::object(bp::handle<>(bp::borrowed(PyImport_AddModule("__main__"))));// pass the reference to a_cxx_foo into python:bp::objectsetup_func =main.attr("setup");setup_func(a_cxx_foo);// now run the python 'main' functionbp::objectrun_func =main.attr("run");run_func();}catch(bp::error_already_set){PyErr_Print();}Py_Finalize();return0;}|
participants (1)
-
Dominik Cirmirakis