comp.lang.ada
 help / color / mirror / Atom feed
* C to Ada interfacing
@ 1996-07-30  0:00 Lee Slaughter
  1996-07-31  0:00 ` Paul Hussein
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Lee Slaughter @ 1996-07-30  0:00 UTC (permalink / raw)



Hi,

I'm not an Ada programmer but need to interface with Ada
routines from C (on a VxWorks platform).

I have searched here, FAQ and the Tartanworks Ada docs
and found a little, but I could use more.

I'd appreciate it, especially sample code.

In my probably naive view i'd make a call to someAdaFn( arg )
and my Ada-programmer peer would define this function in some 
.ada or whatever source file who's
object file would be linked in with the C .o stuff, and of
course have to take special care to be sure data types align,
are in the right order with right number of gaps  and all.

Is that sort of sketchily it?

Thanks...

lee

---
  Lee Slaughter - network management development: submarine stuff   
  Navy R & D, Point Loma, San Diego, CA             lees@nosc.mil
  "Giving credence to his ramblings indicates a cognitive disorder..."
  				- Lee's psychiatrist, circa 1952




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

* Re: C to Ada interfacing
  1996-07-30  0:00 C to Ada interfacing Lee Slaughter
  1996-07-31  0:00 ` Paul Hussein
@ 1996-07-31  0:00 ` PR MITTON
  1996-08-01  0:00 ` Theodore E. Dennison
  2 siblings, 0 replies; 5+ messages in thread
From: PR MITTON @ 1996-07-31  0:00 UTC (permalink / raw)



Lee Slaughter (lees@cts.com) wrote:
: Hi,

: I'm not an Ada programmer but need to interface with Ada
: routines from C (on a VxWorks platform).

: I have searched here, FAQ and the Tartanworks Ada docs
: and found a little, but I could use more.

: I'd appreciate it, especially sample code.

: In my probably naive view i'd make a call to someAdaFn( arg )
: and my Ada-programmer peer would define this function in some 
: .ada or whatever source file who's
: object file would be linked in with the C .o stuff, and of
: course have to take special care to be sure data types align,
: are in the right order with right number of gaps  and all.

: Is that sort of sketchily it?

: Thanks...

: lee

: ---
:   Lee Slaughter - network management development: submarine stuff   
:   Navy R & D, Point Loma, San Diego, CA             lees@nosc.mil
:   "Giving credence to his ramblings indicates a cognitive disorder..."
:   				- Lee's psychiatrist, circa 1952

If you have an Ada 95 compiler then you can make use of the facilities 
described in Annex B.2 of the Ada LRM which is concerned with
interfacing to other languages. The text is available on the web if
you haven't got a printed version. 

(http://sw-eng.falls_church.va.us/AdaIC/standards/Welcome.html#95standards)

Hope this helps.

Pete

--

Pete Mitton             || If I brew good beer
Computing Department    || I'll drink the same
University of Bradford  || - from The Gypsy Davy. 
England                 || sung by Lena Bourne Fish




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

* Re: C to Ada interfacing
  1996-07-30  0:00 C to Ada interfacing Lee Slaughter
@ 1996-07-31  0:00 ` Paul Hussein
  1996-08-01  0:00   ` David Wheeler
  1996-07-31  0:00 ` PR MITTON
  1996-08-01  0:00 ` Theodore E. Dennison
  2 siblings, 1 reply; 5+ messages in thread
From: Paul Hussein @ 1996-07-31  0:00 UTC (permalink / raw)



lees@cts.com (Lee Slaughter) wrote:

>Hi,

>I'm not an Ada programmer but need to interface with Ada
>routines from C (on a VxWorks platform).

>I have searched here, FAQ and the Tartanworks Ada docs
>and found a little, but I could use more.

>I'd appreciate it, especially sample code.

>In my probably naive view i'd make a call to someAdaFn( arg )
>and my Ada-programmer peer would define this function in some 
>.ada or whatever source file who's
>object file would be linked in with the C .o stuff, and of
>course have to take special care to be sure data types align,
>are in the right order with right number of gaps  and all.

>Is that sort of sketchily it?

>Thanks...

>lee

>---
>  Lee Slaughter - network management development: submarine stuff   
>  Navy R & D, Point Loma, San Diego, CA             lees@nosc.mil
>  "Giving credence to his ramblings indicates a cognitive disorder..."
>  				- Lee's psychiatrist, circa 1952




You really need to look in the LRM for your compiler for this sort of
stuff. Pragma interface to and from C is very troublesome as their is
little standard support for it. The best approaches are :

1. Avoid at alll costs.
2. Get someone else to do it.
3. Obtain some example bindings from somewhere ( e.g. X Bindings ),
they must be for your specific target and compiler.
4. Read Appendix F ( or is it E ? ) of your LRM especially for
exporting functions.


   Why don't you bung an example piece of code in the newsgroup and
some kind sole might help you along.





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

* Re: C to Ada interfacing
  1996-07-31  0:00 ` Paul Hussein
@ 1996-08-01  0:00   ` David Wheeler
  0 siblings, 0 replies; 5+ messages in thread
From: David Wheeler @ 1996-08-01  0:00 UTC (permalink / raw)



Paul Hussein (husseinp@logica.co.uk) wrote:
: lees@cts.com (Lee Slaughter) wrote:

: >Hi,

: >I'm not an Ada programmer but need to interface with Ada
: >routines from C (on a VxWorks platform).

: >I'd appreciate it, especially sample code.

Take a look at my Lovelace tutorial.  Chapter 16 has some examples
and a general discussion on calling between C and Ada.  It concentrates on
Ada calling C, but the other direction has quite similar issues.

A key difference is that if the main routine is in C, you need to
call the Ada run-time to get it initialized.  The Ada LRM recommends that
such a function be given a specific name (I think it's ada_init()),
but there's no guarantee that it exists by that name.

A simple approach is to create a main Ada routine that then calls
the main C routine. Voila, you're done.

: >In my probably naive view i'd make a call to someAdaFn( arg )
: >and my Ada-programmer peer would define this function in some 
: >.ada or whatever source file who's
: >object file would be linked in with the C .o stuff, and of
: >course have to take special care to be sure data types align,
: >are in the right order with right number of gaps  and all.

You've got it basically right.  You can ask the Ada compiler to
do a lot of the aligning and gapping for you, but you have to specify
that you want the compiler to use C conventions when calling in or out
of C functions -- otherwise the compiler will use whatever it thinks is best.

: You really need to look in the LRM for your compiler for this sort of
: stuff. Pragma interface to and from C is very troublesome as their is
: little standard support for it.

I think this speaker means Ada 83.  Ada 95 is much nicer about interlanguage
support, I haven't had problems with it at all.  You'll need to get the
details right, so you'd be wise to learn at least the basics of Ada
before writing the interfacing code.  Like anything else, if you want
to interface two widgets, you should have a basic understanding of how
the two widgets work.

Your best bet would be to use the Ada95 pragmas controlling interlanguage
communication; there's a whole section in the Ada Language Reference
Manual that specifically describes how to interface with C programs.
Presuming you don't want to modify the originals, the simplest approach
would be to create small Ada procedures and functions that call the
"real" Ada programs.  Then declare that these trivial procedures and
functions will be called from C, using C types.  Bang.  Done.

Lovelace is at:
 http://lglwww.epfl.ch/Ada/Tutorials/Lovelace/lovelace.htm

--- David A. Wheeler
Net address: wheeler@ida.org





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

* Re: C to Ada interfacing
  1996-07-30  0:00 C to Ada interfacing Lee Slaughter
  1996-07-31  0:00 ` Paul Hussein
  1996-07-31  0:00 ` PR MITTON
@ 1996-08-01  0:00 ` Theodore E. Dennison
  2 siblings, 0 replies; 5+ messages in thread
From: Theodore E. Dennison @ 1996-08-01  0:00 UTC (permalink / raw)



Lee Slaughter wrote:
> 
> I'm not an Ada programmer but need to interface with Ada
> routines from C (on a VxWorks platform).
> 
> In my probably naive view i'd make a call to someAdaFn( arg )
> and my Ada-programmer peer would define this function in some
> .ada or whatever source file who's
> object file would be linked in with the C .o stuff, and of
> course have to take special care to be sure data types align,
> are in the right order with right number of gaps  and all.
> 
> Is that sort of sketchily it?

It depends on what the someAdaFn does. If it "withs" in other Ada
packages, It probably requires elaboration by the Ada runtime. In this
case, you'll probably have to either - 
   o  write the program's "main" in Ada. It can then call your C code
      which will eventually call someAdaFn. (Ada has better support for
      running C than C does for running Ada).
   o  make a call to the Ada runtime's elaboration routine before you
      call someAdaFn.

Other points:

Since the Ada runtime won't be there (unless you choose the first option
above), someAdaFn can't create or rendezvous with any tasks, and may not
even be able to perform a "delay", or do dynamic allocation. 

You also have to be careful about passing dynamicly-allocated data 
between Ada and C. I have seen situations where this caused problems
that could only be solved by redefining "malloc" to call an Ada routine
to do the dynamic allocation.

-- 
T.E.D.          
                |  Work - mailto:dennison@escmail.orl.mmc.com  |
                |  Home - mailto:dennison@iag.net              |
                |  URL  - http://www.iag.net/~dennison         |




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

end of thread, other threads:[~1996-08-01  0:00 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1996-07-30  0:00 C to Ada interfacing Lee Slaughter
1996-07-31  0:00 ` Paul Hussein
1996-08-01  0:00   ` David Wheeler
1996-07-31  0:00 ` PR MITTON
1996-08-01  0:00 ` Theodore E. Dennison

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