Re: Call by name (in a variable)
- From: Joe Krahn <lastname_at_niehs.nih.gov@xxxxx>
- Date: Tue, 04 Apr 2006 17:08:56 -0400
jdujic@xxxxxxxxx wrote:
Well, in general, no. Fortran is compiled language, and in general,
call references must be resolved in link time.
On the other hand, Windows offers somewhat similar mechanism for
working with dlls. Maybe Unix has something equivalent, but I'm not
sufficiently versatile with it. Windows API LoadLibrary loads a dll
identified by filename within process' address space, and
GetProcAddress returns the address of a routine identified by exported
name. The only thing to do is to dereference the pointer to the routine
(which, again, is not the easiest thing to do in Fortran, as it's not
portable) and call it.
So, here's non-standard code in Visual Fortran extensions, which calls
2 routines of different name but the same prototype from one dll:
use DFWIN
integer:: hDll
interface
subroutine Any(k)
integer k
end subroutine Any
end interface
pointer (pAny, Any)
hDll = LoadLibrary("MyDll.dll"//char(0))
if (hDll.ne.0) then
pAny = GetProcAddress(hDll, "_ROUTINE1")
if (pAny.ne.0) call Any(42)
pAny = GetProcAddress(hDll, "_ANOTHERROUTINE1")
if (pAny.ne.0) call Any(11)
end if
--
Jugoslav
http://www.xeffort.com
POSIX systems have a similar C library functions called dlopen() and dlsym(). You will have to deal with interfacing the C calls yourself. In both cases, you need to know what your compiler uses for symbol names.
Also, standard Fortran does not have procedure-pointer variables until F2003. Up to F95, you can reference a procedure name, but it is like a constant.
One portable way to do it is to build a list of functions that you want callable by name, and create a procedure-calling subroutine with something like:
select case(sub_name)
case ("abort")
call abort(arg1)
....
end select
I also assume that all procedures have identical argument lists. If not, you have an even bigger challenge.
Joe
.
- References:
- Call by name (in a variable)
- From: jellby
- Re: Call by name (in a variable)
- From: jdujic
- Call by name (in a variable)
- Prev by Date: Newbie question: correct use of modules and namespaces
- Next by Date: backslash escapes
- Previous by thread: Re: Call by name (in a variable)
- Next by thread: gcc 4.0 fortran frontend
- Index(es):
Relevant Pages
|