views:

1451

answers:

5

I have a C++ header that contains #define statements, Enums and Structures. I have tried using the h2py.py script that is included with Python to no avail (except giving me the #defines converted). Any help would be greatly appreciated.

+1  A: 

From what I can tell, h2py.py isn't intended to convert anything other than #define macros. I did run across cppheaderparser, which might be worth a look.

Fred Larson
A: 

Where did you get the idea that h2py had anything to do with structs or enums?

From the source

# Read #define's and translate to Python code.
# Handle #include statements.
# Handle #define macros with one argument.

The words 'enum' and 'struct' never appear in the module.

S.Lott
+5  A: 

I don't know h2py, but you may want to look at 'ctypes' and 'ctypeslib'. ctypes is included with python 2.5+, and is targeted at creating binary compatibility with c-structs.

If you add ctypeslib, you get a sub-tool called codegen, which has a 'h2xml.py' script, and a 'xml2py.py', the combination of which will auto-generate the python code you're looking for from C++ headers.

ctypeslib:

http://pypi.python.org/pypi/ctypeslib/0.5.4a

h2xml.py will require another tool called gccxml:

http://www.gccxml.org/HTML/Index.html

it's best to check out (via CVS) the latest version of gccxml and build it yourself (actually easier done than said). The pre-packaged version is old.

A: 

Thank you Snoopy, your post was a great help.

williamtroup
+1  A: 

Just found pycparser. May be useful.

Nick