comp.lang.ada
 help / color / mirror / Atom feed
* Re: Interfacing with C
       [not found] ` <37e2b0e7.0@news.pacifier.com>
@ 1999-09-22  0:00   ` Richard D Riehle
  1999-09-22  0:00     ` Ted Dennison
  1999-09-23  0:00     ` Robert Dewar
  0 siblings, 2 replies; 13+ messages in thread
From: Richard D Riehle @ 1999-09-22  0:00 UTC (permalink / raw)


My inquiry is a little different. 

We have a client who needs to interface to C++.  Has anyone completed a reliable
package that corresponds to the Interfaces.C for C++?   At present, they are using
pragma Import to C++ but that creates all kinds of entertaining problems with name
mangling, etc.

Richard Riehle




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

* Re: Interfacing with C
  1999-09-22  0:00   ` Interfacing with C Richard D Riehle
@ 1999-09-22  0:00     ` Ted Dennison
  1999-09-22  0:00       ` David Botton
  1999-09-23  0:00     ` Robert Dewar
  1 sibling, 1 reply; 13+ messages in thread
From: Ted Dennison @ 1999-09-22  0:00 UTC (permalink / raw)


In article <7s9nke$m2o@dfw-ixnews15.ix.netcom.com>,
  Richard D Riehle <LaoXhai@ix.netcom.com> wrote:
> We have a client who needs to interface to C++.  Has anyone completed
a reliable
> package that corresponds to the Interfaces.C for C++?   At present,
they are using

That's not really doable unless the Ada compiler is coded with knowledge
of the name-mangling algorithm that the C++ compiler uses (which is
*not* in the standard, and thus is subject to change every time the C++
compiler is revised). If you are using gcc and Gnat you may be in luck,
but I'm unaware of any other compiler that can deal with this.

I believe the usual technique is to use the C++ C interface features in
the C++ code (eg: extern "C"), and Ada's C interface features in the Ada
code.

--
T.E.D.


Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.




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

* Re: Interfacing with C
  1999-09-22  0:00     ` Ted Dennison
@ 1999-09-22  0:00       ` David Botton
  0 siblings, 0 replies; 13+ messages in thread
From: David Botton @ 1999-09-22  0:00 UTC (permalink / raw)


You can also export C++ objects that way using techniques similar to COM,
see my COM paper at http://www.adapower.com/com/adacom.pdf

David Botton

Ted Dennison wrote in message <7sb3e7$is2$1@nnrp1.deja.com>...

>I believe the usual technique is to use the C++ C interface features in
>the C++ code (eg: extern "C"), and Ada's C interface features in the Ada
>code.







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

* Re: Interfacing with C
  1999-09-22  0:00   ` Interfacing with C Richard D Riehle
  1999-09-22  0:00     ` Ted Dennison
@ 1999-09-23  0:00     ` Robert Dewar
  1 sibling, 0 replies; 13+ messages in thread
From: Robert Dewar @ 1999-09-23  0:00 UTC (permalink / raw)


In article <7s9nke$m2o@dfw-ixnews15.ix.netcom.com>,
  Richard D Riehle <LaoXhai@ix.netcom.com> wrote:
> My inquiry is a little different.
>
> We have a client who needs to interface to C++.  Has anyone
completed a reliable
> package that corresponds to the Interfaces.C for C++?   At
present, they are using
> pragma Import to C++ but that creates all kinds of
entertaining problems with name
> mangling, etc.


puzzling question ... interfaces.c has to do with data, and the
data types in C++ at this level are the same as in C ...


Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.




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

* Re: Interfacing with C
       [not found] <vhiln6cllz7.fsf@grotte.ifi.uio.no>
@ 1999-12-30  0:00 ` Simon Wright
  1999-12-31  0:00 ` Jerry van Dijk
  1 sibling, 0 replies; 13+ messages in thread
From: Simon Wright @ 1999-12-30  0:00 UTC (permalink / raw)


Jan Kroken <jankr@nntp.ifi.uio.no> writes:

> void y (datum d)
> {
> 	printf("d.size=%d\n",d.dsize);
> }

> procedure y (d: datum);
> pragma Import(C,y,"y");

> What I don't understand here is that x works perfectly, while
> the datum in y gets fucket up. Why?

Normally when passing a struct in C you would pass a pointer to it:

  void y (datum *d)

I don't know what the standard says about passing structs, but it's
quite likely your Ada and C compilers are disagreeing.

See LRM B.3(69): An Ada parameter of a record type T, of any mode, is
passed as a t* argument to a C function, where t is the C struct
corresponding to the Ada type T.




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

* Re: Interfacing with C
  1999-12-31  0:00 ` Jerry van Dijk
@ 1999-12-31  0:00   ` Simon Wright
  0 siblings, 0 replies; 13+ messages in thread
From: Simon Wright @ 1999-12-31  0:00 UTC (permalink / raw)


jerry@jvdsys.stuyts.nl (Jerry van Dijk) writes:

> >What I don't understand here is that x works perfectly, while
> >the datum in y gets fucket up. Why?
> 
> B.3(69)
> 
> Check compiler doc for pragma Pass_By_Copy.

A quick scan of the GNAT RM says that the implementation-defined
pragma C_Pass_By_Copy is a configuration pragma (ie applies to whole
program).

The GNAT RM also says there is a pragma Convention (C_Pass_By_Copy)
which could be applied to the record type. I couldn't find any
specific info on this (GNAT RM 3.11p, 3.12a2) -- it's only mentioned
under pragma C_Pass_By_Copy.




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

* Re: Interfacing with C
       [not found] <vhiln6cllz7.fsf@grotte.ifi.uio.no>
  1999-12-30  0:00 ` Simon Wright
@ 1999-12-31  0:00 ` Jerry van Dijk
  1999-12-31  0:00   ` Simon Wright
  1 sibling, 1 reply; 13+ messages in thread
From: Jerry van Dijk @ 1999-12-31  0:00 UTC (permalink / raw)


>What I don't understand here is that x works perfectly, while
>the datum in y gets fucket up. Why?

B.3(69)

Check compiler doc for pragma Pass_By_Copy.

--
--  Jerry van Dijk  | email: jdijk@acm.org
--  Team-Ada        | www:   stad.dsl.nl/~jvandyk
--  Paris, France   | Leiden, Holland




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

* Interfacing with C
@ 2009-05-18 11:29 cedric
  2009-05-18 11:51 ` Maciej Sobczak
  2009-05-18 11:58 ` Dmitry A. Kazakov
  0 siblings, 2 replies; 13+ messages in thread
From: cedric @ 2009-05-18 11:29 UTC (permalink / raw)


Hi All,

I would like to connect a C interface to another software package with
my Ada programs.

The h-file of the api contains the following pre-processor commands:

#ifdef SPSSXD_EXPORTS
# define SPSSXD_API __declspec(dllexport)
#else
# if MS_WINDOWS
#   define SPSSXD_API __declspec(dllimport)
# else
#   define SPSSXD_API
# endif
#endif


Some functions headers are

SPSSXD_API bool IsBackendReady();
SPSSXD_API int SetUpFromSPSS(SpssAdapter* anAdapter, SpssXDSmb*
anSmb);

What do I have to do to get a connection between the C functions and
my Ada program with regard to the commands of the pre-processor?

Regards

Cedric



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

* Re: Interfacing with C
  2009-05-18 11:29 cedric
@ 2009-05-18 11:51 ` Maciej Sobczak
  2009-05-18 11:58 ` Dmitry A. Kazakov
  1 sibling, 0 replies; 13+ messages in thread
From: Maciej Sobczak @ 2009-05-18 11:51 UTC (permalink / raw)


On 18 Maj, 13:29, cedric <c.lann...@web.de> wrote:

> The h-file of the api contains the following pre-processor commands:
>
> #ifdef SPSSXD_EXPORTS
> # define SPSSXD_API __declspec(dllexport)
> #else
> # if MS_WINDOWS
> #   define SPSSXD_API __declspec(dllimport)
> # else
> #   define SPSSXD_API
> # endif
> #endif
>
> Some functions headers are
>
> SPSSXD_API bool IsBackendReady();
> SPSSXD_API int SetUpFromSPSS(SpssAdapter* anAdapter, SpssXDSmb*
> anSmb);
>
> What do I have to do to get a connection between the C functions and
> my Ada program with regard to the commands of the pre-processor?

Nothing, just ignore it. This crap is needed for a Visual C++ compiler
which is unable to create a proper DLL otherwise (note that it has
nothing to do with C++ as the programming language).
Once this library is built, you should be able to interface with it
from Ada as if the preprocessor magic did not exist:

bool IsBackendReady();
int SetUpFromSPSS(SpssAdapter* anAdapter, SpssXDSmb* anSmb);

Still, apart from importing the properly declared subprograms, you
need to provide some special flag to the linker so that it can take
this library into account.

--
Maciej Sobczak * www.msobczak.com * www.inspirel.com

Database Access Library for Ada: www.inspirel.com/soci-ada



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

* Re: Interfacing with C
  2009-05-18 11:29 cedric
  2009-05-18 11:51 ` Maciej Sobczak
@ 2009-05-18 11:58 ` Dmitry A. Kazakov
  2009-05-18 12:11   ` Ludovic Brenta
  2009-06-01  8:24   ` David Thompson
  1 sibling, 2 replies; 13+ messages in thread
From: Dmitry A. Kazakov @ 2009-05-18 11:58 UTC (permalink / raw)


On Mon, 18 May 2009 04:29:44 -0700 (PDT), cedric wrote:

> I would like to connect a C interface to another software package with
> my Ada programs.
> 
> The h-file of the api contains the following pre-processor commands:
> 
> #ifdef SPSSXD_EXPORTS
> # define SPSSXD_API __declspec(dllexport)
> #else
> # if MS_WINDOWS
> #   define SPSSXD_API __declspec(dllimport)
> # else
> #   define SPSSXD_API
> # endif
> #endif

From this you should figure out the call convention. It can be either C or
stdcall. The rest can be usually ignored. Usually this stuff tells whether
the entry point is a reference or else a vector for dynamic run-time
linking or else an entry point, when the library itself is compiled,
nothing interesting for an Ada client.

> Some functions headers are
> 
> SPSSXD_API bool IsBackendReady();
> SPSSXD_API int SetUpFromSPSS(SpssAdapter* anAdapter, SpssXDSmb*
> anSmb);
> 
> What do I have to do to get a connection between the C functions and
> my Ada program with regard to the commands of the pre-processor?

Print the preprocessor output if you are in doubts. Otherwise it could be
like this:

   with Interfaces.C;  use Interfaces.C;
   ...
   function IsBackendReady return Int; -- C does not have Boolean
   pragma Import (stdcall, IsBackendReady, "IsBackendReady");

   type SpssAdapter is record
       ... -- Components of struct SpssAdapter
   end record;
   pragma Convention (C, SpssAdapter);

   type anSmb is record
       ... -- Components of struct anSmb
   end record;
   pragma Convention (C, anSmb);

   function SetUpFromSPSS
      (  anAdapter : access SpssAdapter;
          anSmb : access SpssXDSmb
      );
   pragma Import (stdcall, SetUpFromSPSS, "SetUpFromSPSS");

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



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

* Re: Interfacing with C
  2009-05-18 11:58 ` Dmitry A. Kazakov
@ 2009-05-18 12:11   ` Ludovic Brenta
  2009-05-18 12:13     ` Ludovic Brenta
  2009-06-01  8:24   ` David Thompson
  1 sibling, 1 reply; 13+ messages in thread
From: Ludovic Brenta @ 2009-05-18 12:11 UTC (permalink / raw)


On May 18, 1:58 pm, "Dmitry A. Kazakov" <mail...@dmitry-kazakov.de>
wrote:
> On Mon, 18 May 2009 04:29:44 -0700 (PDT), cedric wrote:
> > I would like to connect a C interface to another software package with
> > my Ada programs.
>
> > The h-file of the api contains the following pre-processor commands:
>
> > #ifdef SPSSXD_EXPORTS
> > # define SPSSXD_API __declspec(dllexport)
> > #else
> > # if MS_WINDOWS
> > #   define SPSSXD_API __declspec(dllimport)
> > # else
> > #   define SPSSXD_API
> > # endif
> > #endif
>
> From this you should figure out the call convention. It can be either C or
> stdcall. The rest can be usually ignored. Usually this stuff tells whether
> the entry point is a reference or else a vector for dynamic run-time
> linking or else an entry point, when the library itself is compiled,
> nothing interesting for an Ada client.
>
> > Some functions headers are
>
> > SPSSXD_API bool IsBackendReady();
> > SPSSXD_API int SetUpFromSPSS(SpssAdapter* anAdapter, SpssXDSmb*
> > anSmb);
>
> > What do I have to do to get a connection between the C functions and
> > my Ada program with regard to the commands of the pre-processor?
>
> Print the preprocessor output if you are in doubts. Otherwise it could be
> like this:
>
>    with Interfaces.C;  use Interfaces.C;
>    ...
>    function IsBackendReady return Int; -- C does not have Boolean
>    pragma Import (stdcall, IsBackendReady, "IsBackendReady");
>
>    type SpssAdapter is record
>        ... -- Components of struct SpssAdapter
>    end record;
>    pragma Convention (C, SpssAdapter);
>
>    type anSmb is record
>        ... -- Components of struct anSmb
>    end record;
>    pragma Convention (C, anSmb);
>
>    function SetUpFromSPSS
>       (  anAdapter : access SpssAdapter;
>           anSmb : access SpssXDSmb
>       );
>    pragma Import (stdcall, SetUpFromSPSS, "SetUpFromSPSS");
>
> --
> Regards,
> Dmitry A. Kazakovhttp://www.dmitry-kazakov.de

Funny, I wrote the bindings for these two particular functions
yesterday afternoon at the Ada-Belgium general assembly for one of our
attendees. The types SpssAdapter and SpssXDSmb are opaque in the
specification (they are declared as:

class SpssAdapter;
class SpssXDSmb;

and the only function using them takes pointers to them). My solution
was something like:

package SPSS is
   type Adapter is null record;
   type Adapter_Access is access all Adapter;
   pragma Convention (C, Adapter_Access);

   type XD_SMB is null record;
   type XD_SMB_Access is access all XD_SMB;
   pragma Convention (C, XD_SMB_Access);

   function Set_Up_From_SPSS
     (An_Adapter : in Adapter; An_SMB : in XD_SMB_Access)
     return Interfaces.C.int;
   pragma Import (Convention => C, Entity => Set_Up_From_SPSS,
     Linker_Name => "SetUpFromSPSS");
end SPSS;

This solution avoids the need to define an Ada type equivalent to, and
with the same representation as, the C type, and it respects the
opaqueness of the types in the C API.

--
Ludovic Brenta.



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

* Re: Interfacing with C
  2009-05-18 12:11   ` Ludovic Brenta
@ 2009-05-18 12:13     ` Ludovic Brenta
  0 siblings, 0 replies; 13+ messages in thread
From: Ludovic Brenta @ 2009-05-18 12:13 UTC (permalink / raw)


I just wrote on comp.lang.ada:
> package SPSS is
>    type Adapter is null record;
>    type Adapter_Access is access all Adapter;
>    pragma Convention (C, Adapter_Access);
>
>    type XD_SMB is null record;
>    type XD_SMB_Access is access all XD_SMB;
>    pragma Convention (C, XD_SMB_Access);
>
>    function Set_Up_From_SPSS
>      (An_Adapter : in Adapter; An_SMB : in XD_SMB_Access)

Sorry; of course I meant
    function Set_Up_From_SPSS
      (An_Adapter : in Adapter_Access; An_SMB : in XD_SMB_Access)

>      return Interfaces.C.int;
>    pragma Import (Convention => C, Entity => Set_Up_From_SPSS,
>      Linker_Name => "SetUpFromSPSS");
> end SPSS;

--
Ludovic Brenta.



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

* Re: Interfacing with C
  2009-05-18 11:58 ` Dmitry A. Kazakov
  2009-05-18 12:11   ` Ludovic Brenta
@ 2009-06-01  8:24   ` David Thompson
  1 sibling, 0 replies; 13+ messages in thread
From: David Thompson @ 2009-06-01  8:24 UTC (permalink / raw)


On Mon, 18 May 2009 13:58:35 +0200, "Dmitry A. Kazakov"
<mailbox@dmitry-kazakov.de> wrote:

> On Mon, 18 May 2009 04:29:44 -0700 (PDT), cedric wrote:

> > SPSSXD_API bool IsBackendReady();

>    with Interfaces.C;  use Interfaces.C;
>    ...
>    function IsBackendReady return Int; -- C does not have Boolean
>    pragma Import (stdcall, IsBackendReady, "IsBackendReady");
> 
C99 does, but that name is optional; _Bool always exists, but bool is
typedef'ed to it only if you #include <stdbool.h>. This was done to
preserve C90 (or earlier) code, where it is fairly common to typedef
bool to an integer or or enum type for documentation, even though it
doesn't change the actual semantics (and machine code). So it would be
necessary to determine which (or what) this code is using.

Though in practice, all integer types up to at least int/u-int are
often treated the same in the calling convention, so there's a fair
chance declaring it as Int=int will work even if it's really something
else including _Bool.




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

end of thread, other threads:[~2009-06-01  8:24 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <7rtlav$bm7$1@nnrp1.deja.com>
     [not found] ` <37e2b0e7.0@news.pacifier.com>
1999-09-22  0:00   ` Interfacing with C Richard D Riehle
1999-09-22  0:00     ` Ted Dennison
1999-09-22  0:00       ` David Botton
1999-09-23  0:00     ` Robert Dewar
     [not found] <vhiln6cllz7.fsf@grotte.ifi.uio.no>
1999-12-30  0:00 ` Simon Wright
1999-12-31  0:00 ` Jerry van Dijk
1999-12-31  0:00   ` Simon Wright
2009-05-18 11:29 cedric
2009-05-18 11:51 ` Maciej Sobczak
2009-05-18 11:58 ` Dmitry A. Kazakov
2009-05-18 12:11   ` Ludovic Brenta
2009-05-18 12:13     ` Ludovic Brenta
2009-06-01  8:24   ` David Thompson

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