Is it possible to automatically generate interface units from C header files? In particular, I want to wrap the HDF5 library, and it would be great if I could avoid writing the interface unit manually.
-
That looks like a very large and complex library. I will confidently predict that there is no tool that does a good and comprehensive translation. – David Heffernan Oct 17 '12 at 17:56
-
1There is a partial translation to Delphi of version 4 of the hdf library [here](http://dspatial.sourceforge.net/). You will find it under the `Drivers` folder. It may serve as a first step of translation. – LU RD Oct 17 '12 at 18:10
-
Problem is HDF4 and HDF5 don't share anything except for the *HDF* in their name ... – andreas-h Oct 17 '12 at 20:42
4 Answers
The free pascal includes the H2PAS tool.
h2pas attempts to convert a C header file to a pascal unit. it can handle most C constructs that one finds in a C header file, and attempts to translate them to their pascal counterparts.

- 134,889
- 20
- 356
- 483
-
It is way better than other tools I used, but in practice you need to still do a lot of post processing by hand. – Jeroen Wiert Pluimers Oct 17 '12 at 18:54
Bob Swart (Dr Bob) has a utility which will convert a lot of header files (although there's usually some manual work involved as well) called HeaderConvert. I've never compared it to the tool @RRUZ links, but it's another option.
Project JEDI has one as well; I've never tested it. You can find it here.

- 123,280
- 14
- 225
- 444
In general fully automated translating of C headers to something else (that isn't an effective superset of the needed C functionality) is hard to impossible.
This because due to macros one can't see how to translate them. Macros often only get their meaning from context. Example
#define uglymacro 1,2,3,4
but also (and this one is more common):
SCARYAPIMACRO void func(int c);
SCARYAPIMACRO is then often a macro that tests OS defines to select the right calling convention for the right OS/architecture.
Still, that doesn't mean that the tools are not real timesavers. But the result is more semiautomatic, I've the most and best experience with h2pas.
I've translated a lot of Windows headers (including FPC's commctrl which has a sendmessage macro every few lines).
What I usually do is craft a small pascal program that scans the source linebased and uses heuristics to split it into parts that are mostly homogeneous (all structs or constants,macros, procedure declaration etc). Then I look at the source and often do some global substitutes.
Only then I run it through the translator, the process is often iterative (refine separation, do global substitutions, try to translate, if it fails, try again etc).
The process unfortunately does require a good grasp of C, pragma stuff included.

- 601,492
- 42
- 1,072
- 1,490

- 25,628
- 5
- 56
- 89
You can download HDF5 API header Delphi translations, Delphi XE2 HDF5 table test program with source code and somewhat modified hdf5dll from my page: http://www.astro.ff.vu.lt/index.php?option=com_content&task=view&id=46&Itemid=63

- 11
- 1