是否可以将子包作为Ada中的单独编译单元

Awa*_*ken 5 ada ada95

我有一个包含普通规格和正文文件的主包.我正在尝试创建父包的子包,但希望它们在单独的编译文件中.如果它只是一个包体,或者它是一个子程序/ proc/func,我可以很容易地完成它.但是,我不能让它让我制作一个子规格文件.

我这样做的原因是因为我希望在同一父母的其他孩子可以使用的孩子中获得信息.我知道我可以通过在父代中包含spec部分来实现这一点,但这使得我的父文件非常大.

这甚至可能,或者我别无选择,只能制作另一个根单位?或者只是在父母身上留下一切明智的东西?

我试过了:

在父:( package Child1 is separate; 也试过Parent.Child1,但这给编译错误

在孩子:

separate(Parent)

package Parent.Child1 is
....
end Parent.Child1;
Run Code Online (Sandbox Code Playgroud)

想法?只是不可能?

更新:我正在使用Green Hills Multi Compiler进行编译.Ada95语言版,非OO项目.

Sha*_*rk8 7

注意到你正在使用separate我要冒险的关键字,你的问题不是关于子单元,而是嵌套单元.

请尝试以下方法:

Testing.adb

With
Ada.Text_IO,
Parent;

Procedure Testing is
Begin
    Ada.Text_IO.Put_Line("Starting Test:");

    Parent.Nested.Test_Procedure;

    Ada.Text_IO.Put_Line("Testing complete.");
End Test;
Run Code Online (Sandbox Code Playgroud)

Parent.ads

Package Parent is

    Package Nested is
        Procedure Test_Procedure;
    End Nested;

End Parent;
Run Code Online (Sandbox Code Playgroud)

Parent.adb

Package Body Parent is

    Package Body Nested is separate;

End Parent;
Run Code Online (Sandbox Code Playgroud)

Parent-Nested.adb

(注意:您可能必须使用稍微不同的文件名,我使用GNAT使用"点替换"的默认设置.)

with Ada.Text_IO;

separate (Parent)

package body Nested is

    Procedure Test_Procedure is
    Message : Constant string:= ASCII.HT &
      "Hello from the separate, nested test-procedure.";
    begin
        Ada.Text_IO.Put_Line( Message );
    end Test_Procedure;

End Nested;
Run Code Online (Sandbox Code Playgroud)

你应该能够编译,输出应该是三行,如下所示:

  1. Starting Test:
  2. Hello from the separate, nested test-procedure.
  3. Testing complete.

这里的问题源于对嵌套包和子包之间的差异的轻微误解.两者都使用相同的dot-delimited-qualification方法访问:Parent.NestedParent.Child.

微妙的区别是子包总是一个单独编译的单元(在GNAT中它们总是在不同的文件中,这是一个实现限制,因为他们[不]实现库..但是一些Ada编译器可以放不同的compilation_units到同一个文件中) - 但是嵌套包必须在编译其封闭单元的同时编译,除非它被特别标记为separate.


为了保留当前的嵌套结构并仍然使用separate,您可以使用以下方法和一个包含包的所有规范的Auxiliary包.

Parent.ads

Package Parent is

    -- Here's the magic of renaming. --'
    Package Nested renames Auxiliary.Delegate;

End Parent;
Run Code Online (Sandbox Code Playgroud)

Auxiliary.ads

Package Auxiliary is

    Package Delegate is
        Procedure Test_Procedure;
    End Delegate;

End Auxiliary;
Run Code Online (Sandbox Code Playgroud)

Auxiliary.adb

package body Auxiliary is

    Package Body Delegate is separate;

end Auxiliary;
Run Code Online (Sandbox Code Playgroud)

Auxiliary-Delegate.adb

(注意:您可能必须使用稍微不同的文件名,我使用GNAT使用"点替换"的默认设置.)

with Ada.Text_IO;

separate (Auxiliary)

package body Delegate is

    Procedure Test_Procedure is
    Message : Constant string:= ASCII.HT &
      "Hello from the separate, nested test-procedure.";
    begin
        Ada.Text_IO.Put_Line( Message );
    end Test_Procedure;

End Delegate;
Run Code Online (Sandbox Code Playgroud)


Ant*_*ony 5

是的,这完全没问题.您可以将父包和子包放在单独的文件中:

parent.ads

package Parent is
-- ...
end Parent;
Run Code Online (Sandbox Code Playgroud)

parent-child.ads

package Parent.Child is
-- ...
end Parent.Child;
Run Code Online (Sandbox Code Playgroud)

parent-other.ads:

limited with Parent.Child; --Need Ada 2005
package Parent.Other is
-- ...
end Parent.Other;
Run Code Online (Sandbox Code Playgroud)

parent.child包和parent.other包访问定义,parent(有一些限制).

请注意parent.other" withs" 如何parent.child使其可以访问中的定义parent.child.

我有一个如何做到的例子.另外,这里是wikibooks的一个例子.