Next: , Previous: Set source search path, Up: Compiling Examples

6.4 Use GNAT project file

In this example, we show how to use a GNAT project file, with no Ada mode project file.

Create the directory Example_4, containing:

hello_pkg.ads:

     package Hello_Pkg is
        procedure Say_Hello;
     end Hello_Pkg;

hello_pkg.adb:

     with Ada.Text_IO;
     package Hello_Pkg is
        procedure Say_Hello
        is begin
           Ada.Text_IO.Put_Line ("Hello from hello_pkg.adb");
        end Say_Hello;
     end Hello_Pkg;

These are the same files from example 1; hello_pkg.adb has an error on line 2.

In addition, create a directory Example_4/Gnat_Project, containing these files:

Gnat_Project/hello_4.adb:

     with Hello_Pkg;
     with Ada.Text_IO; use Ada.Text_IO;
     procedure Hello_4
     is begin
        Hello_Pkg.Say_Hello;
        Put_Line ("From hello_4");
     end Hello_4;

There are no errors in this file.

Gnat_Project/hello_4.gpr:

     Project Hello_4 is
        for Source_Dirs use (".", "..");
     end Hello_4;

In buffer hello_4.adb, invoke ‘Ada | Project | Load...’, and select Example_4/Gnat_Project/hello_4.gpr.

Then, again in hello_4.adb, invoke ‘Ada | Set main and Build’. You should get a *compilation* buffer containing something like (the directory paths will be different):

     cd c:/Examples/Example_4/Gnat_Project/
     gnatmake -o hello_4 hello_4 -Phello_4.gpr -cargs -gnatq -gnatQ -bargs  -largs
     gcc -c -g -gnatyt -gnatq -gnatQ -I- -gnatA c:\Examples\Example_4\Gnat_Project\hello_4.adb
     gcc -c -g -gnatyt -gnatq -gnatQ -I- -gnatA c:\Examples\Example_4\hello_pkg.adb
     hello_pkg.adb:2:08: keyword "body" expected here [see file name]
     gnatmake: "c:\examples\example_4\hello_pkg.adb" compilation error

Compare the gcc options to the compiler output in Set compiler options; this shows that hello_4.gpr is being used to set the compiler options.

Fixing the error, linking and running the code proceed as in No project files.