From: Markus on
Hi,

I am new to Ada, learning with the book "Programming in Ada 2005" from
John Barnes. I want to build a program using the GNAT GPL compiler on
the command line. If my program resides in a single file I just call

gnat make myfun.adb,

but how do I call the compiler if I want to use ("with") packages
defined in another file? I know the include, compile and link
procedure from C programs, is it similar in Ada? What about the file
extensions .ada/.adb/.ads?

Yours
Markus
From: Ludovic Brenta on
Markus Bühren wrote on comp.lang.ada:
> I am new to Ada, learning with the book "Programming in Ada 2005" from
> John Barnes. I want to build a program using the GNAT GPL compiler on
> the command line. If my program resides in a single file I just call
>
> gnat make myfun.adb,
>
> but how do I call the compiler if I want to use ("with") packages
> defined in another file? I know the include, compile and link
> procedure from C programs, is it similar in Ada? What about the file
> extensions .ada/.adb/.ads?

*.ads: Ada Specification
*.adb: Ada Body

Each file contains exactly one compilation unit (i.e. a package, a
function, or a procedure; possibly generic). For child units, name
the file after the fully-qualified name of the unit with a dash as the
separator, e.g.

package Foo.Bar is... -> foo-bar.ads
package body Foo.Bar is... -> foo-bar.adb

You cannot place multiple compilation units in the same file.

If you follow this convention, then gnatmake does everything else for
you because contrary to C, Ada has built-in support for multiple
compilation. You do not need any Makefiles or explicit calls to the
linker. In the simple case where all your source files are in the
same directory, "gnat make myfun" will recompile all units that are
out of date, then bind them, then link them together without your
intervention. The only compilation unit that you name on the command
line is the main procedure; gnat finds all other files by following
the "with" clauses in the main procedure.

In the more complex case where you spread your source files in
multiple directories, you can either use command-line options (i.e.
gnat make -Idir) or project files to tell gnat make where your sources
are. GNAT project files also allow you to use a non-default naming
convention for your source files but this is only recommended when
migrating from another compiler. See the GNAT User's Guide for
details on both methods.

HTH and welcome to Ada!

--
Ludovic Brenta.
From: John B. Matthews on
In article
<b1f1461a-4899-437e-a20b-0a9dbf583c0d(a)n2g2000vba.googlegroups.com>,
Markus <markus.buehren(a)gmail.com> wrote:

> What about the file extensions .ada/.adb/.ads?

In addition to Ludovic Brenta's discussion of .ads and .adb, a file
ending in .ada is a convenient way to store multiple compilation units
in a single file. Such a file is typically created with the 'cat'
command. Typically, it is not directly compilable, but it can be
restored to individual units with the 'gnatchop' utility. For example,

<http://gcc.gnu.org/onlinedocs/gcc-4.3.4/gnat_ugn_unw/
Renaming-Files-Using-gnatchop.html#Renaming-Files-Using-gnatchop>

--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>
From: Markus on
Great, thanks for your kind and quick help!

Markus
From: Stephen Leake on
Markus <markus.buehren(a)gmail.com> writes:

> I am new to Ada, learning with the book "Programming in Ada 2005" from
> John Barnes. I want to build a program using the GNAT GPL compiler on
> the command line. If my program resides in a single file I just call
>
> gnat make myfun.adb,
>
> but how do I call the compiler if I want to use ("with") packages
> defined in another file? I know the include, compile and link
> procedure from C programs, is it similar in Ada? What about the file
> extensions .ada/.adb/.ads?

There is a short tutorial on compiling Ada code in multiple files as
part of the Emacs Ada mode tutorial; see
http://www.stephe-leake.org/emacs/ada-mode/emacs-ada-mode.html

--
-- Stephe