[an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]
 
[an error occurred while processing this directive] [an error occurred while processing this directive]
Skåne Sjælland Linux User Group - http://www.sslug.dk Home   Subscribe   Mail Archive   Forum   Calendar   Search
MhonArc Date: [Date Prev] [Date Index] [Date Next]   Thread: [Date Prev] [Thread Index] [Date Next]   MhonArc
 

Re: [PROG] Ada - Fortran binding



Hej igen, 

[klip]

> Vil du dele løsningen med os andre? 

Meget gerne! Jeg er vedlagt et lille eksempel længere nede i teksten.

> Dit spørgsmål fik
> prikket lidt til min nysgerrighed, så jeg mit gæt på
> løsningen er at B.5(26) i AdaLRM betyder at du definerer en
> "access-to-subprogram"-type til at pege på funktion med den
> rette parameterprofil.

Lige præcis

>
> Det er jo godt. Men husk at det også er værd at forstå
> _hvorfor_ det virker, så du ikke kommer til at generaliserer
> udfra et heldigt tilfælde.

Enig!

>
> Jacob

Jeg vil meget gerne dele et lille eksempel med jer. Jeg synes personligt at 
det er nemmere at lære fra eksempler, men jeg endnu ikke fundet et før jeg 
selv havde skrevet dette. (Det er det klassike problem med hønen og ægget).

Eksemplet er meget simplet. Det har i hoved program skevet i Ada (amain). 
Dette program kalder en Fortran subroutine (fsub) med en access-to-subprogram 
parameter. Denne er erklærer "external" i fortran. Fortran subroutinen kalder 
derefter Ada proceduren wri, som access-to-subprogram variablen peger på. 

Jeg har iøvrigt stadig et lille udestående problem. Det opstår hvis jeg 
indsætter en write statement i fortran koden, så kan jeg ikke lænke koden. 
Jeg går ud fra at problemet opstår fordi jeg skal have adgang til 
fortran-runtime bibliotekerne. Det er sikker fordi min g77 henholdsvis gnat 
er ikke samme version af gcc. Jeg er igang med at kompile gcc 3.1 nu, så jeg 
håber problemet løser sig. 

mvh
Anders


--------------------------------------------
----------- amain.ada ------------------------
-- test call back of Ada program from Fortran
-- compile with gnatmake amain.ada -largs fsub.o

with callback;

with Interfaces.Fortran;
use Interfaces.Fortran;

procedure amain is

  af : callback.a_func;

  -- specifify the use of the fortran subroutine in ada
  procedure fsub(fcn : in callback.a_func);

  pragma Import (Fortran, fsub,"fsub_");

begin

	-- make a reference to the 'wri' procedure
	af := callback.wri'access;

	-- use af as parameter to fsub subroutine
	fsub(af);

end amain;
----------- amain.ada ------------------------
--------------------------------------------

C------------------------------------------
C---------- fsub.f ---------------------------
	  subroutine fsub(fcn)
	  integer i
	  external fcn

C This is a simple subroutine for testing call-back.
C Ada to Fortran and back to Ada
C Fsub takes a function argument and call the function with an integer
C argument.
C An Ada function is passed to Fsub from the ada program amain. The argument
C is the ada procedure wri.
C
C Compile with g77 -c fsub.f

	  call fcn(i)

	  return
	  end
C---------- fsub.f ---------------------------------
C-----------------------------------------------

------------------------------------------------
---------- callback.ads ----------------------------

with Ada.Text_IO;
use Ada.Text_IO;

with Interfaces.Fortran;
use Interfaces.Fortran;

package CallBack is

    -- access (pointer) type which can point to wri procedure
    type a_func is access procedure(data: in integer);   	

    -- the wri procedure specification
    procedure wri(data: in integer);                        

end CallBack;


---------- callback.ads ----------------------------
------------------------------------------------


------------------------------------------------
----------- callback.adb ---------------------------
package body CallBack is

   procedure wri(data: in integer) is
   begin
     Put_Line("This is within the 'wri' procedure!");
   end wri;

end CallBack;
----------- callback.adb -----------------------------
--------------------------------------------------



 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2005-08-10, 19:58 CEST [an error occurred while processing this directive]
This page is maintained by [an error occurred while processing this directive]MHonArc [an error occurred while processing this directive] # [an error occurred while processing this directive] *