ada works . it

Previous tutorial Starting GNAT

GNAT Starting Ada

These instructions are for installing and starting programming using "GNAT" on a 64bit computer with a Linux Fedora 33 operating system.

Computer

They have been tested on a computer with a fresh operating system installation without updates of;

The computer had;

It is expected that the instructions here will work on a far lower specification machine.

The text editor that comes as default is Pluma and this was used.

Using the Terminal

Before WIMP operating systems (Windows Icons Menus Pointer) like the computer desktops on Linux, Mac and Windows computers, most computer work was done through a terminal or Teleprinter. This approach is sufficiently useful that all three WIMP operating systems have a terminal emulator where commands can be typed in and the computer can respond.

The operation of the terminal (emulator) on Linux, Mac and Windows is little different one to another.

In order to help you use the terminal the following notation is used on this website.

Text written by the computer is shown in this style such as the terminal's user prompt;

[john@silver ~]$

Text you should type at the prompt is shown in this style.

Other computer code such as programs are shown in this style.

Installing The GNAT Ada Tools

Open a terminal and the computer responds with the prompt;

[john@silver ~]$

Here the computer prompt is for a user called "john" and a computer (host) called "silver" your user name and host name will most likely be different.

Become an "sudoer"

On a fresh Fedora installation it is likely that you are not an "sudoer" and so cannot use the "sudo" command.

If you are not an sudoer or you do not know if you are, you will need to be added to the sudoers group called "wheel";

This is done with the following commands, type what is written in this style, what the computer displays is shown in this style. Note that when Password: appears you need to type the root password and press the [Enter] key on your keyboard.

[john@silver ~]$ su Password: [root@silver john]# usermod -aG wheel john [root@silver john]# exit exit [john@silver ~]$

RESTART the computer to bring the changes into effect.

Once you are an sudoer

You can do the installation with;

[john@silver ~]$ sudo dnf install gcc-gnat

Type your password and press the [Enter] key when prompted to do so. The installation, after a lot of output, should end with;

Installed: gcc-gnat-10.2.1-6.fc33.x86_64 libgnat-10.2.1-6.fc33.x86_64 libgnat-devel-10.2.1-6.fc33.x86_64 Complete! [john@silver ~]$

Note that the pluma text editor supplied with Fedora Linux will highlight the code, making it easier to read, as soon as you save a file with the .adb or .ads extension, which are the extensions for ada code.

Create a Working directory

When programming it is good to have a directory for your programming activities and one sub-directory for each language. You can create directories using the terminal. You type only the text shown in this style;

[john@silver ~]$ mkdir prog [john@silver ~]$ cd prog [john@silver prog]$ mkdir ada [john@silver prog]$ cd ada [john@silver ada]$

Now you have a working directory for all your ada programming and your Current Directory (cd) is now called "ada".

Example 1: Hello World

Open the terminal to create a directory for this example and make sure its the working directory. You type only the text shown in this style;

[john@silver ~]$ cd ~/prog/ada [john@silver ada]$ mkdir exa1 [john@silver ada]$ cd exa1 [john@silver exa1]$

Use a text editor (pluma) to type or copy the following;

with Ada.Text_IO; procedure hello is begin Ada.Text_IO.Put_Line("Hello world!"); end hello;

then save it in the exa1 directory as hello.adb You are now ready to compile it. Type;

[john@silver exa1]$ gnat make hello.adb gcc -c hello.adb gnatbind -x hello.ali gnatlink hello.ali [john@silver exa1]$

Typing ls (short for LiSt) you can now see that your working directory exa1 contains the following files for your Ada program;

[john@silver exa1]$ ls hello hello.adb hello.ali hello.o [john@silver exa1]$

and that the file "hello" is an executable program. Let's execute it. Type ./hello

[john@silver exa1]$ ./hello Hello world! [john@silver exa1]$

It executes and prints Hello world! and that ends the first example.

Example 2: What is your name?

Open the terminal to set up the command path and create a directory for this example and make sure its the working directory. You type only the text shown in this style;

[john@silver ~]$ export PATH=/opt/gcc-11.1.0/bin:$PATH [john@silver ~]$ cd ~/prog/ada [john@silver ada]$ mkdir exa2 [john@silver ada]$ cd exa2 [john@silver exa2]$

Use a text editor (pluma) to type or copy the following;

with Ada.Text_IO; procedure helloname is s: String(1..16); n: Integer; begin Ada.Text_IO.Put_Line("Hello, what is your name?"); Ada.Text_IO.Get_Line(s,n); Ada.Text_IO.Put("Hello "); Ada.Text_IO.Put(s(1..n)); Ada.Text_IO.Put(" your name is"); Ada.Text_IO.Put(n'Image); Ada.Text_IO.Put(" characters long, including spaces."); end helloname;

then save it in the exa2 directory as helloname.adb You are now ready to compile it. Type;

[john@silver exa2]$ gnat make helloname.adb gcc -c helloname.adb gnatbind -x helloname.ali gnatlink helloname.ali [john@silver exa2]$

Typing ls (short for LiSt) you can now see that your working directory exa2 contains the following files for your Ada program;

[john@silver exa1]$ ls helloname helloname.adb helloname.ali helloname.o [john@silver exa2]$

and that the file "helloname" is an executable program. Let's execute it. Type ./helloname

[john@silver exa2]$ ./helloname

The program runs printing;

Hello, what is your name?

Respond by typing your name i.e.;

John

And press press the [Enter] key. The program responds;

Hello John your name is 4 characters long, including spaces.

The way Ada handles strings of characters is different from many other languages including C. Ada's built in String type is is a way to declare an array of a certain fixed number of characters (This is much safer than C/C++ zero terminated strings) but it is necessary to use the expression s(1..n) to make sure we only output using "Put" the number of characters that were actually input using "Get".

This program can be made more readable like this;

with Ada.Text_IO; use Ada.Text_IO; procedure helloname is s: String(1..16); n: Integer; begin Put_Line("Hello, what is your name?"); Get_Line(s,n); Put("Hello "); Put(s(1..n)); Put(" your name is"); Put(n'Image); Put(" characters long, including spaces."); end helloname;

Here the addition of use Ada.Text_IO; at the start, saves keep typing Ada.Text_IO. before every Put, Put_Line or Get_Line in the program.

Next tutorial GNAT packages