How to port Python code into C++ code automatically?

Jeff Schwab jeff at schwabcenter.com
Thu Mar 13 01:45:15 EDT 2008


Bo wrote:
> I want to port a Python project (about 10,000 line python code) to C+
> +. Is there any automatically tool to do this kind of things? e.g.,

That's not trivial.  Python is very heavily oriented toward run-time 
processing, whereas C++ favors compile-time processing.

> e.g., SWIG(http://www.swig.org/)?

Swig isn't so much a translator as a way of linking C or C++ code to 
multiple different scripting languages, using shared configuration (.i) 
files.  If you're only connecting two languages (Python and C++), Swig 
is probably not going to help you much unless you happen to already be a 
master of Swig-foo.

If the Python code works, then it's probably best to keep it in Python. 
  To access it from C++, try Boost.Python.  If you really do need to 
translate the code from Python to C++, try to do it incrementally.  If 
the Python code is so monolithic that you can't find individual pieces 
to move one-at-a-time, consider refactoring the code in Python so that 
you'll at least have some modularity to work with.  (In the process of 
refactoring, you may also find that you don't have to move the code out 
of Python, after all, e.g. because the performance improves along with 
the design.)



More information about the Python-list mailing list