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.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 109fba,c910ec7af58d6d4b X-Google-Attributes: gid109fba,public X-Google-Thread: 103376,c910ec7af58d6d4b X-Google-Attributes: gid103376,public From: James Hopper Subject: Re: Ada/C++ interface (SPARC Works) Date: 1995/04/20 Message-ID: <3n5vfh$ai3@dayuc.dayton.saic.com>#1/1 X-Deja-AN: 101281577 distribution: world references: content-type: text/plain; charset=ISO-8859-1 x-xxmessage-id: organization: SAIC mime-version: 1.0 newsgroups: comp.lang.ada,comp.lang.c++ Date: 1995-04-20T00:00:00+00:00 List-Id: >I have some code that uses a C library function, I compile this with the >SPARC C++ compiler and it doesn't link with the Ada code. if I compile >it with GCC it links just fine. > Here is a piece from a doc i wrote for our people on interfacing verdix (read sun ada to sun C++) not the extern added to the front of the c declaration this is required for C++ to use c conventions so it will link with Ada procedure C_Put(Value : in Integer); -- Ada extern "C" void c_put(int value) // C++ The pragma interface and pragma interface_Name statements are added to the Ada source to define for the benefit of the linker that this is an external procedure written in C, and its external name. The package language is a Verdix package with defines among other things the prefix that the C compiler adds to a function name. The pragmas are defined as follows: pragma Interface(C, C_Put); pragma Interface_name(C_Put, language.c_prefix & "c_put"); The extern statement is added to the C++ function definition statement to require the compiler produce a C style interface to the function for the use of the linker. The Verdix Ada language interface supports C but not C++ directly, thus the need for the extern "C" statement. It should be noted that pragma Interface_name is a Verdix extension to the Ada language and may be different for other compilers. The full Ada and C source code for this example, as well as the commands to compile and link them is presented below. The example is completed by presenting a sample run. Ada Source for Test #1 with Text_IO; procedure Test1 is procedure C_Put(Value : in Integer); pragma Interface (C, C_Put); pragma Interface_Name (C_Put, language.c_prefix & "c_put"); Value : Integer := 22; begin -- test1 Text_IO.Put_Line("Starting Test #1"); Text_IO.Put_Line("Calling C function with value = " & Integer'Image(Value)); C_Put(Value); Text_IO.Put_Line("Back from C function"); end Test1; C Source for Test #1 #include ; extern "C" void c_put(int value) { printf("Inside C_Put\nValue = %i\n",value); } Compile/Link commands for Test #1 Ada Test1.a CC -c test1.c a.ld test1 test1.o Execution of Test #1 > a.out Starting Test #1 Calling C function with value = 22 Inside C_Put Value = 22 Back from C function