2.4 Running a Program with Multiple Units

Consider a slightly more complicated example that has three files: a main program, and the spec and body of a package:

package Greetings is
   procedure Hello;
   procedure Goodbye;
end Greetings;

with Ada.Text_IO; use Ada.Text_IO;
package body Greetings is
   procedure Hello is
      Put_Line ("Hello WORLD!");
   end Hello;

   procedure Goodbye is
      Put_Line ("Goodbye WORLD!");
   end Goodbye;
end Greetings;

with Greetings;
procedure Gmain is
end Gmain;

Following the one-unit-per-file rule, place this program in the following three separate files:


spec of package Greetings


body of package Greetings


body of main program

Note that there is no required order of compilation when using GNAT. In particular it is perfectly fine to compile the main program first. Also, it is not necessary to compile package specs in the case where there is an accompanying body; you only need to compile the body. If you want to submit these files to the compiler for semantic checking and not code generation, then use the -gnatc switch:

$ gcc -c greetings.ads -gnatc

Although the compilation can be done in separate steps, in practice it is almost always more convenient to use the gnatmake or gprbuild tools:

$ gnatmake gmain.adb