我试图在Ada中绕过OOP。为了做到这一点,我需要了解如何使用gnatmake命名,编译和链接包文件。
这个网站(http://www.infres.enst.fr/~pautet/Ada95/chap22.htm)有很好的例子,但我不明白如何编译程序的各个部分。
我在看e_c22_p2.ada和e_c22_p3.ada。从这些文件中我创建了一个名为Conveyance1.ads的文件,并在其中添加了e_c22_p2的内容,以及一个名为Vehicle1.adb的文件,并在其中添加了e_c22_p3.ada的内容。我使用了gnatmake Vehicle1.adb,但是存在编译错误。
e_c22_p2.ada包含:
-- 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.)
答案 0 :(得分:6)
文件fatal error: unexpectedly found nil while unwrapping an Optional value
包含两个程序单元,规范和包e_c22_p2.ada
的正文。
许多Ada编译器对此非常满意,并且可能会有一些困难说服GNAT按原样接受它,但GNAT默认是期望每个文件一个程序单元,文件名等于单位小写的名称(用连字符替换的点),以及规范的Conveyance1
,身体的.ads
。
GNAT附带了一个工具.adb
(请参阅here),它接受像gnatchop
这样的输入文件,并将它们拆分为GNAT期望的源文件(e_c22_p2.ada
和conveyance1.ads
)。
答案 1 :(得分:0)
正如Simon所提到的,此.ada文件有两个部分:specs和body。 因此,创建两个文件,如下所示:
Conveyance1.ads-它包含函数,过程,变量的声明
Conveyance1.adb-它包含函数,过程的正文
第一个文件
-- 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;
第二个文件
-- 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;
这2个是ada软件包文件。 为了使用这些文件,您需要创建另一个.adb文件(例如demo.adb),如下所示。 然后在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;
然后最终使用
编译代码gnatmake demo.adb
它还将编译您包含的软件包。