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.
-
2possible duplicate of [Ada beginner Stack program](http://stackoverflow.com/questions/16182824/ada-beginner-stack-program) – Simon Wright Feb 20 '15 at 16:31
-
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) – Feb 20 '15 at 16:57
-
What would the declaration syntax look like? Would I have to name the file? – Dennis Garfield Feb 20 '15 at 17:01
-
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 Feb 21 '15 at 21:29
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.

- 6,733
- 17
- 22
-
2Note that "only one compilation unit per file" is **not** an Ada rule. It's a GNAT restriction. Not all Ada compilers have this restriction. – ajb Feb 21 '15 at 21:26
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
.

- 1,229
- 12
- 20
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;

- 105
- 1
- 1
- 7