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 Answers
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.
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
.