1
votes

I have an ada program that has a main procedure, now I want to add another procedure but I got an error saying "end of file expected, file can have only one compilation unit". I did some looking an I think it is because you can only have 1 procedure per file. Do I have to create another file and put the procedure alone in that? If so how would I compile both the codes and run it? Can someone show me how I would be able to compile both and run the whole file together.

3
possible duplicate of Ada beginner Stack programSimon Wright
Simon's comment is bang on. But if one procedure calls the other, you can simply declare the other local to the first (i.e. in its declaration region)user_1818839
What would the declaration syntax look like? Would I have to name the file?Dennis Garfield
The answer to the question "how would I compile both the codes and run it" depends on what compiler you're using. I assume you're using GNAT, but if not please let us know what compiler you're using.ajb

3 Answers

0
votes

As the compiler says, you can only have one compilation unit per file. A main program is compilation unit, which is a procedure.

If you want one program to run two procedures which both are compilation units, when you can do it like this:

with One_Procedure,
     Another_Procedure;
procedure Sequential is
begin
   One_Procedure;
   Another_Procedure;
end Sequential;

If you want to run the two procedures in parallel do it like this:

with One_Procedure,
     Another_Procedure;
procedure Parallel is
   task One;
   task Another;

   task body One is
   begin
      One_Procedure;
   end One;

   task body Another is
   begin
      Another_Procedure;
   end Another;
begin
   null;
end Parallel;

The procedures may of course also be declared in the declarative region of the main program or in some packages.

0
votes

Both recent GNATs and GPRbuild have options for indicating which units of a file you want compiled: that's -gnateINNN for gcc, and -eInn for gprbuild, as documented here.

Another option is to become familiar with gnatchop for extracting compilation units from files, and with -m for minimal recompilation; the latter prevents having to compile the world only because of running gnatchop when an edit has not "semantically" touched all compilation units in a file. GNAT then ignores time stamps. I sometimes run commands like

gnatchop -r -w -c allofit.ada && gnatmake -Ptest -m someunit.adb

where someunit.adb is generated for compilation unit Someunit (a procedure, a package) contained in file allofit.ada.

0
votes

You can have 1 main procedure but several procedures within main procedure.

procedure main is
    ...text...

    procedure sub1 () is
    begin
        ...text...
    end sub1;

    procedure sub2 () is
    begin
        ...text...
    end sub2;

    ...text...
end main;