6

We have some data structure defined in C but need to use it from C/C++ as well as from Python code. In general, the C code is not very complicated, we are talking about a few header files containing constants, enums, "normal" structs and packed structs with bitfields. The files define an interface between two software parts that have to be used on micro controllers as well as on more powerful computers, hence the C structs. We want to use the structs to decode the messages and access different parts with names, rather than by using bit masks or similar techniques.

Of course we could copy the constants to python files, convert from/to raw data using python's struct module or use some library like bitstruct for the packed structures.

But all of that would include writing highly redundant code that would require changes in many different files and languages if a small part of the specification changes slightly. Can that be done in a more automated and less error prone way?

Felix D.
  • 786
  • 1
  • 11
  • 17
  • 1
    https://stackoverflow.com/questions/145270/calling-c-c-from-python – Ashalynd Nov 27 '17 at 23:59
  • Thank you for that link, those libraries are quite useful. But I do not see how they can be used to access structs in a non-verbose way. – Felix D. Nov 28 '17 at 15:38
  • 2
    Write (or google for) a ctypes code generator. Here's an old one: http://starship.python.net/crew/theller/ctypes/old/codegen.html. I've also used [pyparsing](http://pyparsing.wikispaces.com/) to write my own. – Mark Tolonen Dec 02 '17 at 18:16

0 Answers0