From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.1 required=5.0 tests=BAYES_00, PP_MIME_FAKE_ASCII_TEXT autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII X-Google-Thread: 103376,6f1f065a3ff6bcf0 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2004-01-26 21:48:09 PST Path: archiver1.google.com!news2.google.com!news.maxwell.syr.edu!wn13feed!worldnet.att.net!204.71.34.3!newsfeed.cwix.com!nsnmpen1-lo.nuria.telefonica-data.net!telenews.teleline.es!not-for-mail X-Trace-PostClient-IP: 81.61.181.19 From: "Nando" Newsgroups: comp.lang.ada References: Subject: Re: Import C X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2720.3000 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2727.1300 Message-ID: Date: Tue, 27 Jan 2004 05:47:30 GMT NNTP-Posting-Host: 10.20.31.4 X-Complaints-To: usenet@teleline.es X-Trace: telenews.teleline.es 1075182450 10.20.31.4 (Tue, 27 Jan 2004 06:47:30 MET) NNTP-Posting-Date: Tue, 27 Jan 2004 06:47:30 MET Organization: Terra Networks Xref: archiver1.google.com comp.lang.ada:4878 Date: 2004-01-27T05:47:30+00:00 List-Id: 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" escribi� en el mensaje news:m3wu7elr4e.fsf@insalien.org... > "Nando" 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.