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

Calling Fortran DLL from C++ using Visual Studio 2010

Omun_Kwon
Beginner
1,069 Views
I have tried to use Visual Studio 2010 (C++) with fortran DLL (Intel Fortran XE). Based on the posting in previous thread "Calling Fortran DLL from C++ using Visual Studio 2008", I have created simple fortran DLL with sucess, but I can't go through in making executable file in Visaul studio 2010 C++. It looks like DLL link issue in Visual studio 2010. I copied the DLL file into the main C++ folder. I have following error meassage.

error LNK2019: unresolved external symbol "__declspec(dllimport) void __cdecl Dll3(int *,int *)" (__imp_?Dll3@@YAXPAH0@Z) referenced in function _wmain
.\\Mixed3.exe : fatal error LNK1120: 1 unresolved externals
Please give me any idea how to resolve the problem.
The Fortran main routine is as follows
subroutine Dll3(N,M)
! Expose subroutine Dll3 to users of this DLL
!
!DEC$ ATTRIBUTES DLLEXPORT::Dll3
! Variables
integer n,m
M=N*N
! Body of Dll3
end subroutine Dll3
THis is C++ routine.
#include "stdafx.h"
#include
#include
extern __declspec(dllimport) void Dll3(int *a, int *b);
int _tmain(int argc, _TCHAR* argv[])
{
int a,b=0;
a=35;
Dll3(&a,&b);

}
0 Kudos
5 Replies
IanH
Honored Contributor III
1,069 Views
You are missing the "C" qualifer aftern the extern keyword, plus you have mixed case in the function name on the C++ side.

In the C++ code:

extern "C" __declspec(dllimport) void Dll3(int* a, int* b);

Without that the C++ compiler thinks the function has C++ linkage. C++ linkage requires (in order for function overloading to work) that the compiler "mangle" together the name of the function with the type of its arguments to form the linker name - hence the "__imp_?Dll3@@..." part of the error message. You don't want this. The "C" qualifier says to use C linkage for the function, which doesn't mangle in the arguments and is something that the Fortran compiler can match.

In the Fortran code:

SUBROUTINE Dll3(N,M) BIND(C, NAME='Dll3')

This tells the Fortran compiler that the linker name of your Fortran procedure needs to match the linker name that would be generated for a C function named "Dll3". Without the BIND clause different compilers may have different behaviour (in Intel Fortran's case I think the default is that the linker name is an upper case version of the procedure name), without the NAME specifier the compiler must generate an all lowercase linker name. The BIND clause also tells the Fortran compiler that the calling convention of the procedure needs to be such that it can be called by C (in Intel Fortran's case I think this is the default, but regardless it is better to be explicit about such things).
0 Kudos
mecej4
Honored Contributor III
1,069 Views
If you are not heavily invested in code replete with directives you may find it easier to use the portable C-interoperability features available in Intel Fortran rather than using directives and worrying about name decoration.
0 Kudos
Omun_Kwon
Beginner
1,069 Views
Your answer partially works, but it is still error after modification. I have following message.

Mixed3.obj : error LNK2019: unresolved external symbol __imp__Dll3 referenced in function _wmain
1>..\Mixed3\Debug\Mixed3.exe : fatal error LNK1120: 1 unresolved externals
Is there anything I missed on the coding or visual C++ configuration (Visual Studio 2010)?
In Fortran, I modified like this.
SUBROUTINE Dll3(N,M) BIND(C, NAME='Dll3')
!DEC$ ATTRIBUTES DLLEXPORT::Dll3
integer n,m
M=N*N
END SUBROUTINE Dll3
In Visual C++,
#include "stdafx.h"
#include
#include
extern "C" __declspec(dllimport) void Dll3(int* a, int* b);
int _tmain(int argc, _TCHAR* argv[])
{
int a,b=0;
a=35;
Dll3(&a,&b);
}
0 Kudos
Omun_Kwon
Beginner
1,069 Views
Eventually I figured out the issue. I added Dll3.lib on the source files in C++ side. After that, it works fine. I don't know why it fails again with Dll3.dll. Does anybody know the reason? Why do I include *.Lib file instead of *.Dll?
Thanks for the help.
0 Kudos
Steven_L_Intel1
Employee
1,069 Views
On Windows, one links to "export libraries" - the .LIB. The .DLL is not linkable but is used when the program is run.
0 Kudos
Reply