LNK2019, LNK1120 error - calling FORTRAN .dll

LNK2019, LNK1120 error - calling FORTRAN .dll

Hello,
Im aware that this topic was discussed a lot in past, and many solutions were suggested and created. Anyway, no former solution I found in this forum helped me out, I'm clueless.
I work with VS2008, Intel C++ Compiler and Intel Visual Fortran.
At 1st a created an empty c++ project and added a .cpp file with the following source code:

#include
using namespace std;
extern "C" __cdecl void RDNIST(double *);
int main()
{
double x = 4;
RDNIST(&x);
return 0;
}

In the 2nd step a added a Fortran .dll project to the solution. Code as following:

SUBROUTINE RDNIST(FOO)
REAL(8) :: FOO
FOO = FOO * 2
RETURN
END SUBROUTINE RDNIST

In the c++ code I also tried __stdcall, __declspec(dllimport).
In the Fortran Code I tried also to add: !DEC$ATTRIBUTES REFERENCE, ALIAS: "_RDNIST":: RDNIST

Whatever I tried, I always got these two linker errors:

------ Build started: Project: CFoMix, Configuration: Debug Win32 ------
Compiling with Intel C++ 10.1.025 [IA-32]... (Intel C++ Environment)
cfile.cpp
Linking... (Intel C++ Environment)
xilink: executing 'link'
cfile.obj : error LNK2019: unresolved external symbol _RDNIST referenced in function _main
C:\Visual Studio 2008\Projects\CFoMix\CFoMix\Debug/CFoMix.exe : fatal error LNK1120: 1 unresolved externals
Build log was saved at "file://C:\Visual Studio 2008\Projects\CFoMix\CFoMix\CFoMix\Debug\BuildLog.htm"
CFoMix - 2 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 1 up-to-date, 0 skipped ==========

Unfortunatly, I haven't any clue why it does not want to work.

So if you have any ideas, I would be grateful.

5 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.
Best Reply

So the C code tries to find the symbol "_RDNIST".

Use "dumpbin /symbol fortran-code.dll" to verify if the symbol is there.
You can use /FA to check the asm as well and find out if C code or Fortan code is causing the problem.

Quoting - Jennifer Jiang (Intel)

So the C code tries to find the symbol "_RDNIST".

Use "dumpbin /symbol fortran-code.dll" to verify if the symbol is there.
You can use /FA to check the asm as well and find out if C code or Fortan code is causing the problem.

Hello Jennifer,

so I did like you wrote and got the following outputs from dumpbin:

C:Visual Studio 2008ProjectsCFoMixcfilecfileDebug>dumpbin /symbols rdnist.

dll
Microsoft COFF/PE Dumper Version 9.00.30729.01
Copyright (C) Microsoft Corporation. All rights reserved.
Dump of file rdnist.dll
File Type: DLL
Summary

1000 .data
1000 .idata
2000 .rdata
1000 .reloc
1000 .rsrc
2000 .text

1000 .trace

checking the exports with dumpbin:

C:Visual Studio 2008ProjectsCFoMixcfilecfileDebug>dumpbin /exports rdnist.
dll
Microsoft COFF/PE Dumper Version 9.00.30729.01
Copyright (C) Microsoft Corporation. All rights reserved.
Dump of file rdnist.dll
File Type: DLL
Section contains the following exports for RDNIST.dll
00000000 characteristics
498C1757 time date stamp Fri Feb 06 11:56:23 2009
0.00 version
1 ordinal base
1 number of functions
1 number of names

ordinal hint RVA name

1 0 00001005 RDNIST = @ILT+0(_RDNIST)

Summary

1000 .data
1000 .idata
2000 .rdata
1000 .reloc
1000 .rsrc
2000 .text

1000 .trace

Hmmh? So I have my subroutine in the exports specified and its there obviously. OK, c++ wants my subroutine to be named like _RDNIST, so added the option !DEC$ ATTRIBUTES ALIAS:'_RDNIST' :: RDNIST to the fortran subroutine. I still get those linker errors. Any other ideas?

Instead of creating a Fortran DLL, try to create a Fortran lib instead. Then check to see if the C++ project can link ok.

I'm wondering if the fuction in Fortran got optimized away.

Also check to see if building the C++ project with Visual C++ has any link error.

Jennifer

Quoting - olle.onkel

extern "C" __cdecl void RDNIST(double *);

Just had similar trouble with LNK2019, LNK1120 error combinations. It was witin C++ only, one testdriver using a DLL under test, both made using VS2008 C++.
The symbol of a straight forward function in the DLL could not be referenced by the testdriver during linker run.

After fiddling around a few hours, I found out, that the linking bug was caused by a slight overseen difference in the DLL-header declaration and the implementation of a function. Pretty late error message for that, but better than nothing at all. Obviously, the two function signatures are regarded different overloads of the function by the compiler, one with a declaration and no implementation, the other with exported symbol and implementation but no declaration. Strange, but probably correct at least C++-wise...

Here's the problem isolated out:

// DLL header declaration
__declspec(dllexport) double foo( const char* someString );

// DLL implementation
__declspec(dllexport) double foo( /* omitting this gives linking trouble: const */ char* someString )
{
return 0.0;
}

The solution in my case was to add the const to the implementation of the function.

I would recommend to change your RDNIST declaration a bit, e.g. add a const before the double*, to check
whether you have a similar problem with using your Fortran DLL, such as an accidently overloaded function declaration.

Hope this helps.

Leave a Comment

Please sign in to add a comment. Not a member? Join today