Dynamic Link Library, Compaq Visual Fortran 6.6 & Module

Click For Summary

Discussion Overview

The discussion revolves around the creation and use of dynamic-link libraries (DLLs) in Fortran applications using Compaq Visual Fortran 6.6. Participants explore issues related to variable scope and initialization when using modules in DLLs compared to traditional subroutines.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant shares a working example of a DLL subroutine that correctly computes a sum, while another example using a module yields an incorrect result of zero.
  • Another participant suggests that the module may only declare local variables and proposes using COMMON blocks to make variables global.
  • Some participants reference external sources claiming that modules have global characteristics, questioning the inconsistency observed in the results.
  • A participant notes that the same algorithm without using the DLL works correctly, indicating that the issue may be specific to the DLL implementation.
  • Concerns are raised about different address spaces being assigned in the DLL and the main executable, which could lead to the observed discrepancies.

Areas of Agreement / Disagreement

Participants express differing views on the behavior of modules in DLLs, with some suggesting a need for global variable handling while others reference external sources that claim global characteristics. The discussion remains unresolved regarding the underlying cause of the incorrect results.

Contextual Notes

There are limitations regarding the assumptions about variable scope and initialization in the context of DLLs and modules. The discussion does not resolve the mathematical or technical steps involved in the implementation.

Bakhbergen
Messages
3
Reaction score
1
Dear Forum Members,
I need to create and use dynamic-link library (DLL) for Fortran application using Compaq Visual Fortran 6.6. The following code works just fine:

###########################################
PROGRAM AMAIN1
IMPLICIT NONE
REAL(8):: A,B,S
A = 1D0
B = 2D0
CALL SUBRO1(A,B,S)
PRINT*, 'S = ', S
END PROGRAM AMAIN1
###########################################
SUBROUTINE SUBRO1(A,B,S)
!DEC$ ATTRIBUTES DLLEXPORT :: SUBRO1
IMPLICIT NONE
REAL(8):: A,B,S
S = A + B
RETURN
END SUBROUTINE SUBRO1
###########################################
The result is correct:
S = 3.00000000000000
Press any key to continue
###########################################

However, if I implement the same algorithm using the module, I get inconsistent result (i.e. zero):

###########################################
PROGRAM AMAIN2
USE MODUL2
IMPLICIT NONE
A = 1D0
B = 2D0
CALL SUBRO2
PRINT*, 'S = ', S
END PROGRAM AMAIN2
###########################################
MODULE MODUL2
IMPLICIT NONE
REAL(8):: A,B,S
END MODULE MODUL2
###########################################
SUBROUTINE SUBRO2
!DEC$ ATTRIBUTES DLLEXPORT :: SUBRO2
USE MODUL2
IMPLICIT NONE
S = A + B
RETURN
END SUBROUTINE SUBRO2
###########################################
The result is incorrect:
S = 0.000000000000000E+000
Press any key to continue
###########################################

As can be seen above, DLL contains only subprogram in both cases (SUBRO1 and SUBRO2, respectively). I have built DLL and LIB files from the visual development environment. The second case (with the use of module) represents the structure of my large source-code so I need to resolve this issue. Any advice would be greatly appreciated.
 
Physics news on Phys.org
Hallo Bakhbergen, ##\qquad## :welcome:

Modul2 is only a declaration of local variables. You need something like COMMON to make them global is what I would suspect.

(old hand, more fortran 77 than f90, but who knows this helps)
 
  • Like
Likes   Reactions: Bakhbergen
  • Like
Likes   Reactions: Bakhbergen
BvU said:
However, https://www.tutorialspoint.com/fortran/fortran_modules.htm claims global character.
Your result does not reflect that. Still worth investigating (ask for the addresses of the variables in debug or something ?)

Hello BvU! I appreciate your intention to help with this. There weren't any error / warning during the debugging. Just gives incorrect result.
 
I don't expect any errors reported, just diffferent address spaces assigned in the DLL and the main EXE
 
  • Like
Likes   Reactions: Bakhbergen
BTW, the same algorithm without using the DLL works well and gives correct result:

PROGRAM AMAIN3
USE MODUL3
IMPLICIT NONE
A = 1D0
B = 2D0
CALL SUBRO3
PRINT*, 'S = ', S
END PROGRAM AMAIN3

MODULE MODUL3
IMPLICIT NONE
REAL(8):: A,B,S
END MODULE MODUL3

SUBROUTINE SUBRO3
USE MODUL3
IMPLICIT NONE
S = A + B
RETURN
END SUBROUTINE SUBRO3

S = 3.00000000000000
Press any key to continue
 

Similar threads

  • · Replies 7 ·
Replies
7
Views
2K
Replies
7
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
6K
  • · Replies 3 ·
Replies
3
Views
5K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K