[C++-sig] Re: Embedding example anyone?

Dave Hawkes daveh at cadlink.com
Thu Jul 11 03:31:56 CEST 2002


"David Abrahams" <david.abrahams at rcn.com> wrote in message
news:103501c22866$60df3010$6601a8c0 at boostconsulting.com...
> Sorry I haven't responded before -- your message got lost in the shuffle.
>
> Yes, people have done this, but unfortunately we don't have an example in
> the distribution. I've heard it said that it's just like extending Python
> with Boost.Python -- you just need to call your module's init() function
> explicitly to load the module. Maybe someone else on this list can help
you
> out. I know Dave Hawkes is planning to contribute some embedding examples
> for Boost.Python v2.
>
Unfortunately the example I have created so far, although functional, is for
a version of Boost.Python that's not currently in CVS :(
However I can say that embedding using V2 should be particularly
straightforward in most cases.

The best way of tying python and your own code together to make your program
a built-in module using something like this in c++:

PyImport_AppendInittab("MyProgram", initMyProgram);
Py_Initialize();
...

where initMyProgram is your init function.

Then in python:

import MyProgram


MyProgram will then behave as a built-in module. Of course there is nothing
to prevent you having multiple init functions in the same program and
initialising each with PyImport_AppendInittab. That way you can have
multiple modules for different aspects of the program.


It is also possible to create c++ classes which have some member functions
overloaded in python, which my example should also demonstrate when
complete.


Dave Hawkes










More information about the Cplusplus-sig mailing list