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

Pass Module Variables From Main Program to a DLL

Jim_B_3
Beginner
475 Views

I am trying to pass some module variables from a main Fortran program to a dll.  It gets compilicated in that these files get linked with ANSYS but I think I'm missing something fundamental with my Fortran programs or dll creation.  I've created a simple example to demonstrate this.

I have been able to compile, link, and run but I'm not getting the module variables with the right values in my dll.  Since this does link with ANSYS I am not using A visual studio project.  I use some simple scripts to compile and link.

Here is the dll.

      module my_mod
     
contains
      subroutine my_print
         use mod_seven
   !DEC$ ATTRIBUTES DLLEXPORT :: my_print
! can't compile with this and use mod_seven statement         !DEC$ ATTRIBUTES DLLIMPORT :: seven
   write(*,*) ' ===== MyPrint called from USolBeg'
   write(*,*) ' ===== modified after creating custom ANSYS executable'
         write(*,*) ' SEVEN in my_mod.dll = ', seven
      return
      end subroutine my_print
      end module my_mod

 

Here is the module I compile with my main program (and I have to include the mod_seven.obj in the dll creation)

      module mod_seven
      !DEC$ ATTRIBUTES DLLEXPORT :: seven
      integer   seven
      contains
      subroutine set_seven
      seven = 7
      write(*,*) "In Module mod_seven, setting SEVEN to ",SEVEN
      return
      end subroutine set_seven
     
end module mod_seven

This is a subroutine in the "main" program that is called by ANSYS.

*deck,USolBeg      USERDISTRIB        lcase                             jas
      subroutine USolBeg
     USE my_mod
      USE mod_seven
     
#include "impcom.inc"
   
#include "syspar.inc"
     
call set_seven
      write(*,*) ' SEVEN in USolbeg.f = ', seven
      call my_print
     
return
      end    

My latest attempt gives me this for output
 In Module mod_seven, setting SEVEN to            7
  SEVEN in USolbeg.f =            0
  ===== MyPrint called from USolBeg
  ===== modified after creating custom ANSYS executable
  SEVEN in my_mod.dll =            0

I believe module mod_seven is accessing 2 different memory locations.  One for the main program and 1 for the dll.

I'd like the main program and the dll to use the same location.

 

Here is how I am compileing and linking:

1. compile mod_seven.f and usolbeg.f (these are in directory 1_upf)
set INCLUDE=..\2_dll;%INCLUDE%
set LIB=..\2_dll;%LIB%
ifort -D__EFL -DNOSTDCALL -DARGTRAIL /O2 -DPCWIN64_SYS -DPCWINX64_SYS -DPCWINNT_SYS -DCADOE_ANSYS /fpp /4Yportlib /auto /c /Fo.\ /DFORTRAN /MD /watch:source %%P >>compile.log  2>&1

2.  compile dll file my_mod.f  (these are in directory 2_dll)
set INCLUDE=..\1_upf;%INCLUDE%
set LIB=..\1_upf;%LIB%
ifort /c /align:common my_mod.F
ifort  /dll my_mod.obj mod_seven.obj /link

 

3. Link it all together with ansys
set INCLUDE=..\2_dll;..\1_upf;%INCLUDE%
set LIB=..\2_dll;..\1_upf;%LIB%
link @ansys.lrf

 

ansys.lrf includes (among a bunch of ANSYS specific items)
-out:ANSYS.exe
-map:ANSYS.map
-def:ansysex.def
-opt:ref
-machine:X64
-subsystem:windows
-stack:0x2000000
-manifest
-manifestfile:ansys.exe.intermediate.manifest

-defaultlib:my_mod.lib

Thanks
Jim

 

 

 

 

 

 

 

 

0 Kudos
1 Reply
Steven_L_Intel1
Employee
475 Views

I start to get a bit confused when you mix in ANSYS, but fundamentally this should work. You correctly DLLEXPORT the variable in the module - as you found you can't export it again. The executable needs to USE that module and link to the export .lib created when the DLL was linked 

0 Kudos
Reply