Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29391 Discussions

Using modules with global variables along with external routines in DLLs

millerdl
Beginner
837 Views

Hi all,

I am having some problems with using global variables with external routines using DLLs. I compile something like this into a DLL:

MODULE my_mod

IMPLICIT NONE

INTEGER, SAVE :: key

EXTERNAL test

CONTAINS

SUBROUTINE call_test(key)

call test(1)

END SUBROUTINE call_test

END MODULE my_mod

SUBROUTINE test(i)

USE my_mod

IMPLICIT NONE

INTEGER :: i

PRINT*,"i=",i

PRINT*,"key=",key

END SUBROUTINE test

I then call the subroutine call_test() from, say, R with the key value as 2 I get the following output:

i=1

key=0

Apologies if I am missing something very obvious I am a Fortran 77 programmer attempting to learn F90/95.

Thanks in advance!

0 Kudos
1 Solution
thomas_boehme
New Contributor II
837 Views

In your subroutine call_test you are declaring key as an argument. Therefore, key will be a local variable to the subroutine and not the module variable.
Modify your code like that and it should work:
SUBROUTINE call_test(keyval)
key = keyval
call test(1)
END SUBROUTINE call_test
regards,
Thomas

View solution in original post

0 Kudos
2 Replies
thomas_boehme
New Contributor II
838 Views

In your subroutine call_test you are declaring key as an argument. Therefore, key will be a local variable to the subroutine and not the module variable.
Modify your code like that and it should work:
SUBROUTINE call_test(keyval)
key = keyval
call test(1)
END SUBROUTINE call_test
regards,
Thomas
0 Kudos
millerdl
Beginner
837 Views
Thanks Thomas, that's solved my problem! I can't believe I didn't think of doing that!

Quoting - thomas_boehme

In your subroutine call_test you are declaring key as an argument. Therefore, key will be a local variable to the subroutine and not the module variable.
Modify your code like that and it should work:
SUBROUTINE call_test(keyval)
key = keyval
call test(1)
END SUBROUTINE call_test
regards,
Thomas

0 Kudos
Reply