title: Using a C header in Python
body: <p>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 <a href="http://en.cppreference.com/w/c/language/bit_field" rel="nofollow noreferrer">bitfields</a>. 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. </p>
  
  <p>Of course we could copy the constants to python files, convert from/to raw data using <a href="https://docs.python.org/2/library/struct.html" rel="nofollow noreferrer">python's struct module</a> or use some library like <a href="https://pypi.python.org/pypi/bitstruct" rel="nofollow noreferrer">bitstruct</a> for the packed structures.</p>
  
  <p>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?</p>