1

It seems so simple but this does not compile:

procedure Main is
begin
   exit 1;
end Main;

When compiled with gprbuild, yields:

Compile
   [Ada]          main.adb
main.adb:3:04: cannot exit from program unit or accept statement
main.adb:3:08: missing ";"
gprbuild: *** compilation phase failed

The exit keyword in Ada clearly doesn't do what it does in other programming languages. So how do you exit from the ada main procedure with an error code?

TamaMcGlinn
  • 2,840
  • 23
  • 34
  • 1
    This seems to be related to https://stackoverflow.com/questions/28487175/how-to-stop-execution-in-my-program You might want to give a look at the answer, it uses Gnat library (so not as portable as [egilhh](https://stackoverflow.com/a/61207206/4869962) answer). – G_Zeus Apr 14 '20 at 21:10

2 Answers2

4

How about:

with Ada.Command_Line;

procedure Main is
begin
   Ada.Command_Line.Set_Exit_Status(Ada.Command_Line.Failure);
end Main;
egilhh
  • 6,464
  • 1
  • 18
  • 19
0

Make your Ada main program a function, not a procedure, and return the exit code you want:

function Main return integer is
begin
   return 1;
end Main;
TamaMcGlinn
  • 2,840
  • 23
  • 34
  • 2
    From the RM 10.2: "What happens to the result of a main function is also implementation defined." So that's not guaranteed to work for all compilers – egilhh Apr 14 '20 at 11:58
  • @egilhh presumably because not all systems expect main programs to exit returning an integer. My watch doesn't. On any combination of machine and OS where an integer return is expected, it can probably be expected to work. But I agree; use the architected mechanism where there is one. –  Apr 14 '20 at 12:03