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

Calling FORTRAN DLL from C++

dk418
Beginner
687 Views
Hi,

I built a fortran DLL library, and I want to call the subroutines from C++, but I get the following error.

MessengerServer.obj : error LNK2001: unresolved external symbol __imp__ENGAGEABILITY_INIT@0
Debug/MessengerServer.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

I declared the dll subroutines as follows:

extern "C" __declspec(dllimport) void _stdcall ENGAGEABILITY_INIT();

this is in C++ file.

I also export the subroutine from the fortran side.


subroutine Engageability_Init()
!DEC$ ATTRIBUTES DLLEXPORT :: ENGAGEABILITY_INIT

What am I doing wrong?

Thanks
0 Kudos
7 Replies
Jugoslav_Dujic
Valued Contributor II
687 Views
It sounds as if you did not insert yourdll.lib (from YourDllDir/Debug) into VC++ project.

Jugoslav
0 Kudos
dk418
Beginner
687 Views
I do have the dll.lib included in the project setting under link tab. But, I still get the unresolved external symbol error.

I do have the access to the fortran source code, is it easier to bypass using dll and include the Fortran source directly in the C++ project and use it? How would I do this?
0 Kudos
Jugoslav_Dujic
Valued Contributor II
687 Views
I prefer to have it directly inserted into project (as any other source file). If you list it in Project/Settings/Link, you have to specify full path there (or add the path in Tools/Options/Directories/Library files).

It is possible to mix F90 and C++ files directly in one project -- just insert them together (and remove dllexport/dllimport directives).
0 Kudos
dk418
Beginner
687 Views
Still having no luck with the unresolved external symbol error. I tried including the source directly in the project and removed the dll directives, and I still get the same error message.

Any possible leads?
0 Kudos
Jugoslav_Dujic
Valued Contributor II
687 Views
Sigh... I've got no more clues. Attached is a sample mixed-language workspace so I can only suggest you perform some comparisons.
0 Kudos
dk418
Beginner
687 Views
The Fortran function that I'm trying to call from C++ is a member of Fortran module, could this be the problem?
In Fortran, I type "use [moduleName]", do I need to do a similar thing in C++?

0 Kudos
Jugoslav_Dujic
Valued Contributor II
687 Views
Yes, that's it! CVF mangles module procedure names as _MODULENAME_mp_ROUTINENAME@n (case matters). You can either refer to it in C under that name, or use ALIAS directive in Fortran to rename it to something nicer:
MODULE Something
...
SUBROUTINE ENVIRONMENT_INIT()
!DEC$ATTRIBUTES ALIAS: "_EnvironmentInit@0":: ENVIRONMENT_INIT
...
extern "C" void __stdcall EnvironmentInit(void); 


Jugoslav
0 Kudos
Reply