Hello world in Ada

My favorite language is F#, but Ada is also interesting.

mkdir src
Put in this source directory the ada "main" file , hello.adb :
Code:
with Text_IO; 
use Text_IO;
procedure hello is
begin
   Put_Line("Hello world!");
end hello;

Create a gpr file , syntax is not always obvious, hello_world.gpr :
Code:
project Hello_World is
   for Source_Dirs use ("src");
   for Object_Dir use "obj";
   for Exec_Dir use "bin";
   for Main use ("hello.adb");
   package Compiler is
       for Switches ("Ada") use ("-gnat2022", "-O2","-B","-d","-g");
   end Compiler;
   package Builder is
       for Executable ("hello.adb") use "hello.exe";
   end Builder;
end Hello_World;

Execute the following script , compile_run :
Code:
export CC=clang20
export CXX=clang20++
export GCC=clang20
rm -vfR ./obj/*
rm -vfR ./bin/*
gnatmake -vl -we -P hello_world.gpr
./bin/hello.exe
 
How to do object orientation in ada :

In src directory put , counter_pkg.ads :
Code:
-- counter_pkg.ads
package Counter_Pkg is
   type Counter is private;
   function Counter_Make (X: Integer) return Counter;
   procedure Increment(C : in out Counter);
   function Get_Value(C : Counter) return Integer;

private

   type Counter is record
      Value : Integer := 0;
   end record;

end Counter_Pkg;

counter_pkg.adb :
Code:
-- counter_pkg.adb
package body Counter_Pkg is

   function Counter_Make (X: Integer) return Counter is
   c : Counter;
   begin
       c.Value:=X;
       return c;
   end Counter_Make;

   procedure Increment(C : in out Counter) is
   begin
      C.Value := C.Value + 1;
   end Increment;

   function Get_Value(C : Counter) return Integer is
   begin
      return C.Value;
   end Get_Value;

end Counter_Pkg;

hello.adb :
Code:
with Text_IO;
use  Text_IO;
with Counter_Pkg;
use  Counter_Pkg;

procedure hello is

   c : Counter := Counter_Make(5);
   i : Integer := 0;

begin
   Put_Line("Hello world!");
   c.Increment;
   c.Increment;
   i:=c.Get_Value;
   Put_Line("The value is: " & Integer'Image(i));
end hello;

Change the .gpr file and add "gnatX" flag,
Code:
package Compiler is
       for Switches ("Ada") use ("-gnat2022", "-O2","-B","-d","-g","-gnatX");
   end Compiler;
 
Back
Top