Embedding Python with C++

Ken Seehof kseehof at neuralintegrator.com
Tue Apr 9 03:19:49 EDT 2002


Chris Stoy wrote:
> Hey all,
>
> I'm looking to embed Python as a scripting language in a game I am working
> on.  I want to be able to call C++ methods from Python, and call
> Python from
> C++ (for the usual code quick in Python, convert to C++ for speed later.)
>
> I've looked into using the Boost.Python code and I'm fine with that.
>
> My problem is, I have no clue how to compile Python into a static linked
> library for Windows.  Actually, I need it to be cross-platform, which is
> another issue.
>
> I've never embedded Python before, and I'm fairly new to Python, so can
> someone please give me a pointer to how to do this?  What are the files I
> need to compile to build the static lib (using MS Visual Studio
> 6.0?) Also,
> I don't need all of the standard Python components (like networking.)  Can
> those be removed from the lib?
>
> Thanks.
>
> Chris.

Actually you don't need to turn your python code into a library
to access from C++.  That's not the best approach.  The entry
points into python are provided by python*.dll (in windows).

Instead, use PyObject_CallObject and associated supporting functions
to invoke callable objects (python functions and methods).  Read the
"Extending and Embedding" section in the main python documentation.
http://www.python.org/doc/current/ext/ext.html

I recommend making the main application python, and implement low
level functionality as extensions.  In my experience this works much
better than writing that main application in C++.

Implement your low-level C++ funcitonality as a set of extension
types that your main python application calls.

If your development enviroment is Windows, check out my extention
wizard.  It's a Visual C++ wizard for creating python extensions.
I use it in situations where SWIG is not useful (i.e when I am
writing new code and not just wrapping an existing library).

You can find PyExtWizard here:
http://www.neuralintegrator.com/downloads

- Ken Seehof






More information about the Python-list mailing list