3.11.4.1 Running the Binding Generator

The binding generator is part of the gcc compiler and can be invoked via the -fdump-ada-spec switch, which will generate Ada spec files for the header files specified on the command line, and all header files needed by these files transitively. For example:

$ gcc -c -fdump-ada-spec -C /usr/include/time.h
$ gcc -c *.ads

will generate, under GNU/Linux, the following files: time_h.ads, bits_time_h.ads, stddef_h.ads, bits_types_h.ads which correspond to the files /usr/include/time.h, /usr/include/bits/time.h, etc…, and then compile these Ada specs. That is to say, the name of the Ada specs is in keeping with the relative path under /usr/include/ of the header files. This behavior is specific to paths ending with /include/; in all the other cases, the name of the Ada specs is derived from the simple name of the header files instead.

The -C switch tells gcc to extract comments from headers, and will attempt to generate corresponding Ada comments.

If you want to generate a single Ada file and not the transitive closure, you can use instead the -fdump-ada-spec-slim switch.

You can optionally specify a parent unit, of which all generated units will be children, using -fada-spec-parent=`unit'.

The simple gcc`-based command works only for C headers. For C++ headers you need to use either the g++ command or the combination gcc -x c++`.

In some cases, the generated bindings will be more complete or more meaningful when defining some macros, which you can do via the -D switch. This is for example the case with Xlib.h under GNU/Linux:

$ gcc -c -fdump-ada-spec -DXLIB_ILLEGAL_ACCESS -C /usr/include/X11/Xlib.h

The above will generate more complete bindings than a straight call without the -DXLIB_ILLEGAL_ACCESS switch.

In other cases, it is not possible to parse a header file in a stand-alone manner, because other include files need to be included first. In this case, the solution is to create a small header file including the needed #include and possible #define directives. For example, to generate Ada bindings for readline/readline.h, you need to first include stdio.h, so you can create a file with the following two lines in e.g. readline1.h:

#include <stdio.h>
#include <readline/readline.h>

and then generate Ada bindings from this file:

$ gcc -c -fdump-ada-spec readline1.h