Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

embed python in C++

0 views
Skip to first unread message

wkuang

unread,
Aug 8, 2001, 8:21:52 PM8/8/01
to
hi,

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,

in c++ program, I have a class defined as,

class myclass {

int i;
myclass* ptr;
...
}

and I define a pointer and somehow send it to python (I suppose I could?)

myclass* classptr;


@ python script, I generate a list of class,

myclass *classInPython = new myclass[10];
(sorry, in cpp syntax)
and set each pointer in class member pointing to the next class (as link
list)

After running script, could I read all the classes in my c++ main
function?

Is there any book or website that elaborate embedding stuff? And any
suggestion on script interpreter other than python is welcomed.

Thanks!

Wan Kuang

Electric Engineering - Electrophysics
University of Southern California
Los Angeles, CA 90007


Alex Martelli

unread,
Aug 9, 2001, 5:54:38 AM8/9/01
to
"wkuang" <wku...@aludra.usc.edu> wrote in message
news:Pine.GSO.4.21.01080...@aludra.usc.edu...

> hi,
>
> 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
http://www.python.org/doc/current/api/embedding.html says
"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:
http://www.google.com/search?num=20&hl=en&safe=off&q=embedding+python
it will be about the 7th hit and you can visit the
'Cached' entry to read it).


Alex

wkuang

unread,
Aug 10, 2001, 5:47:12 PM8/10/01
to Alex Martelli
Is there anyone know how to install boost.python. The website said most
boost libary are implemented entirely within their header files, but the
makefile for linux have libboost_python.a, so I assume boost.python is one
of the exception. Basically, I havenot been able to run a single example
successfully. Any idea? Thanks.

I am using linux 7.1 with KCC 4.0e, gcc2.96 is kind of ok.

Wan Kuang

Electric Engineering - Electrophysics
University of Southern California
Los Angeles, CA 90007

Alex Martelli

unread,
Aug 20, 2001, 10:15:50 AM8/20/01
to
"wkuang" <wku...@usc.edu> wrote in message
news:Pine.GSO.4.21.010810...@aludra.usc.edu...

> Is there anyone know how to install boost.python. The website said most
> boost libary are implemented entirely within their header files, but the
> makefile for linux have libboost_python.a, so I assume boost.python is one
> of the exception. Basically, I havenot been able to run a single example
> successfully. Any idea? Thanks.
>
> I am using linux 7.1 with KCC 4.0e, gcc2.96 is kind of ok.

Right. Go to boost_1_23_0\libs\python\build where you've unpacked
the sources and you'll see a file named linux_gcc.mak. Run GNU
make, specifying that file as the makefile, and off you go.


Alex

David Konerding

unread,
Aug 20, 2001, 2:37:27 PM8/20/01
to

That's not enough.

1) extract boost.

2) make a new directory, preferably not in the boost source tree

3) cp the linux_gcc.mak from boost_1_23_0/libs/python/build to the new
dir

4) edit linux_gcc.mak so the settings for the variables are correct

5) run "make -f linux_gcc.mak softlinks"

6) finally run "make -f linux_gcc.mak" and cross your fingers.

0 new messages