Call C++ DLL files from Visual Fortran

Call C++ DLL files from Visual Fortran

I am a trying to call C++ function (named: evapdh) in a DLL (edmlib.dll) from a compaq visual fortran 6.6 (CVF) program that I have written. The edmlib.dll file was created by a colleague of mine. In my fortran code I have called evapdh in
the following manner: call evapdh(ev,dh)
Where ev and dh are arrays of type real*4 of length 8 & 5 respectively

Upon building my program I get the following linker error:

Compiling Fortran...
Mari3.obj : error LNK2001: unresolved external symbol _EVAPDH@8
Debug/Mari.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.
Mari.exe - 2 error(s), 0 warning(s)

I read about the LNK2001 error and I think I need to account for the "_" and "@8"
which has been added to the function name. Am I correct in this assumption?

I've read about "ATTRIBUTES Properties and Calling Conventions" in the CVF online help and included the following code in my fortran program to account for the function name mangling:

SUBROUTINE evapdh (ev,dh)
cDEC$ ATTRIBUTES C, ALIAS:'_evapdh@8' :: evapdh ! ia32 systems
REAL*4 ev(8), dh(5)

However, Including this above code caused compilation errors:

Compiling Fortran...
C:MARIMari3.f(1615) : Error: Syntax error, found ',' when expecting one of: ( : % . = =>
cDEC$ ATTRIBUTES C, ALIAS:'_evapdh@8'::evapdh ! ia32 systems
C:MARIMari3.f(1615) : Error: Syntax error, found '::' when expecting one of: , : ) ] /)
cDEC$ ATTRIBUTES C, ALIAS:'_evapdh@8'::evapdh ! ia32 systems
C:MARIMari3.f(1619) : Error: Syntax error, found END-OF-FILE when expecting one of: ; BLOCK BLOCKDATA PROGRAM TYPE COMPLEX BYTE CHARACTER ...
Error executing df.exe.

I have already listed the library name: edmlib.lib in the project setting; under the Project Menu, I clicked Settings and the Link tab. In the "General" category under "Object/Library modules", I listed the edmlib.lib library in addition to the kernel32.lib which was already listed. Thus under the Object/Library modules category the following is listed: kernel32.lib edmLib.lib

Essentially I want to call a c++ function from within my fortran code, and I'm not sure how to do it. Are there any other modifications I need to make to my fortran code to allow me to make calls to functions inside c++ DLL files?
I'd greatly appreciate any feedback!

Thanks in advance

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

The general format for compiler directives is (from the manual)

cDEC$ ATTRIBUTES att [, att] ... :: object [, object] ... 
c s one of the following: C (or c), !, or *.
See Syntax Rules for General Directives.)

but it goes on to say that C (or c) is only allowed in fixed-format files.
Use !DEC$ instead, which is valid in all formats. HTH

Name mangling with C calling convention is different than in STDCALL:

- In C, it'll be "_myCfunc" (or "myCfunc_" on some compilers)
- In STDALL, it'll be "_myCfunc@4".

However, VC++ (and other compilers, each in their own way) will apply by default "C++ name mangling", to enable overloading, kinda "?_myCfunc@YAXXUZ". (Use dumpbin /exports somecppdll.dll to see what I mean) That decoration contains full info about argument and return types, but it's undesired in mixed-language. You have to use extern "C" statement for "exported" C++ routines to prevent that from happening.


Leave a Comment

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