9

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.

0

4 Answers 4

12

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I went down this rabbit hole to try to get gccxml using CVS. It was nearly impossible on Windows. Here is a better way about it. stackoverflow.com/questions/3840268/… . Or here is a directly link to the project: sourceforge.net/projects/ctypes/files/gccxml
Here is a really good starting point for generating python code from a .h file: code.google.com/p/ctypesgen
2

Just found pycparser. May be useful.

Comments

1

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.