1
votes

I am trying to get my head around OOP in Ada. In order to do so, I need to understand how to name, compile and link package files with gnatmake.

This website (http://www.infres.enst.fr/~pautet/Ada95/chap22.htm) has good examples, but I don't understand how to compile the various bits of the program.

I am looking at e_c22_p2.ada and e_c22_p3.ada. From these I created a file called Conveyance1.ads and added the content of e_c22_p2 into this, and a file called Vehicle1.adb and added the content of e_c22_p3.ada in there. I used gnatmake Vehicle1.adb, but there are compilation errors.

e_c22_p2.ada contains:

                                            -- Chapter 22 - Program 2
package Conveyance1 is 

   -- This is a very simple transportation type.
   type TRANSPORT is
      record
         Wheels : INTEGER;
         Weight : FLOAT;
      end record;

   procedure Set_Values(Vehicle_In : in out TRANSPORT; 
                        Wheels_In  : INTEGER;
                        Weight_In  : FLOAT);
   function Get_Wheels(Vehicle_In : TRANSPORT) return INTEGER;
   function Get_Weight(Vehicle_In : TRANSPORT) return FLOAT;


   -- This CAR type extends the functionality of the TRANSPORT type.
   type CAR is new TRANSPORT;

   function Tire_Loading(Vehicle_In : CAR) return FLOAT;

end Conveyance1;




package body Conveyance1 is

-- Subprograms for the TRANSPORT record type.
procedure Set_Values(Vehicle_In : in out TRANSPORT; 
                     Wheels_In  : INTEGER; 
                     Weight_In  : FLOAT) is
begin
   Vehicle_In.Wheels := Wheels_In;
   Vehicle_In.Weight := Weight_In;
end Set_Values;

function Get_Wheels(Vehicle_In : TRANSPORT) return INTEGER is
begin
   return Vehicle_In.Wheels;
end Get_Wheels;

function Get_Weight(Vehicle_In : TRANSPORT) return FLOAT is
begin
   return Vehicle_In.Weight;
end Get_Weight;


-- Subprogram for the CAR record type.
function Tire_Loading(Vehicle_In : CAR) return FLOAT is
begin
   return Vehicle_In.Weight / FLOAT(Vehicle_In.Wheels);
end Tire_Loading;

end Conveyance1;




-- Results of execution
--
-- (This package cannot be executed alone.)
2
See this answer. TL;DR - use gnatchop on the source files (e_c22_p2.ada etc) to create the source files that GNAT expects.Simon Wright
Does it make sense for a means of transportation to have -42 wheels?Jacob Sparre Andersen
@JacobSparreAndersen yes it does - when you are transporting ICBM (or other big stuff) that is likely about the amount of wheels such vehicle will have.darkestkhan
@darkestkhan - I think you missed the sign. ;-)Jacob Sparre Andersen
@JacobSparreAndersen Oh boy, you are right - I did miss it xD (-42 wheels? car with 42 wheels missing?)darkestkhan

2 Answers

7
votes

The file e_c22_p2.ada contains two program units, the spec and the body of package Conveyance1.

Many Ada compilers would be quite happy with this, and it is possible with some difficulty to persuade GNAT to accept it as-is, but GNAT’s default is to expect one program unit per file, with the file name equal to the unit name in lower case (dots replaced by hyphens), and .ads for the spec, .adb for the body.

GNAT comes with a tool gnatchop (see here) which takes input files like your e_c22_p2.ada and splits them up into the source files that GNAT expects (conveyance1.ads and conveyance1.adb).

0
votes

As mentioned by Simon, this .ada file has two sections : specs and body. So create two files as below:

  1. Conveyance1.ads - it contains the declarations of functions, procedures, variables

  2. Conveyance1.adb - it contains the bodies of the functions, procedures

    The first file

-- Conveyance1.ads
---------------------------------------------------------------------------------
    package Conveyance1 is 

   -- This is a very simple transportation type.
   type TRANSPORT is
      record
         Wheels : INTEGER;
         Weight : FLOAT;
      end record;

   procedure Set_Values(Vehicle_In : in out TRANSPORT; 
                        Wheels_In  : INTEGER;
                        Weight_In  : FLOAT);
   function Get_Wheels(Vehicle_In : TRANSPORT) return INTEGER;
   function Get_Weight(Vehicle_In : TRANSPORT) return FLOAT;


   -- This CAR type extends the functionality of the TRANSPORT type.
   type CAR is new TRANSPORT;

   function Tire_Loading(Vehicle_In : CAR) return FLOAT;

end Conveyance1;

And second file,

-- Conveyance1.adb
------------------------------------------------------------------
package body Conveyance1 is

-- Subprograms for the TRANSPORT record type.
procedure Set_Values(Vehicle_In : in out TRANSPORT; 
                     Wheels_In  : INTEGER; 
                     Weight_In  : FLOAT) is
begin
   Vehicle_In.Wheels := Wheels_In;
   Vehicle_In.Weight := Weight_In;
end Set_Values;

function Get_Wheels(Vehicle_In : TRANSPORT) return INTEGER is
begin
   return Vehicle_In.Wheels;
end Get_Wheels;

function Get_Weight(Vehicle_In : TRANSPORT) return FLOAT is
begin
   return Vehicle_In.Weight;
end Get_Weight;


-- Subprogram for the CAR record type.
function Tire_Loading(Vehicle_In : CAR) return FLOAT is
begin
   return Vehicle_In.Weight / FLOAT(Vehicle_In.Wheels);
end Tire_Loading;

end Conveyance1;

These 2 are ada package files. In order to use these you need to create another .adb (say demo.adb) file as below. Then 'with' and 'use' these files in demo.adb

-- demo.adb
------------------------------------------------------------------
with Conveyance1; use Conveyance1;
with Ada.Text_IO; use Ada.Text_IO;


procedure demo is
    my_scooter : TRANSPORT;
begin

    Set_Values(my_scooter, 3, 250.00);    -- it has a sidecar as well !!!!

end demo;

and then finally compile your code with

gnatmake demo.adb

it will compile your included packages as well.