comp.lang.ada
 help / color / mirror / Atom feed
* Can .ads be compiled alone?
@ 2014-11-07  5:21 moixa
  2014-11-07  6:09 ` Jeffrey Carter
                   ` (4 more replies)
  0 siblings, 5 replies; 26+ messages in thread
From: moixa @ 2014-11-07  5:21 UTC (permalink / raw)


Newbie question.

I have only two files, machinery_1_3.ads [1] and machinery_1_3.adb [2], in some directory, when I `gnat compile machinery_1_3.ads`, it told me compile error, (but no further information).

A stackoverflow post[3] said that it's a good practice to separate compile the spec and the body, so, why I cannot compile my .ads?

---------------------------

[3] http://stackoverflow.com/questions/9921794/error-when-compiling-spec-files

---------------------------

[1] machinery_1_3.ads
package Machinery_1_3 is                            -- 1 Package specification; requires body
    type Machine is private;                        -- 2 Specifies the visible part of the data type;
    procedure Turn_On (M : in out Machine);         -- 3 procedure specification
    procedure Turn_Off (M : in out Machine);        -- 4 procedure specification
    function Is_On (M : in Machine) return Boolean; -- 5 function specification
private                                             -- 6 private part hidden from a client of contract
    type Machine is record                          -- 7 full definition of the publicly declared type
        Turned_On : Boolean := False;               -- 8 component of the type; OOP attribute
    end record;                                     -- 9 scope terminator for the component
end Machinery_1_3;                                  -- 10 scope terminator for the specification

---------------------------

[2] machinery_1_3.adb
package body Machinery_1_3 is                         -- 1 Package body; implements specification declarations
    procedure Turn_On (M : in out Machine) is         -- 2 Repeat procedure specification; compiler checks this
    begin                                             -- 3 Starts algorithmic section of procedure
        M.Turned_ON := True;                          -- 4 Simple assignment statement of boolean value
    end Turn_On;                                      -- 5 Procedure scope terminator is required
    procedure Turn_Off (M : in out Machine) is        -- 6 Must match profile in specification
    begin                                             -- 7 Algorithms between begin and end
        M.Turned_On := False;                         -- 8 M.Turned called dot notation
    end Turn_Off;                                     -- 9 Name is optional but end is required
    function Is_On (M : in Machine) return Boolean is -- 10 In mode is like a constant; it may
    begin                                             -- 11 not be on left side of assignment
        return M.Turned_On;                           -- 12 return statement required of every function
    end Is_On;                                        -- 13 Scope terminator for function
end Machinery_1_3;                                    -- 14 End of all declarations for this package


^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: Can .ads be compiled alone?
  2014-11-07  5:21 Can .ads be compiled alone? moixa
@ 2014-11-07  6:09 ` Jeffrey Carter
  2014-11-07 17:26   ` Robert A Duff
  2014-11-07  6:20 ` J-P. Rosen
                   ` (3 subsequent siblings)
  4 siblings, 1 reply; 26+ messages in thread
From: Jeffrey Carter @ 2014-11-07  6:09 UTC (permalink / raw)


On 11/06/2014 10:21 PM, moixa wrote:
> 
> I have only two files, machinery_1_3.ads [1] and machinery_1_3.adb [2], in
> some directory, when I `gnat compile machinery_1_3.ads`, it told me compile
> error, (but no further information).
> 
> A stackoverflow post[3] said that it's a good practice to separate compile
> the spec and the body, so, why I cannot compile my .ads?

This is an idiosyncrasy of GNAT's source-based compilation model. When you
compile something that withs a package, GNAT also looks at the source file for
the pkg spec. So GNAT normally only compiles a pkg body; typically the message
when one tries to compile a spec that has a body is something like "cannot
produce object code for a package specification" (I haven't tried it recently).
You can check a pkg spec that has a body with -gnatc, and compile a spec that
doesn't have a body, or compile a spec along with its body by compiling the body.

Other compilers have other ways of dealing with this, and require that a pkg
spec be compiled before you can compile something that withs it.

-- 
Jeff Carter
"In the frozen land of Nador they were forced to
eat Robin's minstrels, and there was much rejoicing."
Monty Python & the Holy Grail
70



^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: Can .ads be compiled alone?
  2014-11-07  5:21 Can .ads be compiled alone? moixa
  2014-11-07  6:09 ` Jeffrey Carter
@ 2014-11-07  6:20 ` J-P. Rosen
  2014-11-07  7:48 ` Chris Moore
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 26+ messages in thread
From: J-P. Rosen @ 2014-11-07  6:20 UTC (permalink / raw)


Le 07/11/2014 06:21, moixa a écrit :
> I have only two files, machinery_1_3.ads [1] and machinery_1_3.adb
> [2], in some directory, when I `gnat compile machinery_1_3.ads`, it
> told me compile error, (but no further information).
> 
> A stackoverflow post[3] said that it's a good practice to separate
> compile the spec and the body, so, why I cannot compile my .ads?

1) Don't worry, your code is OK. If you have no error message, there is
no error.

2) Explanation:
This spurious message is an (unfortunate) consequence of the way gcc
works. The gcc program is nothing but a driver that selects a front-end
compiler according to the language, and a back-end code generator
according to the target machine. Gnat is technically the front-end for
the Ada language, the full compiler is gcc.

There is no point in generating code if there are compile errors;
therefore, if the front-end encounters any error, it returns a non-null
exit status, and this will prevent gcc from activating the back-end.

However, there is a special case in gnat (due to the so-called source
model): when you compile a package spec, no code is generated; actually,
you don't even need to compile package specs, since they are recompiled
as part of the compilation of the body (although it is good practice to
compile specs anyway, to check for errors as early as possible).

The only way for Gnat to tell gcc that the back-end should not be
launched in that case is to return a non-null exit code, which gcc
interprets as a signal of a compilation error, and triggers the message.
So, you can safely ignore it.

HTH

-- 
J-P. Rosen
Adalog
2 rue du Docteur Lombard, 92441 Issy-les-Moulineaux CEDEX
Tel: +33 1 45 29 21 52, Fax: +33 1 45 29 25 00
http://www.adalog.fr


^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: Can .ads be compiled alone?
  2014-11-07  5:21 Can .ads be compiled alone? moixa
  2014-11-07  6:09 ` Jeffrey Carter
  2014-11-07  6:20 ` J-P. Rosen
@ 2014-11-07  7:48 ` Chris Moore
  2014-11-07 14:54   ` Tero Koskinen
  2014-11-07 17:14   ` Shark8
  2014-11-07 16:50 ` Adam Beneschan
  2014-11-15 12:54 ` rriehle
  4 siblings, 2 replies; 26+ messages in thread
From: Chris Moore @ 2014-11-07  7:48 UTC (permalink / raw)


On 07/11/2014 05:21, moixa wrote:
> Newbie question.
>
> I have only two files, machinery_1_3.ads [1] and machinery_1_3.adb [2], in some directory, when I `gnat compile machinery_1_3.ads`, it told me compile error, (but no further information).
>
> A stackoverflow post[3] said that it's a good practice to separate compile the spec and the body, so, why I cannot compile my .ads?


You cannot generate code from a spec.  As the stackoverflow article 
says, its just like a header file.  So the error is just a helpful 
warning really.  The main point is that you have no other errors in your 
spec.



---
This email is free from viruses and malware because avast! Antivirus protection is active.
http://www.avast.com


^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: Can .ads be compiled alone?
  2014-11-07  7:48 ` Chris Moore
@ 2014-11-07 14:54   ` Tero Koskinen
  2014-11-07 15:49     ` Björn Lundin
  2014-11-07 16:44     ` Adam Beneschan
  2014-11-07 17:14   ` Shark8
  1 sibling, 2 replies; 26+ messages in thread
From: Tero Koskinen @ 2014-11-07 14:54 UTC (permalink / raw)


7.11.2014 9:48, Chris Moore wrote:
> You cannot generate code from a spec.

I think this isn't true anymore for Ada 2012?

For example, with tagged types and null procedures:

with A;
package B is
   type B_Type is new A.A_Type with null record;

   overriding
   procedure Hello(Object : B_Type) is null;
end B;

-Tero


^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: Can .ads be compiled alone?
  2014-11-07 14:54   ` Tero Koskinen
@ 2014-11-07 15:49     ` Björn Lundin
  2014-11-07 16:29       ` G.B.
  2014-11-07 16:29       ` G.B.
  2014-11-07 16:44     ` Adam Beneschan
  1 sibling, 2 replies; 26+ messages in thread
From: Björn Lundin @ 2014-11-07 15:49 UTC (permalink / raw)


On 2014-11-07 15:54, Tero Koskinen wrote:
> 7.11.2014 9:48, Chris Moore wrote:
>> You cannot generate code from a spec.
> 
> I think this isn't true anymore for Ada 2012?
> 

It was never true, if the body was not needed

This should compile and produce an object file

package Constants is
  Bad_Pi : constant Integer := 3;
end Constants;


--
Björn


^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: Can .ads be compiled alone?
  2014-11-07 15:49     ` Björn Lundin
@ 2014-11-07 16:29       ` G.B.
  2014-11-08 13:01         ` moixa
  2014-11-07 16:29       ` G.B.
  1 sibling, 1 reply; 26+ messages in thread
From: G.B. @ 2014-11-07 16:29 UTC (permalink / raw)


On 07.11.14 16:49, Björn Lundin wrote:
> On 2014-11-07 15:54, Tero Koskinen wrote:
>> 7.11.2014 9:48, Chris Moore wrote:
>>> You cannot generate code from a spec.
>>
>> I think this isn't true anymore for Ada 2012?
>>
>
> It was never true, if the body was not needed


As always, the

    ~*~  GNAT User's Guide  ~*~

is a helpful source of information about using GNAT.
It's free and it is included with every installation
of GNAT.

(I should offer an initial donation of $10 to a team tasked
with creating a nice video of someone presenting just its first
sections in a skilful way, on camera. People don't want to read,
even when the Guide is very well written. But they do want
to watch.)

Here is the relevant section(*):

<quote>
3.1 Compiling Programs
======================

(...)

You need _not_ compile the following files

    * the spec of a library unit which has a body

    * subunits

because they are compiled as part of compiling related units. GNAT
package specs when the corresponding body is compiled, and subunits
when the parent is compiled.

    If you attempt to compile any of these files, you will get one of the
following error messages (where FFF is the name of the file you
compiled):

      cannot generate code for file FFF (package spec)
      to check package spec, use -gnatc

      cannot generate code for file FFF (missing subunits)
      to check parent unit, use -gnatc

      cannot generate code for file FFF (subprogram spec)
      to check subprogram spec, use -gnatc

      cannot generate code for file FFF (subunit)
      to check subunit, use -gnatc

As indicated by the above error messages, if you want to submit one of
these files to the compiler to check for correct semantics without
generating code, then use the `-gnatc' switch.
</quote>

__
(*) It would be nice to see GNAT's documentation linked at
stackexchange.


^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: Can .ads be compiled alone?
  2014-11-07 15:49     ` Björn Lundin
  2014-11-07 16:29       ` G.B.
@ 2014-11-07 16:29       ` G.B.
  1 sibling, 0 replies; 26+ messages in thread
From: G.B. @ 2014-11-07 16:29 UTC (permalink / raw)


On 07.11.14 16:49, Björn Lundin wrote:
> On 2014-11-07 15:54, Tero Koskinen wrote:
>> 7.11.2014 9:48, Chris Moore wrote:
>>> You cannot generate code from a spec.
>>
>> I think this isn't true anymore for Ada 2012?
>>
>
> It was never true, if the body was not needed
>
> This should compile and produce an object file
>
> package Constants is
>    Bad_Pi : constant Integer := 3;
> end Constants;

To wit,

$ gnatmake -v -gnatl -gnatv -gnatwa constants.ads

GNATMAKE GPL 2014 (20140331)
Copyright (C) 1992-2014, Free Software Foundation, Inc.
   "constants.ali" being checked ...
   -> "constants.ali" missing.
gcc -c -gnatl -gnatv -gnatwa constants.ads

GNAT GPL 2014 (20140331)
Copyright 1992-2014, Free Software Foundation, Inc.


Compiling: constants.ads (source file time stamp: 2014-11-07 16:16:35)

      1. package Constants is
      2.    Bad_Pi : constant Integer := 3;
      3. end Constants;

  3 lines: No errors
End of compilation
$ ls -l constants.o
-rw-r--r--  1 bauhaus  admin  444  7 Nov 17:17 constants.o


^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: Can .ads be compiled alone?
  2014-11-07 14:54   ` Tero Koskinen
  2014-11-07 15:49     ` Björn Lundin
@ 2014-11-07 16:44     ` Adam Beneschan
  1 sibling, 0 replies; 26+ messages in thread
From: Adam Beneschan @ 2014-11-07 16:44 UTC (permalink / raw)


On Friday, November 7, 2014 6:54:24 AM UTC-8, Tero Koskinen wrote:
> 7.11.2014 9:48, Chris Moore wrote:
> > You cannot generate code from a spec.
> 
> I think this isn't true anymore for Ada 2012?

I don't think it was true for Ada 83.  I can compile this, and there are no warning or other messages:

    with Foo;
    pragma Elaborate(Foo);
    package Pack1 is
        X : Integer := Foo.My_Function;
    end Pack1;

If I add anything to this source (e.g. a procedure declaration) that causes a package body to be required, I do get "cannot generate code".  Your source falls into the same category; your spec doesn't require (or allow) a body, therefore there is no message.

                             -- Adam

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: Can .ads be compiled alone?
  2014-11-07  5:21 Can .ads be compiled alone? moixa
                   ` (2 preceding siblings ...)
  2014-11-07  7:48 ` Chris Moore
@ 2014-11-07 16:50 ` Adam Beneschan
  2014-11-07 17:14   ` Robert A Duff
  2014-11-15 12:54 ` rriehle
  4 siblings, 1 reply; 26+ messages in thread
From: Adam Beneschan @ 2014-11-07 16:50 UTC (permalink / raw)


On Thursday, November 6, 2014 9:21:38 PM UTC-8, moixa wrote:
> Newbie question.
> 
> I have only two files, machinery_1_3.ads [1] and machinery_1_3.adb [2], in some directory, when I `gnat compile machinery_1_3.ads`, it told me compile error, (but no further information).

Did it actually say "compile error"?  I get a very different message when I try it, and that message *is* further information.  Specifically, I get this:

  cannot generate code for file machinery_1_3.ads (package spec)
  to check package spec for errors, use -gnatc

When asking a question (either here or on StackOverflow), it's very helpful to include the exact message you're getting when asking about an error.  The error message (and/or warning message, and/or exception message, and/or stack trace) is very useful information, and I cannot understand why so many questioners (especially on StackOverflow) omit this information when asking for help.  (And, in some cases, won't provide it until people ask multiple times.)

                                -- Adam

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: Can .ads be compiled alone?
  2014-11-07 16:50 ` Adam Beneschan
@ 2014-11-07 17:14   ` Robert A Duff
  2014-11-08  2:01     ` Randy Brukardt
  0 siblings, 1 reply; 26+ messages in thread
From: Robert A Duff @ 2014-11-07 17:14 UTC (permalink / raw)


Adam Beneschan <adambeneschan@gmail.com> writes:

> On Thursday, November 6, 2014 9:21:38 PM UTC-8, moixa wrote:
>> Newbie question.
>> 
>> I have only two files, machinery_1_3.ads [1] and machinery_1_3.adb
>> [2], in some directory, when I `gnat compile machinery_1_3.ads`, it
>> told me compile error, (but no further information).
>
> Did it actually say "compile error"?  I get a very different message
> when I try it, and that message *is* further information.
> Specifically, I get this:
>
>   cannot generate code for file machinery_1_3.ads (package spec)
>   to check package spec for errors, use -gnatc

Recent versions of gnat leave out the second line above.
It's kind of pointless -- either you saw some error messages
(other than the "cannot generate code" one), or you didn't.
Either way, there's no point in then typing:

gcc -gnatc -c machinery_1_3.ads

because you already have the information you were looking for.

To the OP:  You should use gnatmake or gprbuild to compile your
programs.  That way you don't have to worry about which files
need to be compiled.

- Bob


^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: Can .ads be compiled alone?
  2014-11-07  7:48 ` Chris Moore
  2014-11-07 14:54   ` Tero Koskinen
@ 2014-11-07 17:14   ` Shark8
  2014-11-08 21:31     ` Chris Moore
  1 sibling, 1 reply; 26+ messages in thread
From: Shark8 @ 2014-11-07 17:14 UTC (permalink / raw)


On 07-Nov-14 00:48, Chris Moore wrote:
>
> You cannot generate code from a spec.

That's not /entirely/ true; in Ada 2012:

-----------------------
--  ADS_Example.ads  --
-----------------------

pragma Ada_2012;

Package ADS_Example is

    -- The stupid old reverse-a-string beginner's exercise.
    Function Reverse_String( Input : String ) return string;

private

    Function Reverse_String( Input : String ) return string is
      (case Input'Length is
          when 0 | 1  => Input,
          when others => Input(Input'Last) &
                 Reverse_String( 
Input(Input'First..Positive'Pred(Input'Last)) )
      );

End ADS_Example;


---------------
-- Main.ads  --
---------------

with
Ada.Text_IO,
ADS_Example;

Procedure Main is
   Text : constant String:= "This is a test.";
begin
    Ada.Text_IO.Put_Line( "Starting." );
    Ada.Text_IO.Put_Line( "Text:     " & Text );
    Ada.Text_IO.Put_Line( "Reversed: " & ADS_Example.Reverse_String(Text) );
    Ada.Text_IO.Put_Line( "Finished." );
end Main;

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: Can .ads be compiled alone?
  2014-11-07  6:09 ` Jeffrey Carter
@ 2014-11-07 17:26   ` Robert A Duff
  2014-11-07 17:41     ` Jeffrey Carter
  0 siblings, 1 reply; 26+ messages in thread
From: Robert A Duff @ 2014-11-07 17:26 UTC (permalink / raw)


Jeffrey Carter <spam.jrcarter.not@spam.not.acm.org> writes:

> This is an idiosyncrasy of GNAT's source-based compilation model.

Yes, that's the way GNAT works, but I don't think it's directly
related to the source-based model.  AdaMagic uses a source-based model,
but you have to compile package specs.  I don't know of any compilers
that use a non-source-based model and behave like GNAT w.r.t. package
specs, but I don't see why it couldn't be done.

>... When you
> compile something that withs a package, GNAT also looks at the source file for
> the pkg spec. So GNAT normally only compiles a pkg body; typically the message
> when one tries to compile a spec that has a body is something like "cannot
> produce object code for a package specification" (I haven't tried it recently).
> You can check a pkg spec that has a body with -gnatc,

Yes, and you can also do that withOUT -gnatc.  ;-)

>...and compile a spec that
> doesn't have a body, or compile a spec along with its body by compiling the body.
>
> Other compilers have other ways of dealing with this, and require that a pkg
> spec be compiled before you can compile something that withs it.

Yup.  The Ada RM doesn't define how compilers are invoked and so on --
that's up to the individual Ada implementation.

- Bob

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: Can .ads be compiled alone?
  2014-11-07 17:26   ` Robert A Duff
@ 2014-11-07 17:41     ` Jeffrey Carter
  0 siblings, 0 replies; 26+ messages in thread
From: Jeffrey Carter @ 2014-11-07 17:41 UTC (permalink / raw)


On 11/07/2014 10:26 AM, Robert A Duff wrote:
> 
> Yes, that's the way GNAT works, but I don't think it's directly
> related to the source-based model.  AdaMagic uses a source-based model,
> but you have to compile package specs.  I don't know of any compilers
> that use a non-source-based model and behave like GNAT w.r.t. package
> specs, but I don't see why it couldn't be done.

I'm not a compiler writer, so I bow to your superior knowledge.

-- 
Jeff Carter
"We'll make Rock Ridge think it's a chicken
that got caught in a tractor's nuts!"
Blazing Saddles
87


^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: Can .ads be compiled alone?
  2014-11-07 17:14   ` Robert A Duff
@ 2014-11-08  2:01     ` Randy Brukardt
  0 siblings, 0 replies; 26+ messages in thread
From: Randy Brukardt @ 2014-11-08  2:01 UTC (permalink / raw)


"Robert A Duff" <bobduff@shell01.TheWorld.com> wrote in message 
news:wccppczx79i.fsf@shell01.TheWorld.com...
...
>> Did it actually say "compile error"?  I get a very different message
>> when I try it, and that message *is* further information.
>> Specifically, I get this:
>>
>>   cannot generate code for file machinery_1_3.ads (package spec)
>>   to check package spec for errors, use -gnatc
>
> Recent versions of gnat leave out the second line above.
> It's kind of pointless -- either you saw some error messages
> (other than the "cannot generate code" one), or you didn't.
> Either way, there's no point in then typing:
>
> gcc -gnatc -c machinery_1_3.ads
>
> because you already have the information you were looking for.

Not quite true: it's certainly useful for the guy who's writing batch files 
for new ACATS tests and left out the -gnatc on a package specification 
(usually of a B-Test). The second part is helpful for such users. (That may 
be a community of one. ;-)

OTOH, most users should be using Gnatmake or GPRBuild to compile things and 
would never see this message either way; Ada users compiling individual 
files usually have missed the big picture. (Even for the ACATS, I use 
Gnatmake to compile ACATS C-Tests. B-Tests unfortunately have to be compiled 
individually so that the errors can be tied to a particular source unit.)

                              Randy.


^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: Can .ads be compiled alone?
  2014-11-07 16:29       ` G.B.
@ 2014-11-08 13:01         ` moixa
  2014-11-08 14:55           ` G.B.
  0 siblings, 1 reply; 26+ messages in thread
From: moixa @ 2014-11-08 13:01 UTC (permalink / raw)


G.B. Thanks for pointing out the GNAT USER'S GUIDE!! So helpful.

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: Can .ads be compiled alone?
  2014-11-08 13:01         ` moixa
@ 2014-11-08 14:55           ` G.B.
  0 siblings, 0 replies; 26+ messages in thread
From: G.B. @ 2014-11-08 14:55 UTC (permalink / raw)


moixa <moixa.dan@gmail.com> wrote:
> G.B. Thanks for pointing out the GNAT USER'S GUIDE!! So helpful.

You are welcome. Spread the word.

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: Can .ads be compiled alone?
  2014-11-07 17:14   ` Shark8
@ 2014-11-08 21:31     ` Chris Moore
  0 siblings, 0 replies; 26+ messages in thread
From: Chris Moore @ 2014-11-08 21:31 UTC (permalink / raw)


On 07/11/2014 17:14, Shark8 wrote:
> On 07-Nov-14 00:48, Chris Moore wrote:
>>
>> You cannot generate code from a spec.
>
> That's not /entirely/ true; in Ada 2012:
>

<sigh>  This is what I get for posting before I have coffee.



---
This email is free from viruses and malware because avast! Antivirus protection is active.
http://www.avast.com

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: Can .ads be compiled alone?
  2014-11-07  5:21 Can .ads be compiled alone? moixa
                   ` (3 preceding siblings ...)
  2014-11-07 16:50 ` Adam Beneschan
@ 2014-11-15 12:54 ` rriehle
  2014-11-15 19:17   ` Robert A Duff
  4 siblings, 1 reply; 26+ messages in thread
From: rriehle @ 2014-11-15 12:54 UTC (permalink / raw)


On Thursday, November 6, 2014 9:21:38 PM UTC-8, moixa wrote:
> Newbie question.
> 
> I have only two files, machinery_1_3.ads [1] and machinery_1_3.adb [2], in some directory, when I `gnat compile machinery_1_3.ads`, it told me compile error, (but no further information).
> 
> A stackoverflow post[3] said that it's a good practice to separate compile the spec and the body, so, why I cannot compile my .ads?
> 
> ---------------------------
> 
> [3] http://stackoverflow.com/questions/9921794/error-when-compiling-spec-files
> 
> ---------------------------
> 
> [1] machinery_1_3.ads
> package Machinery_1_3 is                            -- 1 Package specification; requires body
>     type Machine is private;                        -- 2 Specifies the visible part of the data type;
>     procedure Turn_On (M : in out Machine);         -- 3 procedure specification
>     procedure Turn_Off (M : in out Machine);        -- 4 procedure specification
>     function Is_On (M : in Machine) return Boolean; -- 5 function specification
> private                                             -- 6 private part hidden from a client of contract
>     type Machine is record                          -- 7 full definition of the publicly declared type
>         Turned_On : Boolean := False;               -- 8 component of the type; OOP attribute
>     end record;                                     -- 9 scope terminator for the component
> end Machinery_1_3;                                  -- 10 scope terminator for the specification
> 
> ---------------------------
> 
> [2] machinery_1_3.adb
> package body Machinery_1_3 is                         -- 1 Package body; implements specification declarations
>     procedure Turn_On (M : in out Machine) is         -- 2 Repeat procedure specification; compiler checks this
>     begin                                             -- 3 Starts algorithmic section of procedure
>         M.Turned_ON := True;                          -- 4 Simple assignment statement of boolean value
>     end Turn_On;                                      -- 5 Procedure scope terminator is required
>     procedure Turn_Off (M : in out Machine) is        -- 6 Must match profile in specification
>     begin                                             -- 7 Algorithms between begin and end
>         M.Turned_On := False;                         -- 8 M.Turned called dot notation
>     end Turn_Off;                                     -- 9 Name is optional but end is required
>     function Is_On (M : in Machine) return Boolean is -- 10 In mode is like a constant; it may
>     begin                                             -- 11 not be on left side of assignment
>         return M.Turned_On;                           -- 12 return statement required of every function
>     end Is_On;                                        -- 13 Scope terminator for function
> end Machinery_1_3;                                    -- 14 End of all declarations for this package

================================================================

The code you just posted is directly copied from Ada Distilled.   The specification will compile just fine since it is only a specification.  You must compile the specification before the body.  They should be compiled separately.   

Richard Riehle, PhD    (Author of Ada Distilled)


^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: Can .ads be compiled alone?
  2014-11-15 12:54 ` rriehle
@ 2014-11-15 19:17   ` Robert A Duff
  2014-11-15 19:19     ` Robert A Duff
                       ` (2 more replies)
  0 siblings, 3 replies; 26+ messages in thread
From: Robert A Duff @ 2014-11-15 19:17 UTC (permalink / raw)


rriehle@itu.edu writes:

> The code you just posted is directly copied from Ada Distilled.  The
> specification will compile just fine since it is only a specification.
> You must compile the specification before the body.  They should be
> compiled separately.

That depends on the compiler.  Some Ada compilers work the way you
describe above.  GNAT does not.

Not that in GNAT, there are no requirements on compilation order.

- Bob


^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: Can .ads be compiled alone?
  2014-11-15 19:17   ` Robert A Duff
@ 2014-11-15 19:19     ` Robert A Duff
  2014-11-17 15:52       ` Adam Beneschan
  2014-11-17  0:07     ` rriehle
  2014-11-24  3:16     ` rriehle
  2 siblings, 1 reply; 26+ messages in thread
From: Robert A Duff @ 2014-11-15 19:19 UTC (permalink / raw)


Robert A Duff <bobduff@shell01.TheWorld.com> writes:

> Not that in GNAT, there are no requirements on compilation order.
  ^^^
  "Note that..."

I hate that typo, because it almost negates the meaning.
And it's far from the first time I've done it.  :-(

- Bob

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: Can .ads be compiled alone?
  2014-11-15 19:17   ` Robert A Duff
  2014-11-15 19:19     ` Robert A Duff
@ 2014-11-17  0:07     ` rriehle
  2014-11-17  6:50       ` Simon Wright
  2014-11-24  3:16     ` rriehle
  2 siblings, 1 reply; 26+ messages in thread
From: rriehle @ 2014-11-17  0:07 UTC (permalink / raw)


On Saturday, November 15, 2014 11:16:50 AM UTC-8, Robert A Duff wrote:
> rriehle@itu.edu writes:
> 
> > The code you just posted is directly copied from Ada Distilled.  The
> > specification will compile just fine since it is only a specification.
> > You must compile the specification before the body.  They should be
> > compiled separately.
> 
> That depends on the compiler.  Some Ada compilers work the way you
> describe above.  GNAT does not.
> 
> Not that in GNAT, there are no requirements on compilation order.
> 
> - Bob

The code, in the example, which I originally published in my book, Ada Distilled, was compiled with GNAT.   I first compiled the specification, and then the body.   It has always been my practice to create the specification before completing the code for the body.   In fact, in the Machinery example, which is used only for pedagogic purposes, was written with the subprograms in the body stubbed-out with simply Put statements, the tested with a separate subprogram, and them finished with the completed code in the body.  I did this with all the coded examples in Ada Distilled so I could be sure that every bit of sample code would actually execute if entered exactly as I showed it in the book.   

In a future edition of Ada Distilled, in which I am collaborating with Ed Colbert, we will add examples of pre- and post-conditions, as well as other newer features of Ada 2012.


^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: Can .ads be compiled alone?
  2014-11-17  0:07     ` rriehle
@ 2014-11-17  6:50       ` Simon Wright
  0 siblings, 0 replies; 26+ messages in thread
From: Simon Wright @ 2014-11-17  6:50 UTC (permalink / raw)


rriehle@itu.edu writes:

> The code, in the example, which I originally published in my book, Ada
> Distilled, was compiled with GNAT.  I first compiled the
> specification, and then the body.

The trouble is that if you use GNAT to compile a spec that needs a body
you get

   $ gnatmake -c -u -f driver.ads
   gcc -c driver.ads
   cannot generate code for file driver.ads (package spec)
   gnatmake: "driver.ads" compilation error

and this confuses a *lot* of new users.

The latest Emacs ada-mode doesn't offer a 'compile current file'
option. Instead, -gnatc (Check syntax and semantics only (no code
generation)) is used (bound to C-c C-v), and is extremely effective.


^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: Can .ads be compiled alone?
  2014-11-15 19:19     ` Robert A Duff
@ 2014-11-17 15:52       ` Adam Beneschan
  0 siblings, 0 replies; 26+ messages in thread
From: Adam Beneschan @ 2014-11-17 15:52 UTC (permalink / raw)


On Saturday, November 15, 2014 11:18:52 AM UTC-8, Robert A Duff wrote:
> 
> > Not that in GNAT, there are no requirements on compilation order.
>   ^^^
>   "Note that..."
> 
> I hate that typo, because it almost negates the meaning.
> And it's far from the first time I've done it.  :-(

The one that bugs me the most is substituting "now" for "not", which also negates the meaning.  I've seen this mistake many times (and I'm sure I've done it many times), but I'm not sure why it happens so much, since "w" and "t" aren't next to each other.

                            -- Adam


^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: Can .ads be compiled alone?
  2014-11-15 19:17   ` Robert A Duff
  2014-11-15 19:19     ` Robert A Duff
  2014-11-17  0:07     ` rriehle
@ 2014-11-24  3:16     ` rriehle
  2014-11-24  6:34       ` Jeffrey Carter
  2 siblings, 1 reply; 26+ messages in thread
From: rriehle @ 2014-11-24  3:16 UTC (permalink / raw)


On Saturday, November 15, 2014 11:16:50 AM UTC-8, Robert A Duff wrote:
> rriehle@itu.edu writes:
> 
> > The code you just posted is directly copied from Ada Distilled.  The
> > specification will compile just fine since it is only a specification.
> > You must compile the specification before the body.  They should be
> > compiled separately.
> 
> That depends on the compiler.  Some Ada compilers work the way you
> describe above.  GNAT does not.
> 
> Not that in GNAT, there are no requirements on compilation order.
> 
> - Bob
All the code in Ada Distilled was compiled with GNAT.  Much of it used AdaGide for the editor.

Richard

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: Can .ads be compiled alone?
  2014-11-24  3:16     ` rriehle
@ 2014-11-24  6:34       ` Jeffrey Carter
  0 siblings, 0 replies; 26+ messages in thread
From: Jeffrey Carter @ 2014-11-24  6:34 UTC (permalink / raw)


On 11/23/2014 08:16 PM, rriehle@itu.edu wrote:
> On Saturday, November 15, 2014 11:16:50 AM UTC-8, Robert A Duff wrote:>>
>> rriehle@itu.edu writes:
>> 
>>> The code you just posted is directly copied from Ada Distilled.  The
>>> specification will compile just fine since it is only a specification.
>>> You must compile the specification before the body.  They should be
>>> compiled separately.
>>
>> That depends on the compiler.  Some Ada compilers work the way you
>> describe above.  GNAT does not.
>>
>> Not[e] that in GNAT, there are no requirements on compilation order.
>>
> All the code in Ada Distilled was compiled with GNAT.  Much of it used AdaGide for the editor.

I think you two are talking at cross purposes. I'm sure Riehle did pass all of
his specs through the compiler separately from their bodies. This can be done
with the -gnatc switch, and does not generate any object code (or it can be done
without that switch, in which case it emits an error msg about not being able to
generate code for a spec). I suspect that he calls this "compiling the spec". I
suspect that Duff calls it syntax checking, and reserves "compiling" for
generating object code.

Let me know if I'm anywhere near the mark.

-- 
Jeff Carter
"I'm particularly glad that these lovely children were
here today to hear that speech. Not only was it authentic
frontier gibberish, it expressed a courage little seen
in this day and age."
Blazing Saddles
88


^ permalink raw reply	[flat|nested] 26+ messages in thread

end of thread, other threads:[~2014-11-24  6:34 UTC | newest]

Thread overview: 26+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-11-07  5:21 Can .ads be compiled alone? moixa
2014-11-07  6:09 ` Jeffrey Carter
2014-11-07 17:26   ` Robert A Duff
2014-11-07 17:41     ` Jeffrey Carter
2014-11-07  6:20 ` J-P. Rosen
2014-11-07  7:48 ` Chris Moore
2014-11-07 14:54   ` Tero Koskinen
2014-11-07 15:49     ` Björn Lundin
2014-11-07 16:29       ` G.B.
2014-11-08 13:01         ` moixa
2014-11-08 14:55           ` G.B.
2014-11-07 16:29       ` G.B.
2014-11-07 16:44     ` Adam Beneschan
2014-11-07 17:14   ` Shark8
2014-11-08 21:31     ` Chris Moore
2014-11-07 16:50 ` Adam Beneschan
2014-11-07 17:14   ` Robert A Duff
2014-11-08  2:01     ` Randy Brukardt
2014-11-15 12:54 ` rriehle
2014-11-15 19:17   ` Robert A Duff
2014-11-15 19:19     ` Robert A Duff
2014-11-17 15:52       ` Adam Beneschan
2014-11-17  0:07     ` rriehle
2014-11-17  6:50       ` Simon Wright
2014-11-24  3:16     ` rriehle
2014-11-24  6:34       ` Jeffrey Carter

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox