软件工程与Ada:stubs;分离和编译单元 [英] Software engineering with Ada: stubs; separate and compilation units

查看:246
本文介绍了软件工程与Ada:stubs;分离和编译单元的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有机械工程背景,但我有兴趣学习Ada良好的软件工程实践。我有几个查询。



Q1。如果我理解正确,那么有人可以只写一个包规范(ads)文件,编译,然后编译使用该包的主程序。后来,当人们知道要在包体中包括什么时,那么后者可以被编写和编译。之后,主程序现在可以运行。我试过这个,我想确认这是好的做法。



Q2。我的第二个问题是关于存根(子单元)和使用SEPARATE。说我有一个主程序如下:

  With Ada.Float_Text_IO; 
With Ada.Text_IO;
With Ada.Integer_Text_IO;

程序测试2 IS
A,B:FLOAT;
N:INTEGER;

程序输入(A,B:输出浮点; N:输出整数)是独立的;

BEGIN - 主程序
INPUT(A,B,N);
Ada.Float_Text_IO.Put(Item => A);
Ada.Text_IO.New_line;
Ada.Integer_Text_IO.Put(Item => N);
END TEST2;然后我有一个单独的文件中的过程INPUT:


$ b


 单独(TEST2)
程序输入(A,B:OUT FLOAT; N:OUT INTEGER)IS
BEGIN
Ada.Float_Text_IO .Get(Item => A);
Ada.Text_IO.New_line;
Ada.Float_Text_IO.Get(Item => B);
Ada.Text_IO.New_line;
Ada.Integer_Text_IO.Get(Item => N);
END INPUT;

我的问题:



a)AdaGIDE建议我将INPUT过程文件保存为input.adb。但在编译主程序test2时,我收到了警告:

 警告:文件test2中的TEST2.INPUT -input.adbnot found 
无法为文件test2.adb(缺少子单元)生成代码

To AdaGIDE,这是一个错误,因为上面的警告在消息之前:

 编译... 
完成 - 检测到错误



因此我将input.adb文件重命名为test2-input.adb正如AdaGIDE在编译时向我建议的那样。现在编译主文件,我没有任何警告。我现在的问题是,如果可以写

  PROCEDURE INPUT(A,B:OUT FLOAT; N:OUT INTEGER)IS 

,就像我在子单元文件test2-input.adb中做的那样,描述性术语如

 程序测试2输入(A,B:输出浮点; N:输出整数)IS 

以强调过程输入具有父过程test2?这个想法来自AdaGIDE暗示我关于test2-input.adb如上所述。



b)我的下一个问题:



如果我很好理解编译顺序,那么我应该先编译主文件test2.adb,然后编译stub test2-input.adb。在编译存根时,我得到错误消息:

 无法生成文件test2-input.adb(subunit)
Done - 检测到错误

但是,现在我可以对test2.adb进行绑定和链接运行程序。



我想知道如果我做错了,试图编译stub test2-input.adb或者不应该编译?



Q3。具有亚基的用途是什么?是只是将一个大型程序分成更小的部分?我知道一个错误出现,如果一个不在BEGIN和END之间的任何语句在子单元。所以这意味着,总是必须在那里发表声明。如果想要稍后编写语句,可以在子单元的BEGIN和END之间总是放置NULL语句,稍后再返回到后者。



感谢很多...

解决方案

Q1:这是优秀的实践。



通过将包规范视为 >



Q2:我相信AdaGIDE实际上是使用GNAT编译器进行所有的编译,所以它实际上是GNAT负责可接受的文件名。 (这可以配置,但除非你有一个非常令人信服的理由这样做,使用GNAT / AdaGIDE的文件命名约定简单得多。)更多与你的问题相关,没有强烈的理由包括父单位作为单独单位名称的一部分。但是看看Q3的答案...



Q3:子单元介绍了第一个版本的Ada-Ada 83,部分是为了帮助模块化代码,用于延迟开发和编译。然而,Ada软件开发实践已经放弃了对子单元的使用,所有的过程/函数/任务/等等体都简单地保持在包的主体中。它们仍然在某些领域中使用,如果可能需要特定于平台的子程序版本,但是大多数情况下它们很少被使用。它留下较少的文件来跟踪,并保持一个包的实现代码在一起。所以我强烈建议你简单地忽略子单元的功能,并将所有的实现代码放在包体中。


I'm with a mechanical engineering background but I'm interested to learn good software engineering practice with Ada. I have a few queries.

Q1. If I understand correctly then someone can just write a package specification (ads) file, compile it and then compile the main program which is using the package. Later on, when one knows what to include in the package body then the latter can be written and compiled. Afterwards, the main program can now be run. I've tried this and I would like to confirm that this is good practice.

Q2. My second question is about stubs (sub-units) and the use of SEPARATE. Say I have a main program as follows:

    WITH Ada.Float_Text_IO;
    WITH Ada.Text_IO;
    WITH Ada.Integer_Text_IO;

    PROCEDURE TEST2 IS
    A,B      : FLOAT;
    N        : INTEGER;

    PROCEDURE INPUT(A,B: OUT FLOAT; N: OUT INTEGER) IS SEPARATE;

    BEGIN -- main program
      INPUT(A,B,N);
      Ada.Float_Text_IO.Put(Item => A);
      Ada.Text_IO.New_line;
      Ada.Integer_Text_IO.Put(Item => N);
    END TEST2;

Then I have the procedure INPUT in a separate file:

separate(TEST2)
PROCEDURE INPUT(A,B: OUT FLOAT; N: OUT INTEGER) IS
   BEGIN
      Ada.Float_Text_IO.Get(Item => A);
      Ada.Text_IO.New_line;
      Ada.Float_Text_IO.Get(Item => B);
      Ada.Text_IO.New_line;
      Ada.Integer_Text_IO.Get(Item => N);
   END INPUT;

My questions:

a) AdaGIDE suggests me to save the the INPUT procedure file as input.adb. But then on compiling the main program test2, I get the warning:

warning: subunit "TEST2.INPUT" in file "test2-input.adb" not found
cannot generate code for file test2.adb (missing subunits)

To AdaGIDE, this is more of an error as the above warnings come before the message:

Compiling...
Done--error detected

So I renamed the input.adb file to test2-input.adb as was suggested to me by AdaGIDE on compiling. Now on compiling the main file, I don't have any warnings. My question now is if it's ok to write

PROCEDURE INPUT(A,B: OUT FLOAT; N: OUT INTEGER) IS

as I did in the sub-unit file test2-input.adb or is it better to write a more descriptive term like

PROCEDURE TEST2-INPUT(A,B: OUT FLOAT; N: OUT INTEGER) IS

to emphasize that procedure input has a parent procedure test2 ? This thought follows from AdaGIDE hinting me about test2-input.adb as I mentioned above.

b) My next question:

If I understand well the compilation order, then I should compile the main file test2.adb first and then the stub test2-input.adb . On compiling the stub I get the error message:

cannot generate code for file test2-input.adb (subunit)
Done--error detected

However, I can now do the binding and linking for test2.adb and run the program .

I would like to know if I did wrong by trying to compile the stub test2-input.adb or should it not be compiled?

Q3. What is the use of having subunits? Is it just to break a large program into smaller parts? I know an error arises if one doesn't put any statements between BEGIN and END in the subunit. So this means that one always has to put a statement there. And if one wants to write the statements later, one can always put a NULL statement between between BEGIN and END in the subunit and comes back to the latter at a later time. Is this how software engineering is done in practice?

Thanks a lot...

解决方案

Q1: That is excellent practice.

And by treating the package specification as a specification, you can provide it to other developers so that they will know how to interface to your code.

Q2: I believe that AdaGIDE actually uses the GNAT compiler for all compilation, so it's actually GNAT that is in charge of the acceptable filenames. (This can be configured, but unless you have a very compelling reason to do so, it is far simpler to simply go with GNAT/AdaGIDE's file naming conventions.) More pertinent to your question, though, there's no strong reason to include the parent unit as part of the separate unit's name. But see the answer to Q3...

Q3: Subunits were introduced with the first version of Ada--Ada 83--in part to help modularize code, and allow for deferred development and compilation. However, Ada software development practice has pretty much abandoned the use of subunits, all the procedure/function/task/etc bodies are simply all maintained in the body of the package. They are still used in some areas, like if a platform-specific version of subprogram may be needed, but for the most part they're rarely used. It leaves fewer files to keep track of, and keeps the implementation code of a package all together. So I strongly recommend you simply ignore the subunit capabilities and place all your implementation code in package bodies.

这篇关于软件工程与Ada:stubs;分离和编译单元的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆