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

Compaq Visual Fortran 6.6: Dynamic-Link Library (DLL) and Module

Bakhbergen
Novice
5,696 Views

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

    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

    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.

 

BTW, the same algorithm without using the DLL works well and gives correct result:

 

    PROGRAM AMAIN3

    USE MODUL3

    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

    S = A + B

    RETURN

    END SUBROUTINE SUBRO3

 

The result is correct:

    S =    3.00000000000000

    Press any key to continue

0 Kudos
1 Solution
Steve_Lionel
Honored Contributor III
5,670 Views

I was able to log in to Bakhbergen's PC and eventually figured out the problem.

In CVF 6.6C (and the later Intel compilers), when you USE a module that has a DLLEXPORT directive, that turns into a DLLIMPORT. But it wasn't always this way, and the version he has doesn't have that behavior. Before that change (which my memory says I lobbied for), you had to supply a separately compiled .mod where the source had DLLIMPORT instead of DLLEXPORT. When I did this, the program worked. I don't remember exactly which update had this change.

So what he needs to do is have two versions of MODUL2.f90, one with DLLEXPORT and one with DLLIMPORT. The DLLEXPORT version gets built into the DLL. The DLLIMPORT version would just be compiled (/c) and only the .mod used, not the ,obj, when linking the main program. Messy, I know, which is why we changed it.

We also discussed why he is using that specific old version, and I understand the reasons (which I will not go into further here.)

View solution in original post

0 Kudos
42 Replies
mecej4
Honored Contributor III
3,153 Views

In the module source, set the DLLEXPORT attribute for the variables A, B and S.

0 Kudos
Steve_Lionel
Honored Contributor III
3,153 Views

This was also asked on StackOverflow and I gave the same advice as mecej4, but I was told it did not work for the OP in CVF. I tested it in Intel Fortran and it worked. Since I no longer have access to CVF I could not try it there, but I'd expect it to work in CVF as well.

0 Kudos
mecej4
Honored Contributor III
3,153 Views

I do have CVF 6.6C and I tested the modified program with it before posting the recommendation. It it did not work for OP, we need to be provided more details.

The Stackoverflow post is at https://stackoverflow.com/questions/54015690/compaq-visual-fortran-6-6-dynamic-link-library-dll-and-module .

0 Kudos
Steve_Lionel
Honored Contributor III
3,153 Views

Please create, and attach to a reply here, a ZIP of the source files and a log of the commands used to build and test the code.

0 Kudos
Bakhbergen
Novice
3,153 Views

mecej4, Steve, I very much appreciate your taking the time to respond to my question. Sorry for the delay in getting back to you. I am eager to solve this problem, and trying to work on this despite my demanding work schedule this week. I’ll get back to you as soon as possible.

0 Kudos
mecej4
Honored Contributor III
3,153 Views

Here are the details. The source file from which the DLL is built, "subro.f90":

MODULE MODUL2
   IMPLICIT NONE
   REAL(8):: A,B,S
!DEC$ ATTRIBUTES DLLEXPORT :: A,B,S
END MODULE MODUL2

SUBROUTINE SUBRO2
  !DEC$ ATTRIBUTES DLLEXPORT :: SUBRO2
   USE MODUL2
   IMPLICIT NONE
   S = A + B
   RETURN
END SUBROUTINE SUBRO2

Command to build the DLL and import library:

df /opt:0 /LD subro.f90

OP's main program, file "amin.f90":

PROGRAM AMAIN2
   USE MODUL2
   implicit none
!
   A = 1D0
   B = 2D0
   CALL SUBRO2
   PRINT*, 'S = ', S
END PROGRAM AMAIN2

Command to build and run:

df /opt:0 /MD amin.f90 subro.lib & amin

 

0 Kudos
Bakhbergen
Novice
3,153 Views

Steve, mecej4, thank you for your time and patience in answering my questions.

mecej4, thanks a lot for the details.

I still can't figure out why I’m getting zero.

Could you please explain what “OP” stands for? As mentioned above, I use the visual development environment for building and calling the DLL.

I’ve attached ZIP of the source files to this post for more details as requested. The DLL building and using steps are provided in the Microsoft Word document.

0 Kudos
mecej4
Honored Contributor III
3,153 Views

I unzipped the attachment to #8, opened AMAIN2.DSW file in the MSDEV IDE, did a rebuild, and then executed the program. It printed out the correct result. Try doing CLEAN and REBUILD.

You have three copies of the DLL in three different subdirectories. In this instance, since they are all identical, there is no harm; however, unless you take steps to ensure that that they remain identical, having multiple copies can cause perplexing behavior.

On Usenet, OP is an abbreviation for "Original Poster". In this thread, that would be "Bekbauov, Bakhbergen".

0 Kudos
Bakhbergen
Novice
3,153 Views

Thanks, mecej4. I have tried doing CLEAN and REBUILD but it still gives zero. I've also tried using PGI Visual Fortran within the Microsoft Visual Studio 10 without success for a while. I greatly appreciate you both for your time and efforts to help me. I will let you know if there is any further update.

0 Kudos
mecej4
Honored Contributor III
3,153 Views

If you don't mind, let us try the following. Please open a CVF command window, create a new directory, copy the source files into that directory and, in that new directory, build the DLL and EXE using the commands that I listed in #7. Then run the resulting EXE.

0 Kudos
Bakhbergen
Novice
3,153 Views

This is a good idea. I have no experience with command line and will do my best to learn it.

0 Kudos
Bakhbergen
Novice
3,153 Views

I've created IVFCVFFORUMCL directory, copied the AMAIN2.F90, MODUL2.F90 and SUBRO2.F90 files into that directory and, in that new directory, tried to build the DLL using the command that you listed in #7. Screenshot of command prompt window with errors is attached. Please let me know how to proceed further. Thanks.

0 Kudos
Steve_Lionel
Honored Contributor III
3,153 Views

You have to compile the module first. Please also TYPE the source files. Also, rather than a screenshot, please copy and paste the whole session contents.

0 Kudos
Bakhbergen
Novice
3,153 Views

Thanks, Steve. Source files' content is exactly the same as mecej4 wrote in #7, but there are some differences in names we used. Please find attached zip file containing the files after I tried actions below. First I used df /compile_only, which created modul2.mod and modul2.obj. Then use of df /dll subro2 creates only subro2.lib and subro2.exp, as well as gives the following:

Microsoft Windows [Version 10.0.17134.523]
(c) Корпорация Майкрософт (Microsoft Corporation), 2018. Все права защищены.

C:\Users\bakhb_000>D:

D:\>cd TemporaryFolder\IVFCVFFORUMCL

D:\TemporaryFolder\IVFCVFFORUMCL>df /compile_only modul2
Compaq Visual Fortran Optimizing Compiler Version 6.6
Copyright 2001 Compaq Computer Corp. All rights reserved.

modul2

D:\TemporaryFolder\IVFCVFFORUMCL>df /dll subro2
Compaq Visual Fortran Optimizing Compiler Version 6.6
Copyright 2001 Compaq Computer Corp. All rights reserved.

subro2
Microsoft (R) Incremental Linker Version 6.00.8447
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.

/entry:_DllMainCRTStartup@12
/ignore:505
/debugtype:cv
/debug:minimal
/pdb:none
C:\Users\BAKHB_~1\AppData\Local\Temp\objB802.tmp
dfordll.lib
msvcrt.lib
dfconsol.lib
dfport.lib
kernel32.lib
/out:subro2.dll
/dll
   Creating library subro2.lib and object subro2.exp
objB802.tmp : error LNK2001: unresolved external symbol _MODUL2_mp_A
objB802.tmp : error LNK2001: unresolved external symbol _MODUL2_mp_B
objB802.tmp : error LNK2001: unresolved external symbol _MODUL2_mp_S
subro2.dll : fatal error LNK1120: 3 unresolved externals

D:\TemporaryFolder\IVFCVFFORUMCL>

0 Kudos
mecej4
Honored Contributor III
3,153 Views

You placed the module source into a separate file, but did not modify the build procedure to match that change. When building the DLL, use the command

df /dll SUBRO2.F90 modul2.obj

 

0 Kudos
Bakhbergen
Novice
3,153 Views

Microsoft Windows [Version 10.0.17134.523]
(c) Корпорация Майкрософт (Microsoft Corporation), 2018. Все права защищены.

C:\Users\bakhb_000>d:

D:\>cd temporaryfolder/ivfcvfforumcl

D:\TemporaryFolder\IVFCVFFORUMCL>df /compile_only modul2.f90
Compaq Visual Fortran Optimizing Compiler Version 6.6
Copyright 2001 Compaq Computer Corp. All rights reserved.

modul2.f90

D:\TemporaryFolder\IVFCVFFORUMCL>df /dll subro2.f90 modul2.obj
Compaq Visual Fortran Optimizing Compiler Version 6.6
Copyright 2001 Compaq Computer Corp. All rights reserved.

subro2.f90
Microsoft (R) Incremental Linker Version 6.00.8447
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.

/entry:_DllMainCRTStartup@12
/ignore:505
/debugtype:cv
/debug:minimal
/pdb:none
C:\Users\BAKHB_~1\AppData\Local\Temp\obj7132.tmp
modul2.obj
dfordll.lib
msvcrt.lib
dfconsol.lib
dfport.lib
kernel32.lib
/out:subro2.dll
/dll
   Creating library subro2.lib and object subro2.exp
LINK : warning LNK4098: defaultlib "libc.lib" conflicts with use of other libs; use /NODEFAULTLIB:library

D:\TemporaryFolder\IVFCVFFORUMCL>df amain2.f90 modul2.obj subro2.lib & amain2
Compaq Visual Fortran Optimizing Compiler Version 6.6
Copyright 2001 Compaq Computer Corp. All rights reserved.

amain2.f90
Microsoft (R) Incremental Linker Version 6.00.8447
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.

/subsystem:console
/entry:mainCRTStartup
/ignore:505
/debugtype:cv
/debug:minimal
/pdb:none
C:\Users\BAKHB_~1\AppData\Local\Temp\obj5BCC.tmp
modul2.obj
subro2.lib
dfor.lib
libc.lib
dfconsol.lib
dfport.lib
kernel32.lib
/out:amain2.exe
   Creating library amain2.lib and object amain2.exp
 S =   0.000000000000000E+000

D:\TemporaryFolder\IVFCVFFORUMCL>

0 Kudos
mecej4
Honored Contributor III
3,153 Views

That is puzzling. I do notice one point that may have a bearing on the problem: your version of CVF is slightly older than the one I have:

Compaq Visual Fortran Optimizing Compiler Version 6.6 (Update C)
Copyright 2003 Compaq Computer Corp. All rights reserved.

I even took the MOD, LIB and DLL files from your zip in #8, and built amain2.exe from amain2.f90 and those files. The output was 3.0.

Other than that, I do not have any more suggestions for you; sorry!

0 Kudos
Bakhbergen
Novice
3,153 Views

Thanks.

0 Kudos
Steve_Lionel
Honored Contributor III
3,153 Views

Please retry this, adding /MD to each of the df commands. On the last one, please use:

df /MD amain2.f90 modul2.obj subro2.lib /link /map & amain2

Then zip amain2.map and attach it to a reply here.

0 Kudos
Steve_Lionel
Honored Contributor III
3,009 Views

I would not expect to see "Creating library amain2.lib and object amain2.exp" when linking the main program. Here's an interesting suggestion - instead of "df" use "f90" - I think that was supported (has been a VERY long time). There are some other suspicious things in the output of the df command that builds the executable.

0 Kudos
Reply