JiscMail Logo
Email discussion lists for the UK Education and Research communities

Help for COMP-FORTRAN-90 Archives


COMP-FORTRAN-90 Archives

COMP-FORTRAN-90 Archives


COMP-FORTRAN-90@JISCMAIL.AC.UK


View:

Message:

[

First

|

Previous

|

Next

|

Last

]

By Topic:

[

First

|

Previous

|

Next

|

Last

]

By Author:

[

First

|

Previous

|

Next

|

Last

]

Font:

Proportional Font

LISTSERV Archives

LISTSERV Archives

COMP-FORTRAN-90 Home

COMP-FORTRAN-90 Home

COMP-FORTRAN-90  2005

COMP-FORTRAN-90 2005

Options

Subscribe or Unsubscribe

Subscribe or Unsubscribe

Log In

Log In

Get Password

Get Password

Subject:

C Interop for DLLs: Was: Dynamic loading of shared object files in Fortran

From:

Aleksandar Donev <[log in to unmask]>

Reply-To:

Fortran 90 List <[log in to unmask]>

Date:

Thu, 1 Dec 2005 22:15:55 -0500

Content-Type:

multipart/mixed

Parts/Attachments:

Parts/Attachments

text/plain (49 lines) , dlfcn.f90 (116 lines) , dlfcn_c.c (7 lines) , dlfcn_aux.f90 (21 lines) , shared.f90 (8 lines)

Richard E Maine wrote:

> It uses the C dl (dynamic link) library and is thus highly
> system-dependent in a way... but I've been able to port it to
> multiple Unix platforms without horribly much change. 
Attached is a file dlfcn.f90 which illustrates how to use C interop 
features in Fortran 2003 and the dl library to invoke a dll under UNIX. 
I provide code which works if procedure pointers are not available--the 
file dlfcn_aux.f90 provides an illegal hack that should work on most 
compilers. If procedure pointers are available, just uncomment those 
lines in the main program.

First compile the "dll" using the appropriate switches, for example:

f95 -c -pic shared.f90 -o shared.o
ld -shared shared.o -o shared.so

And then

f95 -c dlfcn_aux.f90
f95 -o dlfcn.x dlfcn.f90 -ldl dlfcn_aux.o

It seems to work nicely:

1.
dlfcn.x
  Enter the name of the DLL and the name of the DLL subroutine:
shared.so
MySub
  MySub: x=   1.0000000000000000

2.
dlfcn.x
  Enter the name of the DLL and the name of the DLL subroutine:
super
m
  Error in dlopen: super: cannot open shared object file: No such file or
directory

3.
dlfcn.x
  Enter the name of the DLL and the name of the DLL subroutine:
shared.so
YourSub
  Error in dlsym: ./shared.so: undefined symbol: YourSub

Aleks



MODULE DLFCN    USE ISO_C_BINDING    IMPLICIT NONE    PRIVATE        INTEGER, PARAMETER, PUBLIC :: RTLD_LAZY=1, RTLD_NOW=2, RTLD_GLOBAL=256, RTLD_LOCAL=0    PUBLIC :: DLOpen, DLSym, DLClose, DLError, CToFortranString        CHARACTER(C_CHAR), DIMENSION(1), SAVE, TARGET, PUBLIC :: dummy_string="?"        INTERFACE       FUNCTION DLOpen(file,mode) RESULT(handle) BIND(C,NAME="dlopen")          ! void *dlopen(const char *file, int mode);          USE ISO_C_BINDING          CHARACTER(C_CHAR), DIMENSION(*), INTENT(IN) :: file          INTEGER(C_INT), VALUE :: mode          TYPE(C_PTR) :: handle       END FUNCTION       FUNCTION DLSym(handle,name) RESULT(funptr) BIND(C,NAME="dlsym")          ! void *dlsym(void *handle, const char *name);          USE ISO_C_BINDING          TYPE(C_PTR), VALUE :: handle          CHARACTER(C_CHAR), DIMENSION(*), INTENT(IN) :: name          TYPE(C_FUNPTR) :: funptr ! A pointer       END FUNCTION       FUNCTION DLClose(handle) RESULT(status) BIND(C,NAME="dlclose")          ! int dlclose(void *handle);          USE ISO_C_BINDING          TYPE(C_PTR), VALUE :: handle          INTEGER(C_INT) :: status       END FUNCTION       FUNCTION DLError() RESULT(error) BIND(C,NAME="dlerror")          ! char *dlerror(void);          USE ISO_C_BINDING          TYPE(C_PTR) :: error       END FUNCTION    END INTERFACE        CONTAINS    FUNCTION CToFortranString(cstr) RESULT(string)       TYPE(C_PTR), VALUE :: cstr       CHARACTER(C_CHAR), DIMENSION(:), POINTER :: string              CHARACTER(KIND=C_CHAR,LEN=1024), POINTER :: error_string       TYPE(C_PTR) :: error              IF(C_ASSOCIATED(cstr)) THEN          CALL C_F_POINTER(CPTR=cstr, FPTR=error_string) ! Not really standard-conforming          CALL C_F_POINTER(CPTR=cstr, FPTR=string, SHAPE=[INDEX(error_string,C_NULL_CHAR)])       ELSE          string=>dummy_string       END IF        END FUNCTION END MODULE PROGRAM DLFCN_Test    USE ISO_C_BINDING    USE DLFCN    USE Dynamic_Subroutine    IMPLICIT NONE        CHARACTER(KIND=C_CHAR,LEN=1024) :: dll_name, sub_name    TYPE(C_PTR) :: handle=C_NULL_PTR    TYPE(C_FUNPTR) :: funptr=C_NULL_FUNPTR    INTEGER(C_INT) :: status        INTERFACE       ! If procedure pointers are not implemented, hack away       SUBROUTINE CallDynamicSub(Sub,x)          USE ISO_C_BINDING          TYPE(C_FUNPTR), VALUE :: Sub ! Lie about this argument          REAL(C_DOUBLE), VALUE :: x       END SUBROUTINE    END INTERFACE    ! Otherwise, do it properly:    !PROCEDURE(MySub), POINTER :: dll_sub        WRITE(*,*) "Enter the name of the DLL and the name of the DLL subroutine:"    READ(*,"(A)") dll_name    READ(*,"(A)") sub_name    ! Convert to C convention:    dll_name=TRIM(dll_name)//C_NULL_CHAR    sub_name=TRIM(sub_name)//C_NULL_CHAR        handle=DLOpen(dll_name, IOR(RTLD_NOW, RTLD_GLOBAL))       ! The use of IOR is not really proper...wait till Fortran 2008    IF(.NOT.C_ASSOCIATED(handle)) THEN       WRITE(*,*) "Error in dlopen: ", CToFortranString(DLError())       STOP    END IF        funptr=DLSym(handle,sub_name)    IF(.NOT.C_ASSOCIATED(funptr)) THEN       WRITE(*,*) "Error in dlsym: ", CToFortranString(DLError())       STOP    END IF    ! If procedure pointers are not available, hack:    CALL CallDynamicSub(funptr,1.0_c_double) ! Actually invoke the DLL    ! Otherwise, do it properly:    ! CALL C_F_PROCPOINTER(cptr=funptr, fptr=dll_sub)    ! CALL dll_sub(1.0_c_double)        status=DLClose(handle)    IF(status/=0) THEN       WRITE(*,*) "Error in dlclose: ", CToFortranString(DLError())       STOP    END IF END PROGRAM
MODULE Dynamic_Subroutine    IMPLICIT NONE    ABSTRACT INTERFACE       SUBROUTINE MySub(x) BIND(C)          USE ISO_C_BINDING          REAL(C_DOUBLE), VALUE :: x       END SUBROUTINE    END INTERFACE END MODULE SUBROUTINE CallDynamicSub(Sub,x)    USE ISO_C_BINDING    USE Dynamic_Subroutine    IMPLICIT NONE    PROCEDURE(MySub) :: Sub    REAL(C_DOUBLE), VALUE :: x    CALL Sub(x) END SUBROUTINE
SUBROUTINE MySub(x) BIND(C,NAME="MySub")    USE ISO_C_BINDING    REAL(C_DOUBLE), VALUE :: x    WRITE(*,*) "MySub: x=",x END SUBROUTINE

Top of Message | Previous Page | Permalink

JiscMail Tools


RSS Feeds and Sharing


Advanced Options


Archives

December 2023
February 2023
November 2022
September 2022
February 2022
January 2022
June 2021
November 2020
September 2020
June 2020
May 2020
April 2020
December 2019
October 2019
September 2019
March 2019
February 2019
January 2019
November 2018
October 2018
September 2018
August 2018
July 2018
May 2018
April 2018
March 2018
February 2018
January 2018
December 2017
November 2017
October 2017
September 2017
August 2017
July 2017
June 2017
May 2017
April 2017
March 2017
January 2017
December 2016
November 2016
October 2016
September 2016
August 2016
July 2016
June 2016
May 2016
April 2016
March 2016
February 2016
December 2015
November 2015
October 2015
September 2015
August 2015
June 2015
April 2015
March 2015
January 2015
December 2014
November 2014
October 2014
August 2014
July 2014
May 2014
April 2014
March 2014
February 2014
January 2014
December 2013
November 2013
July 2013
June 2013
May 2013
April 2013
February 2013
January 2013
December 2012
November 2012
October 2012
September 2012
August 2012
July 2012
June 2012
April 2012
March 2012
February 2012
January 2012
December 2011
November 2011
October 2011
September 2011
August 2011
July 2011
June 2011
May 2011
April 2011
March 2011
February 2011
January 2011
December 2010
November 2010
October 2010
August 2010
July 2010
June 2010
March 2010
February 2010
January 2010
December 2009
October 2009
August 2009
July 2009
June 2009
March 2009
February 2009
January 2009
December 2008
November 2008
October 2008
September 2008
August 2008
July 2008
June 2008
May 2008
April 2008
March 2008
February 2008
December 2007
November 2007
October 2007
September 2007
August 2007
July 2007
June 2007
May 2007
April 2007
March 2007
January 2007
2006
2005
2004
2003
2002
2001
2000
1999
1998
1997


JiscMail is a Jisc service.

View our service policies at https://www.jiscmail.ac.uk/policyandsecurity/ and Jisc's privacy policy at https://www.jisc.ac.uk/website/privacy-notice

For help and support help@jisc.ac.uk

Secured by F-Secure Anti-Virus CataList Email List Search Powered by the LISTSERV Email List Manager