3.11.5.1 Running the C Header Generator

The C header generator is part of the GNAT compiler and can be invoked via the -gnatceg combination of switches, which will generate a .h file corresponding to the given input file (Ada spec or body). Note that only spec files are processed in any case, so giving a spec or a body file as input is equivalent. For example:

$ gcc -c -gnatceg pack1.ads

will generate a self-contained file called pack1.h including common definitions from the Ada Standard package, followed by the definitions included in pack1.ads, as well as all the other units withed by this file.

For instance, given the following Ada files:

package Pack2 is
   type Int is range 1 .. 10;
end Pack2;
with Pack2;

package Pack1 is
   type Rec is record
      Field1, Field2 : Pack2.Int;
   end record;

   Global : Rec := (1, 2);

   procedure Proc1 (R : Rec);
   procedure Proc2 (R : in out Rec);
end Pack1;

The above gcc command will generate the following pack1.h file:

/* Standard definitions skipped */
#ifndef PACK2_ADS
#define PACK2_ADS
typedef short_short_integer pack2__TintB;
typedef pack2__TintB pack2__int;
#endif /* PACK2_ADS */

#ifndef PACK1_ADS
#define PACK1_ADS
typedef struct _pack1__rec {
  pack2__int field1;
  pack2__int field2;
} pack1__rec;
extern pack1__rec pack1__global;
extern void pack1__proc1(const pack1__rec r);
extern void pack1__proc2(pack1__rec *r);
#endif /* PACK1_ADS */

You can then include pack1.h from a C source file and use the types, call subprograms, reference objects, and constants.