From: "Nando" <nando89@terra.es>
Subject: Re: Import C
Date: Tue, 27 Jan 2004 05:47:30 GMT
Date: 2004-01-27T05:47:30+00:00 [thread overview]
Message-ID: <SXmRb.2937772$uj6.7516955@telenews.teleline.es> (raw)
In-Reply-To: m3wu7elr4e.fsf@insalien.org
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 3913 bytes --]
Thans you very much!!! :)))
It's a very complete information, with examples and comments. I'm going to
try now everything you told. I think every thing is going to be all right ;)
Thanks again
Fernando
"Ludovic Brenta" <ludovic.brenta@insalien.org> escribi� en el mensaje
news:m3wu7elr4e.fsf@insalien.org...
> "Nando" <nando89@terra.es> writes:
>
> > Hello!!!
> > I need make an Ada application. I need send and receive some information
> > to/from the serial port communication (from PC to another device).
> >
> > I don't know how to do it in Ada, but I know to do it in C/C++. I have
found
> > the command "pragma inport(C, ...)" but I don't know how it works :((
> >
> > My questions:
> > * Is enough the .C and .H files or I need the object file?
>
> The Ada toolchain needs the object file only. The .C and .H file are
> useful only for you to see the prototype for the function you want.
>
> > * How my Ada code know what file has my subrutine? i.e., where I say in
my
> > Ada source code what files to include?
>
> The Ada compiler will not include anything. The linker will link your
> object file produced from C with the object files produced from Ada.
>
> > If you could give me a little example or documentation you would help me
> > very much.
>
> You need to redeclare your C function in Ada, and then Import it.
> Suppose yourt .H file has this prototype:
>
> extern int foo(int a, int b);
>
> then you need to write this in an Ada source file:
>
> package Binding_To_External_Library is
> function Foo (A, B : Integer) return Integer; -- redeclare in Ada
> pragma Import (Convention => C, Entity => Foo, External_Name => "foo");
> end Binding_To_External_Library;
>
> The External_Name is the name that the function has in the object (not
> C source) file. Be careful with certain C compilers that insert an
> underscore before the function name; the External_Name might be
> "_foo". I would advise not to import a C++ method because the name
> mangling and the "this" pointer are sources of headaches. Write a C
> wrapper and import that instead.
>
> If your function uses structures, you also need to declare a
> corresponding type in Ada and use pragma Convention (C) on them.
>
> There are many existing bindings which you can find on the web. Thick
> bindings are better. A thick binding is one that replaces error codes
> (returned from the C functions) into exceptions, that perform range
> checking, and that are more type-safe than thin ones. For example,
> suppose your C header file defines these:
>
> /* Values for parameter A passed to foo */
> #define NO 0
> #define YES 1
> #define MAYBE 2
> #define MAYBENOT 3
>
> /* Your program will crash if B is larger than this. You're warned. */
> /* Oh, by the way, it will also crash if B < 0. */
> #define MAX_VALUE_FOR_B 100
>
> /* Values returned by foo */
> #define ERROR -1
> #define OK 0
>
> extern int foo(int a, int b);
>
> Then your thick binding would look like this:
>
> package Thick_Binding is
> Foo_Error : exception;
> type Assessment is (No, Yes, Maybe, Maybe_Not);
> pragma Convention (C, Assessment);
> type Safe_Value_For_B is new Integer range 0 .. 100;
> pragma Convention (C, Safe_Value_For_B);
> procedure Foo (A : Assessment; B : Safe_Value_For_B);
> -- A type-safe procedure that always checks error codes. The compiler
> -- ensures that you always call it with safe values for B.
> end Thick_Binding;
>
> package body Thick_Binding is
> procedure Foo (A : Assessment; B : Safe_Value_For_B) is
> -- Hide the ugly and error-prone C function
> function Internal (A, B : Integer) return Integer;
> pragma Import (C, Internal, "foo"); -- or maybe "_foo"
> begin
> if Internal (Assessment'Pos (A), Integer (B)) = -1 then
> raise Foo_Error;
> end if;
> end Foo;
> end Thick_Binding;
>
> HTH
>
> --
> Ludovic Brenta.
next prev parent reply other threads:[~2004-01-27 5:47 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2004-01-26 13:48 Import C Nando
2004-01-26 14:30 ` Ludovic Brenta
2004-01-27 5:47 ` Nando [this message]
2004-01-26 14:51 ` Stephen Leake
2004-01-27 5:56 ` Nando
2004-01-28 9:30 ` Import C... Works!! but how does it work :/ Nando
2004-01-28 14:24 ` Ludovic Brenta
2004-01-29 17:12 ` Stephen Leake
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox