Running the Binding Generator (GNAT User’s Guide for Native Platforms)

#### 3.11.5.1 Running the Binding Generator ¶

The binding generator is part of the `gcc` compiler and you can invoke it via the `-fdump-ada-spec` switch, which generates 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

generates, 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`, and `/usr/include/bits/time.h` and then compile these Ada specs. The name of the Ada specs is consistent 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 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 generates more complete bindings than a call without the `-DXLIB_ILLEGAL_ACCESS` switch.

In other cases, you can’t 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