Calling a DLL FORTRAN with C++

Calling a DLL FORTRAN with C++

I have the following function (it's an example) made with FORTRAN in a DLL

integer(2) function suma_int(a,b)
!DEC$ ATTRIBUTES ALIAS:'suma_int' :: suma_int

integer(2) a,b

end function

Now I need to call this function from an external program made with Visual C++ and I dont know how call it.

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

You have to declare your function in .cpp (or, better, a .h file) like this:

extern "C" __declspec(dllimport) short __stdcall suma_int(short* a, short* b); 

extern "C" should be mandatory for all symbols "imported" to C++. __declspec(dllimport) is equivalent to !DEC$ATTRIBUTES DLLIMPORT. __stdcall specifies stdcall calling convention (CVF default). Arguments have to be pointers because CVF passes arguments by reference by default (if you had specified !DEC$ATTRIBUTES VALUE for these, you should omit *'s from C declaration).

Include yourdll.lib that CVF creates for you into VC++ project, and that should be it.


Jugoslav, I've done a C++ program with your indications, but I obtain the next error

--------------------Configuration: Call_C - Win32 Debug--------------------
main.obj : error LNK2001: unresolved external symbol __imp__suma_int@8
Debug/Call_C.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

Call_C.exe - 2 error(s), 0 warning(s)

I've included mydll.lib in Project->Settings->Link->Category->Input->Object/library modules. I show you C++ code


extern "C" __declspec(dllimport) short __stdcall suma_int(short* a,short* b);

void main ()
short suma;
short a,b;



Thanks Jugoslav

Oh, yes, I realized that it might happen after I posted already. The problem is in ALIAS directive. When you say _stdcall suma_int, C++ expects that it will find name "_suma_int@8". However, using ALIAS you export "suma_int" so there's a name mismatch. Now, I'm not versatile enough in C++ to tell you how to fix it on C++ side -- however, the obvious answer would be to ALIAS it with "_suma_int@8" in Fortran code. (If you don't do ALIAS at all, it would be _SUMA_INT@8 -- CVF uppercases by default).

Leave a Comment

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