From: David Abrahams (dave_at_[hidden])
Date: 2002-11-25 17:55:19
> I'm trying to embedd and extend a C++ application.
> I was not able to find any good examples of how to do this.
> From the small tidbits of information I found, plus browsing the
> Boost.Python sources, I tried the following approach (starting from
> the "getting_started2" example, with an added main() + other
> use bjam -omylog.txt -n -sTOOLS=msvc
> To get a log of what exactly goes on when bjam builds the examples. Looking
> at the log it seems we have to make som modifications:
> Compile step:
> remove -DBOOST_DYNAMIC_LIB
Has no effect.
> Link step:
> remove /DLL
> change /out: (file extension should be .exe instead of .pyd)
> remove /implib:* (no need for an import lib for an .exe)
> Using these modifications to the command lines we can compile and link an
You might try adding -DBOOST_PYTHON_STATIC_LIB if you insist on
linking Booost.Python directly into your application, but there's no
reason you can't just use the Boost.Python DLL for this job.
> It seems to work fine. I manually call the init function (whatever
> init##name() expands to in the BOOST_PYTHON_MODULE macro) sometime after
> Py_Initialize() init##name(). I can then instatiate objects of classes
> defined in C++ in my embedded python script. (There must be a better way to
> initialize the module??). I also suspect the application is now exporting
> functions and interfaces, but I'm not too worried about that right now.
> I would also like to have access to _instances_ of C++ objects. Ie. the
> object has already been instantiated in C++, and contains state. I would
> like to use this object in an embedded script.
> Are there anyone with ideas, pointers, or examples showing how this can be
If you know the lifetime of the C++ object will persist past its use
in Python, you can pass it to some Python callable object with ref():
void pass_to_python(object func, SomeCppClass& x)
// Invoke func with a Python object built around x
-- David Abrahams dave_at_[hidden] * http://www.boost-consulting.com Boost support, enhancements, training, and commercial distribution
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk