How to create a .ads file in Ada?
(Ada language) I would like to create a .ads file for the specifications and to associate to it a .adb file for the body of the programm. Is it possible? And how? Thank you in advance.
1 Answer
Yes, it is possible to create an Ada program with a separate specification and body file. Here are the steps to do it:
- Create a new file with the extension .ads and write the program specifications in it. For example, let's say you want to create a package named MyPackage with a procedure named MyProcedure. Here's how you can write the specifications in a file named mypackage.ads:
package MyPackage is
procedure MyProcedure;
end MyPackage;
- Create a new file with the extension .adb and write the program body in it. For example, let's say you want to implement the MyProcedure procedure. Here's how you can write the body in a file named mypackage.adb:
with Ada.Text_IO; -- import the Ada.Text_IO package
package body MyPackage is -- implementation of the package
procedure MyProcedure is
begin
Ada.Text_IO.Put_Line("Hello, world!"); -- print a message to the console
end MyProcedure;
end MyPackage;
Note that the with clause is used to import the Ada.Text_IO package, which is used in the implementation of MyProcedure.
3.Make sure both files are in the same directory.
4. To compile and run the program, you can use the gnatmake command. Open a terminal or command prompt and navigate to the directory where the .ads and .adb files are located. Then, type the following command:
gnatmake mypackage.ad[sb]
This will compile the program and generate an executable file named mypackage. You can then run the program by typing:
./mypackage
This will execute the program and print the message "Hello, world!" to the console.