comp.lang.ada
 help / color / mirror / Atom feed
* Rational Apex Ada - Pragma interface with C++
@ 2002-10-10 17:20 John Smith
  2002-10-10 19:32 ` Stephen Leake
                   ` (2 more replies)
  0 siblings, 3 replies; 10+ messages in thread
From: John Smith @ 2002-10-10 17:20 UTC (permalink / raw)


Does anyone know how to use pragma interface for interfacing C++ functions
to an Ada program.  I know how to do it with C, but can't seem to get it to
work with C++.

Thanks..





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

* Re: Rational Apex Ada - Pragma interface with C++
  2002-10-10 17:20 Rational Apex Ada - Pragma interface with C++ John Smith
@ 2002-10-10 19:32 ` Stephen Leake
  2002-10-10 21:47 ` Matthew Heaney
  2002-10-11 11:12 ` Marc A. Criley
  2 siblings, 0 replies; 10+ messages in thread
From: Stephen Leake @ 2002-10-10 19:32 UTC (permalink / raw)


"John Smith" <someone@microsoft.com> writes:

> Does anyone know how to use pragma interface for interfacing C++ functions
> to an Ada program.  I know how to do it with C, but can't seem to get it to
> work with C++.

The Ada 95 standard does not define how to interface with C++. 

Your compiler may define this on its own; you'll have to consult the
compiler docs.

-- 
-- Stephe



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

* Re: Rational Apex Ada - Pragma interface with C++
  2002-10-10 17:20 Rational Apex Ada - Pragma interface with C++ John Smith
  2002-10-10 19:32 ` Stephen Leake
@ 2002-10-10 21:47 ` Matthew Heaney
  2002-10-14 16:41   ` John Smith
  2002-10-14 23:03   ` John Smith
  2002-10-11 11:12 ` Marc A. Criley
  2 siblings, 2 replies; 10+ messages in thread
From: Matthew Heaney @ 2002-10-10 21:47 UTC (permalink / raw)



"John Smith" <someone@microsoft.com> wrote in message
news:H3s05H.LMt@news.boeing.com...
> Does anyone know how to use pragma interface for interfacing C++ functions
> to an Ada program.  I know how to do it with C, but can't seem to get it
to
> work with C++.

Interfacing with C++ requires care, especially if you have static variables
the require elaboration-time initialization.

When a C++ program loads, all the statics get initialized.  There will be
some code inserted by the compiler to go this, just prior to call the C++
main.

The same is true of an Ada program.

In general, a C++ program will need a C++ main, and an Ada program will need
an Ada main.

If you don't have any statics, you may OK.

There's no official function you can call from an Ada program to perform the
C++ initialization -- you'll have to check your compiler docs.

There are official functions to perform Ada initialization -- there are two
linker symbols called "adainit" and "adafinal".  If you have a C++ main, and
you're calling code written in Ada, then you'll need to call these.

In any event, there's no pragma to interface with C++ directly.  I know GNAT
has compiler-specific pragmas to do this.

However, I wouldn't bother.  Just create a C wrapper, and link to those.
For example, if you have a C++ class you want to use from Ada, like this:

class C
{
public:
   void f();
//...
};

Assuming you don't mind manipulating a pointer, you can do this:

extern "C" C* makeC()
{
   return new C;
}

extern "C" void freeC(C* p)
{
   delete p;
}

extern "C" void c_f(C* p)
{
   p->f();
}

On the Ada side, create a set of bindings for each of the external functions
above, e.g.

function Make_C return System.Address;
pragma Interface (C, Make_C);

procedure Free_C (Object : System.Address);
pragma Interface (C, Free_C);

procedure C_F (Object : System.Address);
pragma Interface (C, C_F);

You could add more type safety than I've shown above, e.g.

package C_Types is

   type C_Type (<>) is limited private;

   type C_Access is access all C_Type;
   pragma Convention (C, C_Access);

   function New_C return C_Access;

   procedure Free (C : in out C_Access);

   procedure Do_F (C : access C_Type);

private

   type C_Type is null record;

   function Make_C return C_Access;
   pragma Interface (C, Make_C);

  --etc
end C_Types;







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

* Re: Rational Apex Ada - Pragma interface with C++
  2002-10-10 17:20 Rational Apex Ada - Pragma interface with C++ John Smith
  2002-10-10 19:32 ` Stephen Leake
  2002-10-10 21:47 ` Matthew Heaney
@ 2002-10-11 11:12 ` Marc A. Criley
  2002-10-14 16:28   ` John Smith
  2 siblings, 1 reply; 10+ messages in thread
From: Marc A. Criley @ 2002-10-11 11:12 UTC (permalink / raw)


John Smith wrote:
> 
> Does anyone know how to use pragma interface for interfacing C++ functions
> to an Ada program.  I know how to do it with C, but can't seem to get it to
> work with C++.

There are detailed instructions in the Apex documentation on how to do
this,
and they do work.

The part that isn't mentioned, though, is that the C++ _must_ be
compiled
with Apex C++. Otherwise the run-time environments conflict, and the
resulting executable runs unreliably, if at all, which was our
experience.
This rather significant restriction was stated by a Rational
representative.

Originally, the C++ code we had to link in was compiled with Sun's Forte
C++, and we were fortunate that the nature of the code was highly
algorithmic and portable, so compiling it with Apex C++ went quite
smoothly.

Marc A. Criley
Quadrus Corporation
www.quadruscorp.com



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

* Re: Rational Apex Ada - Pragma interface with C++
  2002-10-11 11:12 ` Marc A. Criley
@ 2002-10-14 16:28   ` John Smith
  0 siblings, 0 replies; 10+ messages in thread
From: John Smith @ 2002-10-14 16:28 UTC (permalink / raw)


Thank you for your input.  Unfortunately I don't have the Apex C++ compiler
installed.  Thanks.


"Marc A. Criley" <mcq95@earthlink.net> wrote in message
news:3DA6B3DA.98A0240E@earthlink.net...
> John Smith wrote:
> >
> > Does anyone know how to use pragma interface for interfacing C++
functions
> > to an Ada program.  I know how to do it with C, but can't seem to get it
to
> > work with C++.
>
> There are detailed instructions in the Apex documentation on how to do
> this,
> and they do work.
>
> The part that isn't mentioned, though, is that the C++ _must_ be
> compiled
> with Apex C++. Otherwise the run-time environments conflict, and the
> resulting executable runs unreliably, if at all, which was our
> experience.
> This rather significant restriction was stated by a Rational
> representative.
>
> Originally, the C++ code we had to link in was compiled with Sun's Forte
> C++, and we were fortunate that the nature of the code was highly
> algorithmic and portable, so compiling it with Apex C++ went quite
> smoothly.
>
> Marc A. Criley
> Quadrus Corporation
> www.quadruscorp.com





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

* Re: Rational Apex Ada - Pragma interface with C++
  2002-10-10 21:47 ` Matthew Heaney
@ 2002-10-14 16:41   ` John Smith
  2002-10-14 23:03   ` John Smith
  1 sibling, 0 replies; 10+ messages in thread
From: John Smith @ 2002-10-14 16:41 UTC (permalink / raw)


Thank you.  I will give this a try.

John Smith
"Matthew Heaney" <mheaney@on2.com> wrote in message
news:uqbtbu6rtkk7b@corp.supernews.com...
>
> "John Smith" <someone@microsoft.com> wrote in message
> news:H3s05H.LMt@news.boeing.com...
> > Does anyone know how to use pragma interface for interfacing C++
functions
> > to an Ada program.  I know how to do it with C, but can't seem to get it
> to
> > work with C++.
>
> Interfacing with C++ requires care, especially if you have static
variables
> the require elaboration-time initialization.
>
> When a C++ program loads, all the statics get initialized.  There will be
> some code inserted by the compiler to go this, just prior to call the C++
> main.
>
> The same is true of an Ada program.
>
> In general, a C++ program will need a C++ main, and an Ada program will
need
> an Ada main.
>
> If you don't have any statics, you may OK.
>
> There's no official function you can call from an Ada program to perform
the
> C++ initialization -- you'll have to check your compiler docs.
>
> There are official functions to perform Ada initialization -- there are
two
> linker symbols called "adainit" and "adafinal".  If you have a C++ main,
and
> you're calling code written in Ada, then you'll need to call these.
>
> In any event, there's no pragma to interface with C++ directly.  I know
GNAT
> has compiler-specific pragmas to do this.
>
> However, I wouldn't bother.  Just create a C wrapper, and link to those.
> For example, if you have a C++ class you want to use from Ada, like this:
>
> class C
> {
> public:
>    void f();
> //...
> };
>
> Assuming you don't mind manipulating a pointer, you can do this:
>
> extern "C" C* makeC()
> {
>    return new C;
> }
>
> extern "C" void freeC(C* p)
> {
>    delete p;
> }
>
> extern "C" void c_f(C* p)
> {
>    p->f();
> }
>
> On the Ada side, create a set of bindings for each of the external
functions
> above, e.g.
>
> function Make_C return System.Address;
> pragma Interface (C, Make_C);
>
> procedure Free_C (Object : System.Address);
> pragma Interface (C, Free_C);
>
> procedure C_F (Object : System.Address);
> pragma Interface (C, C_F);
>
> You could add more type safety than I've shown above, e.g.
>
> package C_Types is
>
>    type C_Type (<>) is limited private;
>
>    type C_Access is access all C_Type;
>    pragma Convention (C, C_Access);
>
>    function New_C return C_Access;
>
>    procedure Free (C : in out C_Access);
>
>    procedure Do_F (C : access C_Type);
>
> private
>
>    type C_Type is null record;
>
>    function Make_C return C_Access;
>    pragma Interface (C, Make_C);
>
>   --etc
> end C_Types;
>
>
>
>





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

* Re: Rational Apex Ada - Pragma interface with C++
  2002-10-10 21:47 ` Matthew Heaney
  2002-10-14 16:41   ` John Smith
@ 2002-10-14 23:03   ` John Smith
  2002-10-15  0:12     ` Jeffrey Creem
  1 sibling, 1 reply; 10+ messages in thread
From: John Smith @ 2002-10-14 23:03 UTC (permalink / raw)


Hello Matthew,

I just tried your suggestion, but I don't quite know how to link it.  I
compiled the C++ module that contains the externs with the CC compiler from
Sun OS and compiled the C code that interfaces with the C++ module with cc.
Now my problem is how do I link both together?  I've generated two object
files, but then I'm stuck.  Help please.

John


"Matthew Heaney" <mheaney@on2.com> wrote in message
news:uqbtbu6rtkk7b@corp.supernews.com...
>
> "John Smith" <someone@microsoft.com> wrote in message
> news:H3s05H.LMt@news.boeing.com...
> > Does anyone know how to use pragma interface for interfacing C++
functions
> > to an Ada program.  I know how to do it with C, but can't seem to get it
> to
> > work with C++.
>
> Interfacing with C++ requires care, especially if you have static
variables
> the require elaboration-time initialization.
>
> When a C++ program loads, all the statics get initialized.  There will be
> some code inserted by the compiler to go this, just prior to call the C++
> main.
>
> The same is true of an Ada program.
>
> In general, a C++ program will need a C++ main, and an Ada program will
need
> an Ada main.
>
> If you don't have any statics, you may OK.
>
> There's no official function you can call from an Ada program to perform
the
> C++ initialization -- you'll have to check your compiler docs.
>
> There are official functions to perform Ada initialization -- there are
two
> linker symbols called "adainit" and "adafinal".  If you have a C++ main,
and
> you're calling code written in Ada, then you'll need to call these.
>
> In any event, there's no pragma to interface with C++ directly.  I know
GNAT
> has compiler-specific pragmas to do this.
>
> However, I wouldn't bother.  Just create a C wrapper, and link to those.
> For example, if you have a C++ class you want to use from Ada, like this:
>
> class C
> {
> public:
>    void f();
> //...
> };
>
> Assuming you don't mind manipulating a pointer, you can do this:
>
> extern "C" C* makeC()
> {
>    return new C;
> }
>
> extern "C" void freeC(C* p)
> {
>    delete p;
> }
>
> extern "C" void c_f(C* p)
> {
>    p->f();
> }
>
> On the Ada side, create a set of bindings for each of the external
functions
> above, e.g.
>
> function Make_C return System.Address;
> pragma Interface (C, Make_C);
>
> procedure Free_C (Object : System.Address);
> pragma Interface (C, Free_C);
>
> procedure C_F (Object : System.Address);
> pragma Interface (C, C_F);
>
> You could add more type safety than I've shown above, e.g.
>
> package C_Types is
>
>    type C_Type (<>) is limited private;
>
>    type C_Access is access all C_Type;
>    pragma Convention (C, C_Access);
>
>    function New_C return C_Access;
>
>    procedure Free (C : in out C_Access);
>
>    procedure Do_F (C : access C_Type);
>
> private
>
>    type C_Type is null record;
>
>    function Make_C return C_Access;
>    pragma Interface (C, Make_C);
>
>   --etc
> end C_Types;
>
>
>
>





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

* Re: Rational Apex Ada - Pragma interface with C++
  2002-10-14 23:03   ` John Smith
@ 2002-10-15  0:12     ` Jeffrey Creem
  2002-10-15  1:08       ` John Smith
  0 siblings, 1 reply; 10+ messages in thread
From: Jeffrey Creem @ 2002-10-15  0:12 UTC (permalink / raw)


Look for the view switch "NON_ADA_LINKAGE:"
Add the .o files from the c compile to this switch.
"John Smith" <someone@microsoft.com> wrote in message
news:H3zuq4.G8y@news.boeing.com...
> Hello Matthew,
>
> I just tried your suggestion, but I don't quite know how to link it.  I
> compiled the C++ module that contains the externs with the CC compiler
from
> Sun OS and compiled the C code that interfaces with the C++ module with
cc.
> Now my problem is how do I link both together?  I've generated two object
> files, but then I'm stuck.  Help please.
>
> John
>
>
> "Matthew Heaney" <mheaney@on2.com> wrote in message
> news:uqbtbu6rtkk7b@corp.supernews.com...
> >
> > "John Smith" <someone@microsoft.com> wrote in message
> > news:H3s05H.LMt@news.boeing.com...
> > > Does anyone know how to use pragma interface for interfacing C++
> functions
> > > to an Ada program.  I know how to do it with C, but can't seem to get
it
> > to
> > > work with C++.
> >
> > Interfacing with C++ requires care, especially if you have static
> variables
> > the require elaboration-time initialization.
> >
> > When a C++ program loads, all the statics get initialized.  There will
be
> > some code inserted by the compiler to go this, just prior to call the
C++
> > main.
> >
> > The same is true of an Ada program.
> >
> > In general, a C++ program will need a C++ main, and an Ada program will
> need
> > an Ada main.
> >
> > If you don't have any statics, you may OK.
> >
> > There's no official function you can call from an Ada program to perform
> the
> > C++ initialization -- you'll have to check your compiler docs.
> >
> > There are official functions to perform Ada initialization -- there are
> two
> > linker symbols called "adainit" and "adafinal".  If you have a C++ main,
> and
> > you're calling code written in Ada, then you'll need to call these.
> >
> > In any event, there's no pragma to interface with C++ directly.  I know
> GNAT
> > has compiler-specific pragmas to do this.
> >
> > However, I wouldn't bother.  Just create a C wrapper, and link to those.
> > For example, if you have a C++ class you want to use from Ada, like
this:
> >
> > class C
> > {
> > public:
> >    void f();
> > //...
> > };
> >
> > Assuming you don't mind manipulating a pointer, you can do this:
> >
> > extern "C" C* makeC()
> > {
> >    return new C;
> > }
> >
> > extern "C" void freeC(C* p)
> > {
> >    delete p;
> > }
> >
> > extern "C" void c_f(C* p)
> > {
> >    p->f();
> > }
> >
> > On the Ada side, create a set of bindings for each of the external
> functions
> > above, e.g.
> >
> > function Make_C return System.Address;
> > pragma Interface (C, Make_C);
> >
> > procedure Free_C (Object : System.Address);
> > pragma Interface (C, Free_C);
> >
> > procedure C_F (Object : System.Address);
> > pragma Interface (C, C_F);
> >
> > You could add more type safety than I've shown above, e.g.
> >
> > package C_Types is
> >
> >    type C_Type (<>) is limited private;
> >
> >    type C_Access is access all C_Type;
> >    pragma Convention (C, C_Access);
> >
> >    function New_C return C_Access;
> >
> >    procedure Free (C : in out C_Access);
> >
> >    procedure Do_F (C : access C_Type);
> >
> > private
> >
> >    type C_Type is null record;
> >
> >    function Make_C return C_Access;
> >    pragma Interface (C, Make_C);
> >
> >   --etc
> > end C_Types;
> >
> >
> >
> >
>
>





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

* Re: Rational Apex Ada - Pragma interface with C++
  2002-10-15  0:12     ` Jeffrey Creem
@ 2002-10-15  1:08       ` John Smith
  2002-10-15 13:31         ` Ted Dennison
  0 siblings, 1 reply; 10+ messages in thread
From: John Smith @ 2002-10-15  1:08 UTC (permalink / raw)


It doesn't seem to like that idea.  It's complaining about the C++ obect
file.


"Jeffrey Creem" <jeff@thecreems.com> wrote in message
news:Z3Jq9.48415$oq6.12550@sccrnsc01...
> Look for the view switch "NON_ADA_LINKAGE:"
> Add the .o files from the c compile to this switch.
> "John Smith" <someone@microsoft.com> wrote in message
> news:H3zuq4.G8y@news.boeing.com...
> > Hello Matthew,
> >
> > I just tried your suggestion, but I don't quite know how to link it.  I
> > compiled the C++ module that contains the externs with the CC compiler
> from
> > Sun OS and compiled the C code that interfaces with the C++ module with
> cc.
> > Now my problem is how do I link both together?  I've generated two
object
> > files, but then I'm stuck.  Help please.
> >
> > John
> >
> >
> > "Matthew Heaney" <mheaney@on2.com> wrote in message
> > news:uqbtbu6rtkk7b@corp.supernews.com...
> > >
> > > "John Smith" <someone@microsoft.com> wrote in message
> > > news:H3s05H.LMt@news.boeing.com...
> > > > Does anyone know how to use pragma interface for interfacing C++
> > functions
> > > > to an Ada program.  I know how to do it with C, but can't seem to
get
> it
> > > to
> > > > work with C++.
> > >
> > > Interfacing with C++ requires care, especially if you have static
> > variables
> > > the require elaboration-time initialization.
> > >
> > > When a C++ program loads, all the statics get initialized.  There will
> be
> > > some code inserted by the compiler to go this, just prior to call the
> C++
> > > main.
> > >
> > > The same is true of an Ada program.
> > >
> > > In general, a C++ program will need a C++ main, and an Ada program
will
> > need
> > > an Ada main.
> > >
> > > If you don't have any statics, you may OK.
> > >
> > > There's no official function you can call from an Ada program to
perform
> > the
> > > C++ initialization -- you'll have to check your compiler docs.
> > >
> > > There are official functions to perform Ada initialization -- there
are
> > two
> > > linker symbols called "adainit" and "adafinal".  If you have a C++
main,
> > and
> > > you're calling code written in Ada, then you'll need to call these.
> > >
> > > In any event, there's no pragma to interface with C++ directly.  I
know
> > GNAT
> > > has compiler-specific pragmas to do this.
> > >
> > > However, I wouldn't bother.  Just create a C wrapper, and link to
those.
> > > For example, if you have a C++ class you want to use from Ada, like
> this:
> > >
> > > class C
> > > {
> > > public:
> > >    void f();
> > > //...
> > > };
> > >
> > > Assuming you don't mind manipulating a pointer, you can do this:
> > >
> > > extern "C" C* makeC()
> > > {
> > >    return new C;
> > > }
> > >
> > > extern "C" void freeC(C* p)
> > > {
> > >    delete p;
> > > }
> > >
> > > extern "C" void c_f(C* p)
> > > {
> > >    p->f();
> > > }
> > >
> > > On the Ada side, create a set of bindings for each of the external
> > functions
> > > above, e.g.
> > >
> > > function Make_C return System.Address;
> > > pragma Interface (C, Make_C);
> > >
> > > procedure Free_C (Object : System.Address);
> > > pragma Interface (C, Free_C);
> > >
> > > procedure C_F (Object : System.Address);
> > > pragma Interface (C, C_F);
> > >
> > > You could add more type safety than I've shown above, e.g.
> > >
> > > package C_Types is
> > >
> > >    type C_Type (<>) is limited private;
> > >
> > >    type C_Access is access all C_Type;
> > >    pragma Convention (C, C_Access);
> > >
> > >    function New_C return C_Access;
> > >
> > >    procedure Free (C : in out C_Access);
> > >
> > >    procedure Do_F (C : access C_Type);
> > >
> > > private
> > >
> > >    type C_Type is null record;
> > >
> > >    function Make_C return C_Access;
> > >    pragma Interface (C, Make_C);
> > >
> > >   --etc
> > > end C_Types;
> > >
> > >
> > >
> > >
> >
> >
>
>





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

* Re: Rational Apex Ada - Pragma interface with C++
  2002-10-15  1:08       ` John Smith
@ 2002-10-15 13:31         ` Ted Dennison
  0 siblings, 0 replies; 10+ messages in thread
From: Ted Dennison @ 2002-10-15 13:31 UTC (permalink / raw)


"John Smith" <someone@microsoft.com> wrote in message news:<H400I6.1uL@news.boeing.com>...
> It doesn't seem to like that idea.  It's complaining about the C++ obect
> file.

Smart compiler. :-)



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

end of thread, other threads:[~2002-10-15 13:31 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-10-10 17:20 Rational Apex Ada - Pragma interface with C++ John Smith
2002-10-10 19:32 ` Stephen Leake
2002-10-10 21:47 ` Matthew Heaney
2002-10-14 16:41   ` John Smith
2002-10-14 23:03   ` John Smith
2002-10-15  0:12     ` Jeffrey Creem
2002-10-15  1:08       ` John Smith
2002-10-15 13:31         ` Ted Dennison
2002-10-11 11:12 ` Marc A. Criley
2002-10-14 16:28   ` John Smith

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