Next: , Up: Binding Using gnatbind

4.1 Running gnatbind

The form of the gnatbind command is

     $ gnatbind [switches] mainprog[.ali] [switches]

where mainprog.adb is the Ada file containing the main program unit body. If no switches are specified, gnatbind constructs an Ada package in two files whose names are, and b~mainprog.adb. For example, if given the parameter hello.ali, for a main program contained in file hello.adb, the binder output files would be and b~hello.adb.

When doing consistency checking, the binder takes into consideration any source files it can locate. For example, if the binder determines that the given main program requires the package Pack, whose .ALI file is pack.ali and whose corresponding source spec file is, it attempts to locate the source file (using the same search path conventions as previously described for the gcc command). If it can locate this source file, it checks that the time stamps or source checksums of the source and its references to in ALI files match. In other words, any ALI files that mentions this spec must have resulted from compiling this version of the source file (or in the case where the source checksums match, a version close enough that the difference does not matter).

The effect of this consistency checking, which includes source files, is that the binder ensures that the program is consistent with the latest version of the source files that can be located at bind time. Editing a source file without compiling files that depend on the source file cause error messages to be generated by the binder.

For example, suppose you have a main program hello.adb and a package P, from file and you perform the following steps:

  1. Enter gcc -c hello.adb to compile the main program.
  2. Enter gcc -c to compile package P.
  3. Edit file
  4. Enter gnatbind hello.

At this point, the file p.ali contains an out-of-date time stamp because the file has been edited. The attempt at binding fails, and the binder generates the following error messages:

     error: "hello.adb" must be recompiled ("" has been modified)
     error: "" has been modified and must be recompiled

Now both files must be recompiled as indicated, and then the bind can succeed, generating a main program. You need not normally be concerned with the contents of this file, but for reference purposes a sample binder output file is given in Example of Binder Output File.

In most normal usage, the default mode of gnatbind which is to generate the main package in Ada, as described in the previous section. In particular, this means that any Ada programmer can read and understand the generated main program. It can also be debugged just like any other Ada code provided the -g switch is used for gnatbind and gnatlink.

However for some purposes it may be convenient to generate the main program in C rather than Ada. This may for example be helpful when you are generating a mixed language program with the main program in C. The GNAT compiler itself is an example. The use of the -C switch for both gnatbind and gnatlink will cause the program to be generated in C (and compiled using the gnu C compiler).