embed python in C++
aleax at aleax.it
Thu Aug 9 11:54:38 CEST 2001
"wkuang" <wkuang at aludra.usc.edu> wrote in message
news:Pine.GSO.4.21.0108081706040.6900-100000 at aludra.usc.edu...
> I am interested in having an interpreter handling my c++ program
> input/output to add some flexibility, and someone tells me python maybe
> one. I searched through python website and unfortanetly got little
> information other than a page saying python could be embeded in C++
> program. I am wondering if it is possible to have my c++ program read
> memory that python generated. For example,
> Is there any book or website that elaborate embedding stuff? And any
If your C++ compiler is up to snuff, check out www.boost.org, and
specifically the Boost Python Library: it makes it very easy to
expose your program's classes to Python (that's the "extending"
part of the "extending and embedding" paradigm). Once you have
that, the embedding proper is quite simple, and pretty well
documented e.g. at http://www.python.org/doc/current/ext/ext.html
(which however focuses on the C-level approaches; Boost Python
uses the most advanced C++ functionality, particularly templates,
to let you do similar extending-things MUCH more smoothly:-).
If your C++ compiler isn't very good & updated, there are other
approaches to exposing your application functionality to the
scripting language -- the C-level Python API at the above URL,
SWIG at http://www.swig.org/, CXX (if you have a decent but
not really good C++ compiler) http://cxx.sourceforge.net/, and
others yet. Note that all of these focus on the hard part,
extending -- embedding itself is much simpler, as
"The one important task that only embedders (as opposed to extension
writers) of the Python interpreter have to worry about is the
initialization, and possibly the finalization, of the Python interpreter".
For a simple example of embedding, see also
http://www.mcmillan-inc.com/embed.html (seems to be down
right now, but try Google at:
it will be about the 7th hit and you can visit the
'Cached' entry to read it).
More information about the Python-list