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=-2.9 required=5.0 tests=BAYES_00,MAILING_LIST_MULTI, WEIRD_QUOTING autolearn=unavailable autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,32d4b7099c3e0137 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2003-01-20 17:20:26 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!newsmi-us.news.garr.it!NewsITBone-GARR!news.mailgate.org!fr.usenet-edu.net!usenet-edu.net!enst.fr!not-for-mail From: sk Newsgroups: comp.lang.ada Subject: Re: C code to Ada Date: Mon, 20 Jan 2003 19:23:26 -0600 Organization: ENST, France Message-ID: References: Reply-To: "comp.lang.ada mail to news gateway" NNTP-Posting-Host: marvin.enst.fr Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Trace: avanie.enst.fr 1043112025 70662 137.194.161.2 (21 Jan 2003 01:20:25 GMT) X-Complaints-To: usenet@enst.fr NNTP-Posting-Date: Tue, 21 Jan 2003 01:20:25 +0000 (UTC) To: "comp.lang.ada mail to news gateway" Return-Path: User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.0.1) Gecko/20020828 X-Accept-Language: en-us, en X-BeenThere: comp.lang.ada@ada.eu.org X-Mailman-Version: 2.1 Precedence: list List-Id: comp.lang.ada mail to news gateway List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Xref: archiver1.google.com comp.lang.ada:33261 Date: 2003-01-20T19:23:26-06:00 Hi yet again, Since I messed it all up pretty bad, here you go ... (feel free to clean it up, thicken it and release it back to CLA, hint hint :-) Please also remember that this is still q&d programming and is not tested other than "Hey, it works !" My original binding and everything else was a lot cleaner etc. ----------------------------- -- dynamic_shared_library.ads ----------------------------- package Dynamic_Shared_Library is function What_Does_It_All_Mean return Integer; pragma Export (C, What_Does_It_All_Mean, "wdiam"); end Dynamic_Shared_Library; ----------------------------- -- dynamic_shared_library.adb ----------------------------- package body Dynamic_Shared_Library is function What_Does_It_All_Mean return Integer is begin return 42; end What_Does_It_All_Mean; end Dynamic_Shared_Library; --------------------------------------- -- dynamic_linking_loading_inteface.ads --------------------------------------- with Interfaces.C; with Interfaces.C.Strings; with System; package Dynamic_Linking_Loading_Interface is pragma Linker_Options ("-ldl"); package IC renames Interfaces.C; package ICS renames Interfaces.C.Strings; type Handle is private; Empty_Handle : constant Handle; type Open_Flags is (Lazy, Now); -- Values from "/usr/include/bits/dlfcn.h" for Open_Flags use ( Lazy => 1, Now => 2 ); function Dl_Open ( Name : ICS.Chars_Ptr; Flags : Open_Flags ) return Handle; pragma Import (C, Dl_Open, "dlopen"); function Dl_Error return ICS.Chars_Ptr; pragma Import (C, Dl_Error, "dlerror"); function Dl_Sym ( Handle : Dynamic_Linking_Loading_Interface.Handle; Symbol : ICS.Chars_Ptr ) return System.Address; pragma Import (C, Dl_Sym, "dlsym"); function Dl_Close ( Handle : Dynamic_Linking_Loading_Interface.Handle ) return IC.Int; pragma Import (C, Dl_Close, "dlclose"); private type Handle is new System.Address; Empty_Handle : constant Handle := Handle(System.Null_Address); end Dynamic_Linking_Loading_Interface; --------------- -- lib_test.adb --------------- with Ada.Text_Io; with Ada.Unchecked_Conversion; with Dynamic_Linking_Loading_Interface; with Gnat.Directory_Operations; with Interfaces.C; with Interfaces.C.Strings; with System; procedure Lib_Test is -- References : -- -- "Gnat Users Guide : Creating an Ada Library" -- -- "Linux Documentation Project - Program Library HOWTO" -- -- This reference is the basis for the whole example and library -- packages. package DLLI renames Dynamic_Linking_Loading_Interface; package IC renames Interfaces.C; package ICS renames Interfaces.C.Strings; package GDIR renames Gnat.Directory_Operations; package TIO renames Ada.Text_Io; type Called_Function is access function return Integer; type Called_Procedure is access procedure; Library_Function : Called_Function; Ada_Library_Init : Called_Procedure; Ada_Library_Final : Called_Procedure; function Handle_To_Called_Function is new Ada.Unchecked_Conversion ( Source => System.Address, Target => Called_Function ); function Handle_To_Called_Procedure is new Ada.Unchecked_Conversion ( Source => System.Address, Target => Called_Procedure ); Handle : DLLI.Handle; use type DLLI.Handle; Errors : Boolean := False; Library_Error : ICS.Chars_Ptr; use type ICS.Chars_Ptr; Full_Library_Name : constant String := ( GDIR.Get_Current_Dir & "libmy_lib.so" ); begin Handle := DLLI.Dl_Open ( ICS.New_String(Full_Library_Name), DLLI.Lazy ); if Handle = DLLI.Empty_Handle then TIO.Put_Line ( "Couldn't open library """ & Full_Library_Name & """" ); TIO.Put_Line ("ERROR => " & ICS.Value (DLLI.Dl_Error)); Errors := True; end if; -- ADAINIT if not Errors then Ada_Library_Init := Handle_To_Called_Procedure ( DLLI.DL_Sym (Handle, ICS.New_String("adainit")) ); Library_Error := DLLI.DL_Error; if Library_Error = ICS.Null_Ptr then -- "adainit" Ada_Library_Init.All; else TIO.Put_Line ("Couldn't call libraries ""adainit"""); TIO.Put_Line ("ERROR => " & ICS.Value (Library_Error)); Errors := True; end if; end if; -- LIBRARY CALL if not Errors then Library_Function := Handle_To_Called_Function ( DLLI.DL_Sym (Handle, ICS.New_String("wdiam")) ); Library_Error := DLLI.DL_Error; if Library_Error = ICS.Null_Ptr then declare Result : Integer; begin Result := Library_Function.All; TIO.Put_Line ( "What does it all mean ? " & Integer'Image(Result) ); end; else TIO.Put_Line ( "Error finding the requested library symbol." ); TIO.Put_Line ("ERROR => " & ICS.Value (Library_Error)); Errors := True; end if; end if; -- ADAFINAL if not Errors then Ada_Library_Final := Handle_To_Called_Procedure ( DLLI.DL_Sym (Handle, ICS.New_String("adafinal")) ); Library_Error := DLLI.DL_Error; if Library_Error = ICS.Null_Ptr then -- "adafinal" Ada_Library_Final.All; else TIO.Put_Line ("Couldn't call libraries ""adainit"""); TIO.Put_Line ("ERROR => " & ICS.Value (Library_Error)); Errors := True; end if; end if; if not Errors then declare Throwaway : IC.Int; begin Throwaway := DLLI.DL_Close (Handle); end; end if; end Lib_Test; Putting the pieces together ... -- make of dynamic-library example $ gcc -c dynamic_shared_library.adb $ gnatbind -n dynamic_shared_library $ gcc -c b~dynamic_shared_library.adb $ gcc -shared -o libmy_lib.so \ dynamic_shared_library.o \ b~dynamic_shared_library.o \ /usr/gnat/lib/gcc-lib/i686-pc-linux-gnu/2.8.1/adalib/libgnat.a -- Note: Linking against the static gnat-library -- To illustrate a "read-only" library ... $ rm *.o $ chmod -w *.ali -- Example $ gnatmake lib_test -- -- -- Merge vertically for real address -- ------------------------------------ -- s n p @ t . o -- k i e k c c m ------------------------------------