Writing large amounts of code requires separating out the functionality into different files. This is achieved in C/C++ with include files but in Ada this is done with "Packages".
The files for the previous example programs that end in .adb
are known as Ada "body" files. A package consists of a "body" file and also an Ada "specification" file ending in .ads
which declares the functions and other things that the package makes available to other packages of the program.
If you want to add multiple functions and packages to your program then example 3 is a good starting example. If you are starting a large project then this is worth looking at to be sure you understand packages but you will want to look at GNAT Projects later to see how a big project can be managed.
Now you have your GNAT Ada Tools installed for your particular Operating System I hope you will be able to be able to do this example no matter what your OS is. There are some subtle differences.
At the moment this page is based on Linux so I hope you will have no problems translating it to use with Mac and Windows.
Open the terminal to create a directory for this example and make sure its the working directory. You type only the text shown in
[john@silver ~]$ cd ~/prog/ada
[john@silver ada]$ mkdir exa3
[john@silver ada]$ cd exa3
[john@silver exa3]$
Download the three files and save them in the exa3 directory.
This code uses Ada Strings which are fixed length strings as described above. However in this little example we have used the '#' character as a string terminator. The function stringReplaceWith() in the package p_stringy is used to do a couple of string replacements with '#' terminated strings.
So now your folder exa3 contains the three files above. Type;
[john@silver exa3]$ gnat make packdemo.adb
gcc -c packdemo.adb
gcc -c p_stringy.adb
gnatbind -x packdemo.ali
gnatlink packdemo.ali
[john@silver exa3]$
You can now see that your directory contains the following files for your Ada program;
packdemo
packdemo.adb
packdemo.ali
packdemo.o
p_stringy.adb
p_stringy.ads
p_stringy.ali
p_stringy.o
and that the file "packdemo" is an executable program. Let's execute it. Type;
[john@silver exa3]$ ./packdemo
Hello my label is computer, what is your label?##############
Hello my name is computer, what is your thingy?##############
[john@silver exa3]$
Looking at the file "packdemo.adb" you will see that it first defines the string s. It then outputs the result of replacing "name" with "label" and then outputs the result of replacing "name?" with "thingy?" using the function stringReplaceWith.
with Ada.Text_IO; use Ada.Text_IO;
with p_stringy; use p_stringy;
procedure packdemo is
s: String:="Hello my name is computer, what is your name?#sdfsdfsdfsdfsdfsdfdsfsdf";
begin
Put_Line( p_stringy.stringReplaceWith( s ,"name#","label#") );
Put_Line( p_stringy.stringReplaceWith( s ,"name?#","thingy?#") );
end packdemo;
Remember that the '#' character is used as a string terminator (a mark character to mean the end of the string of characters) in the stringReplaceWith() function, so that strings shorter than the declared length can be represented. I am not suggesting you use '#' as a terminator in serious work.
The stringReplaceWith function is defined in a seperate package called p_stringy and it is linked to packdemo by the line "with p_stringy; use p_stringy;" in just the same way as "with Ada.Text_IO; use Ada.Text_IO;" links in the input output functions like Put_Line.
If you want to run your program on other machines there may be missing libraries!
When I tried to run some code I got this error;
error while loading shared libraries: libgnarl-10.so: cannot open shared object file: No such file or directory
is the message I got. It didn't happen on the run images I get compiled on a slightly earlier version of GNAT Programming Studio on my laptop, but the desktop version of GNAT Programming Studio compiling exactly the same source code has the problem.
After attempting to install libgnarl-10 I gave up and just installed libgnat and that cured it!;
[john@silver ~]$ sudo dnf install libgnat