I've been working on creating a python wrapper around a C library
using Cython. In the process of doing this, I solved a more general
problem and created a script that converts any C header to a pxd
definition file. This is attached here, in the hopes that it may be
useful to someone else as well. I suppose I could put it in github or
something, but that just seems silly for a single file. Perhaps the
Cython maintainers will see fit to pull it into the main distribution.
The script, of course, is not very magic. It converts whatever C
definitions it sees, but it doesn't really know anything about
conditional compilation (though you can instruct it to ignore
#if(n)def blocks based on a given definition), it ignores macro
functions, and it assumes that all instances of "#define x y" define a
constant. You can configure quite a lot of the behavior, via
commandline or configuration file, e.g. to make all structs opaque, or
to skip headers or add other header files. After tweaking the
configuration, I was able to use most of my header files as-is, though
I had to hand-code a few stubs. For more complicated headers, you
might have to tweak the output manually, but you'll probably still
save time over writing it yourself.
Cheers,
Evan
Hi all,I've been working on creating a python wrapper around a C library
using Cython. In the process of doing this, I solved a more general
problem and created a script that converts any C header to a pxd
definition file. This is attached here, in the hopes that it may be
useful to someone else as well. I suppose I could put it in github or
something, but that just seems silly for a single file. Perhaps the
Cython maintainers will see fit to pull it into the main distribution.
I agree that this would be very useful. There's a potential GSoC
project to do this leveraging GCC's parser too.
- Robert