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.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,d7a4bf5a82094fb X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2002-10-14 17:12:42 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!canoe.uoregon.edu!arclight.uoregon.edu!wn14feed!worldnet.att.net!204.127.198.203!attbi_feed3!attbi.com!sccrnsc01.POSTED!not-for-mail From: "Jeffrey Creem" Newsgroups: comp.lang.ada References: Subject: Re: Rational Apex Ada - Pragma interface with C++ X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1106 X-Mimeole: Produced By Microsoft MimeOLE V6.00.2800.1106 Message-ID: NNTP-Posting-Host: 66.31.5.146 X-Complaints-To: abuse@attbi.com X-Trace: sccrnsc01 1034640761 66.31.5.146 (Tue, 15 Oct 2002 00:12:41 GMT) NNTP-Posting-Date: Tue, 15 Oct 2002 00:12:41 GMT Organization: AT&T Broadband Date: Tue, 15 Oct 2002 00:12:41 GMT Xref: archiver1.google.com comp.lang.ada:29783 Date: 2002-10-15T00:12:41+00:00 List-Id: Look for the view switch "NON_ADA_LINKAGE:" Add the .o files from the c compile to this switch. "John Smith" 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" wrote in message > news:uqbtbu6rtkk7b@corp.supernews.com... > > > > "John Smith" 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; > > > > > > > > > >