在 Ada 中如何退出带有错误代码的主程序?

How do you exit from the main procedure with an errorcode in Ada?

看起来很简单但是编译不了:

procedure Main is
begin
   exit 1;
end Main;

使用 gprbuild 编译时,产生:

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

Ada 中的 exit 关键字显然与其他编程语言不同。那么,如何退出 ada 主程序并返回错误代码?

让你的 Ada 主程序成为一个函数,而不是一个过程,return你想要的退出代码:

function Main return integer is
begin
   return 1;
end Main;

怎么样:

with Ada.Command_Line;

procedure Main is
begin
   Ada.Command_Line.Set_Exit_Status(Ada.Command_Line.Failure);
end Main;