|
forums.silverfrost.com Welcome to the Silverfrost forums
|
View previous topic :: View next topic |
Author |
Message |
hnoboa
Joined: 23 Feb 2009 Posts: 2 Location: Wisconsin, USA
|
Posted: Mon Feb 23, 2009 10:56 pm Post subject: Linking ftn95 DLLs in a ftn95 exe |
|
|
I am trying to learn how to link dlls in ftn95, so I made one file with this code:
Code: | MODULE Variables
IMPLICIT NONE
! INTEGER, PARAMETER::IKIND = SELECTED_REAL_KIND(12,99)
REAL :: a, b, c, x1, x2 !(KIND=IKIND)
END MODULE Variables
PROGRAM Binomial
USE Variables
a=1
b=1
c=-12
call RootsFinder
call PrintResults(a,b,c,x1,x2)
read *,a
END PROGRAM Binomial
|
a second file with this code:
Code: | SUBROUTINE RootsFinder
USE Variables
REAL :: Disc !(KIND=IKIND)
Disc = b**2-4.0*a*c
IF (Disc > 0) THEN
x1=(-b+sqrt(Disc))/(2*a)
x2=(-b-sqrt(Disc))/(2*a)
END IF
END SUBROUTINE RootsFinder
|
These two files are compiled together using the Source Files in the project explorer. In this project, I also reference the dll created with the following code:
Code: | PROGRAM TEST
IMPLICIT NONE
INTERFACE
SUBROUTINE PrintResults(a,b,c,x1,x2)
REAL, INTENT(IN):: a,b,c,x1,x2
END SUBROUTINE PrintResults
END INTERFACE
CONTAINS
F_STDCALL SUBROUTINE PrintResults(a,b,c,x1,x2)
! INTEGER, PARAMETER::IKIND = SELECTED_REAL_KIND(12,99)
IMPLICIT NONE
REAL, INTENT(IN):: a, b, c, x1, x2 !, (KIND=IKIND)
Print *, a,b,c
print *, x1,x2
END SUBROUTINE PrintResults
F_STDCALL INTEGER FUNCTION LIBMAIN(hInst,ul,lpR)
INTEGER hInst,ul,lpR
!***** Initialise global data here
LIBMAIN=1
END
END PROGRAM TEST |
This third file is compiled by itself to generate the dll.
When I execute the exe, I get this:
Run-time error:
missing routine _printresults at 0x0....
in file clacvars.f95, at line 13 (the first file)
What am I missing? Could anybody direct me to a short clean example how to do this?
Thanks
Homero |
|
Back to top |
|
|
PaulLaidler Site Admin
Joined: 21 Feb 2005 Posts: 7927 Location: Salford, UK
|
Posted: Tue Feb 24, 2009 8:37 am Post subject: |
|
|
There may be a few things to sort out here but to start with you do not need/want F_STDCALL if you are using FTN95 throughout.
If you do put it in then you need it at both ends (in the call as well as the definition) but this can be more tricky. |
|
Back to top |
|
|
hnoboa
Joined: 23 Feb 2009 Posts: 2 Location: Wisconsin, USA
|
Posted: Tue Feb 24, 2009 3:39 pm Post subject: |
|
|
Thanks Paul, I got it running!!
Thanks to your advise. The other main problem my dll had was that it had the subroutine inside the program, therefore was NOT external and could not be accessed by the main program. I made this change:
Code: | PROGRAM TEST
IMPLICIT NONE
INTERFACE
SUBROUTINE PrintResults(a,b,c,x1,x2)
REAL, INTENT(IN):: a,b,c,x1,x2
END SUBROUTINE PrintResults
END INTERFACE
END PROGRAM TEST
SUBROUTINE PrintResults(a,b,c,x1,x2)
! INTEGER, PARAMETER::IKIND = SELECTED_REAL_KIND(12,99)
IMPLICIT NONE
REAL, INTENT(IN):: a, b, c, x1, x2 !, (KIND=IKIND)
Print *, a,b,c
print *, x1,x2
END SUBROUTINE PrintResults
F_STDCALL INTEGER FUNCTION LIBMAIN(hInst,ul,lpR)
INTEGER hInst,ul,lpR
!***** Initialise global data here
LIBMAIN=1
END
|
and now it works.
I have another question for you. Is the LIBMAIN useful at all? In the help I read that I can use it to declare global variables. Could you explain how it works?
Thanks. |
|
Back to top |
|
|
PaulLaidler Site Admin
Joined: 21 Feb 2005 Posts: 7927 Location: Salford, UK
|
Posted: Tue Feb 24, 2009 4:49 pm Post subject: |
|
|
If you do not use LibMain then leave it out and SLINK will provide it for you.
Sometimes programmers need to create global data the persists from one subroutine call to another and is shared between different subroutines. It is simpler and easier to avoid global data and to pass everything as arguments. |
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|