comp.lang.ada
 help / color / mirror / Atom feed
* "extern" procedure in ada
@ 2002-09-27 19:40 Bernard Azria
  2002-09-28  2:28 ` Jim Rogers
                   ` (3 more replies)
  0 siblings, 4 replies; 10+ messages in thread
From: Bernard Azria @ 2002-09-27 19:40 UTC (permalink / raw)


I am looking for the equivallent of an "extern" definition procedure in
C for ADA.

In other words, how could I define an external procedure to my program
without "withing" the package where it is defined ( I have only the "obj'
and the "ali" file of this procedure in a library)

Would it be an kind of : pragma import ( ADA, procedure_name )

Thanks in advance

B.A.






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

* Re: "extern" procedure in ada
  2002-09-27 19:40 "extern" procedure in ada Bernard Azria
@ 2002-09-28  2:28 ` Jim Rogers
  2002-09-28  3:13 ` SteveD
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 10+ messages in thread
From: Jim Rogers @ 2002-09-28  2:28 UTC (permalink / raw)


Bernard Azria wrote:

> I am looking for the equivallent of an "extern" definition procedure in
> C for ADA.
> 
> In other words, how could I define an external procedure to my program
> without "withing" the package where it is defined ( I have only the "obj'
> and the "ali" file of this procedure in a library)
> 
> Would it be an kind of : pragma import ( ADA, procedure_name )
> 


You would "with" the package containing the procedure.

You seem to think that the Ada "with" mechanism is similar to the C
#include mechanism. It is not. The Ada "with" mechanism is designed to
specifically support separate compilation. You still need the package
specification to perform a "with" so that the compiler can check the
validity of your references to identifiers from the foreign package.

The ".ali" file is not helpful. It primarily specifies linker instructions.
It is a product of the GNAT compiler, not a source file used by the GNAT
compiler.

Jim Rogers






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

* Re: "extern" procedure in ada
  2002-09-27 19:40 "extern" procedure in ada Bernard Azria
  2002-09-28  2:28 ` Jim Rogers
@ 2002-09-28  3:13 ` SteveD
  2002-09-28  3:47 ` sk
  2002-09-30 14:37 ` Stephen Leake
  3 siblings, 0 replies; 10+ messages in thread
From: SteveD @ 2002-09-28  3:13 UTC (permalink / raw)


"Bernard Azria" <azria@cae.ca> wrote in message
news:an2c4e$52l$1@dns3.cae.ca...
> I am looking for the equivallent of an "extern" definition procedure in
> C for ADA.
>
> In other words, how could I define an external procedure to my program
> without "withing" the package where it is defined ( I have only the "obj'
> and the "ali" file of this procedure in a library)
>

I am still not sure I understand your question, but here is "sort of" an
answer... but I'm not sure it is to your question.

I can declare a variable in an Ada source file and "export" that variable:

package sharer is
  pragma Elaborate_Body;
end sharer;

package body sharer is

  x : integer := 42;
  pragma export( Ada, x );

end sharer;

I can then "import" that variable into another Ada source file:

with Ada.Text_io;
with Sharer;
pragma Elaborate( Sharer );
procedure shared is
  x : Integer;
  pragma import( Ada, x );
begin
  Ada.Text_Io.Put_Line( "x is " & Integer'IMAGE( x ) );
end shared;

In my example I have still With'd the "Sharer" package in so that the "x"
variable will get initialized during elaboration.  But you'll note that
"Sharer" does not explicitly make anything visible in the package spec.

I hope this helps,
SteveD

> Would it be an kind of : pragma import ( ADA, procedure_name )
>
> Thanks in advance
>
> B.A.
>
>
>





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

* Re: "extern" procedure in ada
  2002-09-27 19:40 "extern" procedure in ada Bernard Azria
  2002-09-28  2:28 ` Jim Rogers
  2002-09-28  3:13 ` SteveD
@ 2002-09-28  3:47 ` sk
  2002-09-30 14:37 ` Stephen Leake
  3 siblings, 0 replies; 10+ messages in thread
From: sk @ 2002-09-28  3:47 UTC (permalink / raw)


Hi,

> "Bernard Azria" <azria@cae.ca> ...

If you must use this technique and you use Gnat, look at 

procedure xxx is
begin
    null;
end xxx;

> gnatmake -c xxx
> gnatbind xxx

then look at b_xxx.adb file to see how your program 
("xxx" in this case) is called.

-- 
-------------------------------------
-- Merge vertically for real address
-------------------------------------
s n p @ t . o
 k i e k c c m
-------------------------------------



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

* Re: "extern" procedure in ada
  2002-09-27 19:40 "extern" procedure in ada Bernard Azria
                   ` (2 preceding siblings ...)
  2002-09-28  3:47 ` sk
@ 2002-09-30 14:37 ` Stephen Leake
  2002-09-30 22:39   ` Bernard Azria
  3 siblings, 1 reply; 10+ messages in thread
From: Stephen Leake @ 2002-09-30 14:37 UTC (permalink / raw)


"Bernard Azria" <azria@cae.ca> writes:

> I am looking for the equivallent of an "extern" definition procedure in
> C for ADA.
> 
> In other words, how could I define an external procedure to my program
> without "withing" the package where it is defined ( I have only the "obj'
> and the "ali" file of this procedure in a library)

I gather you are trying to use a library that you don't have source
for? Be careful not to violate your license to use the library.

In order to call any subprogram from Ada, you need the specification of
the subprogram; the parameter types and return type.

If you have the source code of the subprograms in Ada, you have the
subprogram specifications.

If you don't have the source code (your case), you need to write the
specifications yourself, and tell the compiler to get the bodies of
the subprogram from the library. pragma Import is precisely what you
need to use for this task.

For a library package, it is usually convenient to group the
specifications in an Ada package. The package spec contains all the
subprogram specs, and all the 'pragma Import' statements.

If you are only calling a few subprograms from one place, putting the
subprogram specifications in the same package where you are calling
them makes sense.

> Would it be an kind of : pragma import ( ADA, procedure_name )

Yes, exactly. Although, if the library .o is compiled by a different
compiler than your code, this is not guarranteed to work. You might
need your vendor to provide an "Ada_GNAT" convention.

-- 
-- Stephe



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

* Re: "extern" procedure in ada
  2002-09-30 14:37 ` Stephen Leake
@ 2002-09-30 22:39   ` Bernard Azria
  2002-10-01 19:57     ` Simon Wright
  0 siblings, 1 reply; 10+ messages in thread
From: Bernard Azria @ 2002-09-30 22:39 UTC (permalink / raw)


Thanks to all for your quick responses, It helped me a lot.

I decided to use a pragma import (ada, ..) and to ask people building
the library to define in it, the coreponsding pragma export ( ada,  .. ).
In this way, I shouldn't have to "with " any file from the library.

Thanks again, B.A.

"Stephen Leake" <stephen.a.leake.1@gsfc.nasa.gov> wrote in message
news:u7kh3o86d.fsf@gsfc.nasa.gov...
> "Bernard Azria" <azria@cae.ca> writes:
>
> > I am looking for the equivallent of an "extern" definition procedure in
> > C for ADA.
> >
> > In other words, how could I define an external procedure to my program
> > without "withing" the package where it is defined ( I have only the
"obj'
> > and the "ali" file of this procedure in a library)
>
> I gather you are trying to use a library that you don't have source
> for? Be careful not to violate your license to use the library.
>
> In order to call any subprogram from Ada, you need the specification of
> the subprogram; the parameter types and return type.
>
> If you have the source code of the subprograms in Ada, you have the
> subprogram specifications.
>
> If you don't have the source code (your case), you need to write the
> specifications yourself, and tell the compiler to get the bodies of
> the subprogram from the library. pragma Import is precisely what you
> need to use for this task.
>
> For a library package, it is usually convenient to group the
> specifications in an Ada package. The package spec contains all the
> subprogram specs, and all the 'pragma Import' statements.
>
> If you are only calling a few subprograms from one place, putting the
> subprogram specifications in the same package where you are calling
> them makes sense.
>
> > Would it be an kind of : pragma import ( ADA, procedure_name )
>
> Yes, exactly. Although, if the library .o is compiled by a different
> compiler than your code, this is not guarranteed to work. You might
> need your vendor to provide an "Ada_GNAT" convention.
>
> --
> -- Stephe





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

* Re: "extern" procedure in ada
  2002-09-30 22:39   ` Bernard Azria
@ 2002-10-01 19:57     ` Simon Wright
  2002-10-02  1:49       ` Jeffrey Carter
  0 siblings, 1 reply; 10+ messages in thread
From: Simon Wright @ 2002-10-01 19:57 UTC (permalink / raw)


"Bernard Azria" <azria@cae.ca> writes:

> I decided to use a pragma import (ada, ..) and to ask people
> building the library to define in it, the coreponsding pragma export
> ( ada, .. ).  In this way, I shouldn't have to "with " any file from
> the library.

What happens about elaboration?

I think you would have a _lot_ of trouble with GNAT if you approach
this in a naive way.

Does the library come with an entry called something_adainit or some
such, that you must call before you use any of its facilities? I would
be a bit suspicious if it doesn't.



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

* Re: "extern" procedure in ada
  2002-10-01 19:57     ` Simon Wright
@ 2002-10-02  1:49       ` Jeffrey Carter
  2002-10-02 15:13         ` Bernard Azria
  0 siblings, 1 reply; 10+ messages in thread
From: Jeffrey Carter @ 2002-10-02  1:49 UTC (permalink / raw)


"Bernard Azria" <azria@cae.ca> writes:
>I decided to use a pragma import (ada, ..) and to ask people
>building the library to define in it, the coreponsding pragma export
>( ada, .. ).  In this way, I shouldn't have to "with " any file from
>the library.

If you have contact with the people writing the library, get a 
specification file from them, with the package, and call it normally. 
That's the easiest, simplest, and safest way to do this. You should only 
try another approach if you absolutely have to.

-- 
Jeff Carter
"We use a large, vibrating egg."
Annie Hall




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

* Re: "extern" procedure in ada
  2002-10-02  1:49       ` Jeffrey Carter
@ 2002-10-02 15:13         ` Bernard Azria
  2002-10-02 18:56           ` Jeffrey Carter
  0 siblings, 1 reply; 10+ messages in thread
From: Bernard Azria @ 2002-10-02 15:13 UTC (permalink / raw)




"Jeffrey Carter" <jrcarter@acm.org> wrote in message
> If you have contact with the people writing the library, get a
> specification file from them, with the package, and call it normally.
> That's the easiest, simplest, and safest way to do this. You should only
> try another approach if you absolutely have to.
>
>
I have tried to use the spec file of the procedure i am calling in the
library, but the problem is when linking the object files, because this spec
file needs a body file ...
Using spec files twice also gives warning of "multiply defined", but it
seems
we can live with that ...
May be is there a Gnat compilation option, that can used object files of the
spec
files I am "withing", instead of the source files ?

Response to Simon:

Yes, the library is provided with an elaboration adainit procedure.






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

* Re: "extern" procedure in ada
  2002-10-02 15:13         ` Bernard Azria
@ 2002-10-02 18:56           ` Jeffrey Carter
  0 siblings, 0 replies; 10+ messages in thread
From: Jeffrey Carter @ 2002-10-02 18:56 UTC (permalink / raw)


Bernard Azria wrote:
> "Jeffrey Carter" <jrcarter@acm.org> wrote in message
> 
>>If you have contact with the people writing the library, get a
>>specification file from them, with the package, and call it normally.
>>That's the easiest, simplest, and safest way to do this. You should only
>>try another approach if you absolutely have to.
>>
>>
> 
> I have tried to use the spec file of the procedure i am calling in the
> library, but the problem is when linking the object files, because this spec
> file needs a body file ...
> Using spec files twice also gives warning of "multiply defined", but it
> seems
> we can live with that ...
> May be is there a Gnat compilation option, that can used object files of the
> spec
> files I am "withing", instead of the source files ?

With GNAT, you need the spec, the read-only .ali file, and the object 
file. When the .ali file is read only, GNAT won't worry about whether it 
has a body or do any checking to see if it needs recompilation.

-- 
Jeff Carter
"This trial is a travesty. It's a travesty of a mockery of a
sham of a mockery of a travesty of two mockeries of a sham. ...
Do you realize there's not a single homosexual on that jury?"
Bananas




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

end of thread, other threads:[~2002-10-02 18:56 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-09-27 19:40 "extern" procedure in ada Bernard Azria
2002-09-28  2:28 ` Jim Rogers
2002-09-28  3:13 ` SteveD
2002-09-28  3:47 ` sk
2002-09-30 14:37 ` Stephen Leake
2002-09-30 22:39   ` Bernard Azria
2002-10-01 19:57     ` Simon Wright
2002-10-02  1:49       ` Jeffrey Carter
2002-10-02 15:13         ` Bernard Azria
2002-10-02 18:56           ` Jeffrey Carter

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