I am having problems importing a DLL into my current Fortran project. The DLL file I am trying to import, fdlltest.dll
, has the following functions defined when I do dumpbin /exports
:
C:\temp>dumpbin /exports fdlltest.dll
Microsoft (R) COFF/PE Dumper Version 12.00.40629.0
Copyright (C) Microsoft Corporation. All rights reserved.
Dump of file fdlltest.dll
File Type: DLL
Section contains the following exports for fdlltest.dll
00000000 characteristics
560ED478 time date stamp Fri Oct 02 14:01:12 2015
0.00 version
1 ordinal base
3 number of functions
3 number of names
ordinal hint RVA name
1 0 00001000 add2i
2 1 00001010 add2r
3 2 00001040 simpson
Summary
1000 .data
1000 .rdata
1000 .reloc
1000 .rsrc
1000 .text
Next, I need to know step-by-step how to import the add2i
, add2r
, and simpson
functions from this DLL located in C:\temp
into my current Fortran project. I do not want examples with *.lib, since the actual DLL I want to use once I get past this example does not have a companion *.lib. This is my Fortran code which is supposed to generate an EXE that is linked to the DLL file:
program fdllrun
implicit none
INTERFACE
INTEGER FUNCTION add2i(a,b)
!DEC$ ATTRIBUTES DLLIMPORT, ALIAS:"add2i" :: add2i
!DEC$ ATTRIBUTES REFERENCE::a, b
INTEGER, intent(in), value:: a, b
END FUNCTION add2i
REAL FUNCTION add2r(a,b)
!DEC$ ATTRIBUTES DLLIMPORT, ALIAS:"add2r" :: add2r
!DEC$ ATTRIBUTES REFERENCE::a, b
REAL, intent(in), value:: a, b
END FUNCTION add2r
REAL FUNCTION simpson(f, a, b, n)
!DEC$ ATTRIBUTES DLLIMPORT, ALIAS:"simpson" :: simpson
!DEC$ ATTRIBUTES REFERENCE::f, a, b, n
EXTERNAL f
real, intent(in), value :: a, b
integer, intent(in), value :: n
END FUNCTION simpson
END INTERFACE
! Variables
INTEGER :: i1, i2, ians
i1=1
i2=2
! Body of fdllrun
ians = add2i(i1, i2)
print '(I3)', ians
end program fdllrun
In Intel Visual Studio Fortran, I tried to right click on my project and do Add -> Existing Item, then browse to the DLL file located in C:\temp, then Build All. It does not work. The errors I get when I try to compile are:
Error 1 error LNK2019: unresolved external symbol __imp_add2i referenced in function _MAIN__ fdllrun.obj
Error 2 fatal error LNK1120: 1 unresolved externals Release\fdllrun.exe
I have tried several other Fortran compilers out there, including g95 and gfortran (both MinGW and TDM-GCC varieties), but have not had any luck with anything I have tried. TDM-GCC gfortran is perhaps the closest I got, as I could run a DLL created from gfortran, but not in C. In fact, in TDM-GCC gfortran, after I created a Fortran DLL that was referencing a C DLL, all function calls to the Fortran generated DLL within Excel VBA would immediately return 0, even after no warnings were given after compiling the Fortran DLL! In other cases, it said it could not find the function name in the DLL, even though I made them without decorations or underscores. Excluding the C DLL from that build would eliminate that problem, but of course, I need to reference the C DLL as it has required 3rd party functions.
I looked at other posts and could not find an answer to this question:
My question is not importing a Fortran DLL into c++ or C#
I do not get the "Add as Link" option when right clicking Add->Existing Item
The answer from this post did not solve my question
A similar question here was not answered adequately
Please provide a step-by-step guide to getting Fortran to work with DLLIMPORT, with both gfortran and Intel Visual Studio Fortran, if possible. I just want something that works...