[C++-sig] Py++/boost::python: How to correctly wrap libraries (DLLs) with dependencies on each other.

Roman Yakovenko roman.yakovenko at gmail.com
Wed Jan 20 09:22:30 CET 2010


On Mon, Jan 11, 2010 at 6:15 PM, mtn <mtn at xcm.ch> wrote:
>
> Hi all,
>
> I try to embed a Python interpreter into my project using
> Py++/boost::python.
>
> I have a problem wrapping different libraries for which i own the source and
> which I compile to DLL/*.so files . The libraries have dependencies on each
> other but do not necessarily have to be used together.
>
>
> An abstract example:
>
> The interpreter is in a library on which all other libraries depend and
> where they register themselves using init functions generated by
> py++/boost::python.
> Let's say I have a base library B.
> Additionally a library E extending library B.
>
> lib B:
> class __exported_to_python__ Base {
>  void __exported_to_pytthon__  foo(){...}
> }
>
> lib E -> depends B:
> class __exported_to_python__ Extended : public B
> {
>  void __exported_to_python__  boo(){...}
> }
>
> It makes sense to also use B alone, therefore I would like to build a Python
> wrapper for each library directly into the DLL.
>
> Now I register my init function generate by Py++/boost::python to the
> interpreter instance using `PyImport_AppendInittab`.
>
> The thing, that I cannot generate a proper inheritance relation between the
> class `Extended` and `Base` without telling Py++ to wrap both classes. But
> then I have wrapped `Base` twice. Once in B and once in E.
> And when boost::python tries to register some transfer functions based on
> the C++ type (RTTI) it fails, as it has already been registered (because
> init_B was called).
>
> If I simply delete the duplicated classes by hand from the wrapper of
> library `E` it works.
> But one has to ensure that first `B` is imported to Python (`import B`) and
> afterwards `E` (`import E`) otherwise boost::python does not have `Base`
> registered on which `Extended` depends.

You can import "B" from "E". You can do this by building "EImpl"
module and the "wrapping" it with package:
 E/
     __init__.py
     EImpl.[so|pyd|dll]

where __init__.py contains the following code:

import B
from EImpl import * #or something like that


>
> I hope I made myself clear enough now :-)
> So has anybody solved this problem before me?
> I did not find a source which would explain how to solve this issue with
> Py++ and boost::python.

Take a look on http://language-binding.net/pyplusplus/documentation/multi_module_development.html
. The document describes the situation you have.

If you need a good example take a look on Python-Ogre project. It uses
"multi module development" feature all the way.

HTH

-- 
Roman Yakovenko
C++ Python language binding
http://www.language-binding.net/


More information about the Cplusplus-sig mailing list