我正试着把头转到艾达的头上去。为此,我需要了解如何使用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的内容。我使用了gnatmakVehicle1.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.)发布于 2015-09-01 19:14:23
文件e_c22_p2.ada包含两个程序单元,即规范和包Conveyance1的主体。
许多Ada编译器会对此很满意,很难说服小虫接受原样,但默认情况下,每个文件需要一个程序单元,文件名等于小写的单位名(用连字符替换),.ads代表规范,.adb代表主体。
GNAT附带了一个工具gnatchop (请参阅here),它接受像e_c22_p2.ada这样的输入文件,并将它们分割成预期的源文件(conveyance1.ads和conveyance1.adb)。
发布于 2020-04-29 12:39:38
正如西蒙所提到的,这个.ada文件有两个部分:规范和正文。因此,创建两个文件如下:
-- 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;这两个是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它还将编译您所包含的包。
https://stackoverflow.com/questions/32336188
复制相似问题