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=-0.3 required=5.0 tests=BAYES_00, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,c1bdceb867926fdb X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII Path: g2news1.google.com!news3.google.com!feeder.news-service.com!newsfeed.straub-nv.de!open-news-network.org!news.musoftware.de!wum.musoftware.de!news.weisnix.org!newsfeed.ision.net!newsfeed2.easynews.net!ision!newsfeed.arcor.de!newsspool3.arcor-online.net!news.arcor.de.POSTED!not-for-mail From: "Dmitry A. Kazakov" Subject: Re: Interfacing Ada with C Newsgroups: comp.lang.ada User-Agent: 40tude_Dialog/2.0.15.1 MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 8bit Reply-To: mailbox@dmitry-kazakov.de Organization: cbb software GmbH References: <0ee9eec7-6024-4fb8-8df0-f65c146e4b84@i28g2000yqa.googlegroups.com> <143ef70b-7e74-426b-a621-a5fd157849be@x21g2000yqa.googlegroups.com> <18zszx6sjlloa$.k5nohxp9k27i$.dlg@40tude.net> <91c174e6-c359-4bf5-b284-d93a725ad09d@c10g2000yqi.googlegroups.com> <0dadd6b6-452d-43e9-b0b9-374b5106b298@t2g2000yqe.googlegroups.com> <4acf7f59-2675-4498-8d95-35d01bc1e3af@j8g2000yqd.googlegroups.com> <1vcstpdax6vxp$.1umnj0p2l8a32$.dlg@40tude.net> Date: Sun, 25 Jul 2010 20:26:51 +0200 Message-ID: <1x17akdoyxksk$.1edm73b1nxqyf$.dlg@40tude.net> NNTP-Posting-Date: 25 Jul 2010 20:26:53 CEST NNTP-Posting-Host: ed4c609b.newsspool2.arcor-online.net X-Trace: DXC=9hJ@Ff1:NMIAa;:RKVJ>LEA9EHlD;3YcB4Fo<]lROoRA8kF On Sun, 25 Jul 2010 10:40:09 -0700 (PDT), Ada novice wrote: > On Jul 25, 7:06�pm, "Dmitry A. Kazakov" > wrote: >> C files or library files? > >> With library files (import or object) it is simpler >> > >> (If you have only a shared library then it would require a bit more work.) >> > > It's a library file in IMSL named imslcmath_dll.lib > > To use IMSL C library there are 2 steps: > > 1. Specify the location of the IMSL header files. It's an "include" > directory in the IMSL directory You do not need include files > 2. Add the actual IMSL C library to the project. This IMSL C library > is called imslcmath_dll.lib which is in a "lib" directory in the IMSL > directory. There are other lib files, but this one is used for > numerical tasks. > > I'm able to do the above easily with MVS2008/2010. The first step is > adding the "include" directory under Project > Configuration > Properties > C/C++ > General. The second step is hust Project > Add > existing item. > > > I have only limited experience with GPS but let's give it a try: > > First I add create a folder and put the file imsl.ads, imsl.adb and > main_ada_file.adb (my testmatrix.adb). Then I create a project gpr, > specifying the main file and the source directory (containing our 3 > files). > > Then according to my step 1 from above, I add the folder "include" > from IMSL as also a source directory. So now I have 2 source > directories. > > Then for step 2, I need to add the imslcmath_dll.lib file? How do I do > that in GPS? I can compile all 3 files imsl.ads, imsl.adb, > main_ada_file.adb in GPS. Then how do I proceed? > > How do I do this in GPS: --------------------- imsl.ads with Interfaces.C; use Interfaces.C; package IMSL is type Float_Matrix is -- Row-wise, packed/aligned array (Positive range <>, Positive range <>) of C_Float; pragma Convention (C, Float_Matrix); type F_Complex is record Re : C_Float; Im : C_Float; end record; pragma Convention (C, F_Complex); type Complex_Array is array (Positive range <>) of F_Complex; pragma Convention (C, Complex_Array); function F_Eig_Gen (Matrix : Float_Matrix) return Complex_Array; end IMSL; -------------------------- imsl.adb with Interfaces.C; use Interfaces.C; with System; use System; package body IMSL is procedure Free (Ptr : System.Address); pragma Import (C, Free, "free"); function F_Eig_Gen (Matrix : Float_Matrix) return Complex_Array is begin if Matrix'Length (1) /= Matrix'Length (2) then raise Constraint_Error; end if; declare function imsl_f_eig_gen (N : Int; A : Address; Terminator : Address := Null_Address) return Address; pragma Import (C, imsl_f_eig_gen, "imsl_f_eig_gen"); Ptr : Address := imsl_f_eig_gen ( Matrix'Length (1), Matrix ( Matrix'First (1), Matrix'First (2) )' Address ); Data : Complex_Array (1..Matrix'Length (1)); for Data'Address use Ptr; pragma Import (Ada, Data); Result : Complex_Array := Data; -- Copy, we don't own that memory begin Free (Ptr); return Result; end; end F_Eig_Gen; end IMSL; ------------------------------------ imsl.gpr project IMSL is for Source_Files use ("imsl.adb", "imsl.ads"); package Linker is for Default_Switches ("ada") use ("imslcmath_dll.lib"); end Linker; end IMSL; ----------------------------------- test.adb with Ada.Text_IO; use Ada.Text_IO; with IMSL; use IMSL; with Interfaces.C; use Interfaces.C; procedure Test is Values : Complex_Array := F_Eig_Gen ( ( ( 8.0,-1.0,-5.0), (-4.0, 4.0,-2.0), (18.0,-5.0,-7.0) ) ); begin for Index in Values'Range loop Put_Line ( '(' & C_Float'Image (Values (Index).Re) & " ," & C_Float'Image (Values (Index).Im) & ')' ); end loop; end Test; ------------------------------------ test.gpr with "imsl.gpr"; project Test is for Source_Files use ("test.adb"); for Main use ("test.adb"); package Linker renames IMSL.Linker; end Test; put "imslcmath_dll.lib into the same directory or change its path in the project files. Make sure that the DLL is in the search path when you start the program. -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de