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

FORTRAN calling C code via .lib and .dll

jph
New Contributor I
845 Views

I recently 'inherited' some FORTRAN code that makes use of C functions using the 'INTERFACE' statement and am in the process of rebuilding the FORTRAN code as an 'x64' application, which means that I have to rebuild the C libraries as well. I can (successfully) build these as either (Configuration Type) a Dynamic Library (.dll) (this produces .dll and .lib files; I can then copy the .dll file to the location of the .exe file for the FORTRAN code and then list the .lib file in the Linker 'Additional Dependencies' for the FORTRAN build) or (alternatively) build a Static Library (.lib) (this produces a .lib file that is listed in 'Additional Dependencies').

Unfortunately ...

  • I cannot post any of this code (sorry!)
  • the result from using either option (above) is the same: I get 'unresolved externals' error for each function call (LNK2019 in the Linker) and then the compiler lists the associated .lib file as being 'unused' 

I have very little experience with FORTRAN code that calls C functions and would appreciate any assistance--thanks ...

 

Joe

0 Kudos
1 Solution
Steve_Lionel
Honored Contributor III
724 Views

Change the line to:

!DEC$ ATTRIBUTES C, DECORATE, ALIAS:"CreateUniform" :: CreateUniform

and make similar changes elsewhere. You could also do away with the ATTRIBUTES line by using an INTERFACE block that defines the routine with BIND(C,"CreateUniform"), but that may require further changes such as adding VALUE to pass-by-value dummy arguments.

View solution in original post

9 Replies
Steve_Lionel
Honored Contributor III
799 Views

The steps you say you took look fine. Can you give an example of one of the LNK2019 errors and show the declaration of that routine in the C code? (Just the function itself, no actual code.) If you want to anonymize the name, OK, but do not change case or any prefix or suffix characters.

My guess is that you have a mismatch in expectations for external name decoration. In particular, if your Fortran code explicitly has an alias of the form "_funcname", with the leading underscore, that won't be correct for x64.

jph
New Contributor I
771 Views

I believe that you are completely correct ... all of the errors seem to be of the form:

 

INSENS.obj: error LNK2019: unresolved external symbol _CreateUniform referenced in function INSENS

 

for this same function, the C code (library) has the declaration:

 

void DllExport MYCALLBACK CreateUniform(<arglist> ...)

 

and, for this same function, the file INSENS.FOR contains the line:

 

!DEC$ ATTRIBUTES C, ALIAS:"_CreateUniform :: CreateUniform

 

so, I'm not sure what I need to do--should I just delete this 'alias' line for all of these functions, as they won't be needed for building the 64 bit application? thanks ..

 

Joe

0 Kudos
jph
New Contributor I
755 Views

well, I just deleted all of the 'ALIAS' lines, and I'm still seeing 'unresolved external' errors ... in my example above, it would be (w/o the '_'):

 

INSENS.obj: error LNK2019: unresolved external symbol CreateUniform referenced in function INSENS

 

Joe

0 Kudos
Steve_Lionel
Honored Contributor III
725 Views

Change the line to:

!DEC$ ATTRIBUTES C, DECORATE, ALIAS:"CreateUniform" :: CreateUniform

and make similar changes elsewhere. You could also do away with the ATTRIBUTES line by using an INTERFACE block that defines the routine with BIND(C,"CreateUniform"), but that may require further changes such as adding VALUE to pass-by-value dummy arguments.

jph
New Contributor I
607 Views

hmmm ... now I'm getting:

error #7794: Only a function or subroutine subprogram may have the DEC$ ATTRIBUTES directive DECORATE specifier. [CREATEUNIFORM]

this makes no sense because 'CreateUniform' (et al) IS a subroutine ... I'll look up this error ...

0 Kudos
jph
New Contributor I
598 Views

well, it turned out that the actual name of the subroutine was extending past 132 columns ... so I moved the 'DECORATE' line a few columns to the left, and now everything seems to build ... thanks so much for your help!

Steve_Lionel
Honored Contributor III
591 Views

Glad to hear it. Fixed-form source, eh? You can also add a second !DEC$ ATTRIBUTES line to specify additional attributes.

Please mark my reply above as the answer to your question.

0 Kudos
jph
New Contributor I
583 Views

I'm so sorry--I think that I mistakenly marked my reply as the solution rather than yours ... is there a way to 'undo' this?

 

Joe

0 Kudos
Steve_Lionel
Honored Contributor III
512 Views

It looks as if a moderator fixed it. Thanks.

0 Kudos
Reply