Skip to content Skip to sidebar Skip to footer

Convert C++ Header Files To Python

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 #d

Solution 1:

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.

Solution 2:

Just found pycparser. May be useful.

Solution 3:

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.

Solution 4:

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.

Post a Comment for "Convert C++ Header Files To Python"