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

using dll's in CVF

johnnyp
Beginner
1,363 Views
Hi All,

I'm trying to call a c++ function (CppFunction) in a C++ DLL from the below fortran program that I've written.


program testfortranmain


real*4 ev(8), dh(5)

ev(1) = 1.0
ev(2) = 23.0
ev(3) = 24.0
ev(4) = 24.0
ev(5) = 15.0
ev(6) = 5.0
ev(7) = 24.0
ev(8) = 14.0


call CppFunction(ev,dh)

end program testfortranmain


Under Project->Settings->Link I've listed the .lib file for the DLL along with it's path.

I keep getting the following link error:
------
Compiling Fortran...
C:MARIEvapModelFortranProj estfortranmain.f
Linking...
testfortranmain.obj : error LNK2001: unresolved external symbol _CppFunction@8
Debug/FortranProj.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

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

Does anyone know what I am doing wrong or how I can get the compiler/linker to recognize the call to
"CppFunction"?

Many Thanks!
0 Kudos
6 Replies
Intel_C_Intel
Employee
1,363 Views
Johny,

you must take care of calling convention. I'd suggest to go through the Chapter 18 in Michael Etzel's book on Mixed Language Programming issues (take a look at compiler Attributes like C/STDCALL and ALIAS. Table 18-2, page 486, is very helpful). An (opposite way) example of calling Fortran DLL from C++ you can find here.

Artur
0 Kudos
Jugoslav_Dujic
Valued Contributor II
1,363 Views
Use dumpbin /exports cppdll.dll to see what the real alias is. See my comments in this thread.

Message Edited by intel.software.network.support on 12-09-2005 09:56 AM

0 Kudos
Steven_L_Intel1
Employee
1,363 Views
My guess is that since the DLL function is in C++, it has a "mangled" name. We haven't seen the C++ declaration yet.

The routine should start out something like this:

extern "C" void __stdcall CppFunction ...


I note that the routine name appears as mixed case in the linker warning, making me wonder if there's a !DEC$ ATTRIBUTES ALIAS there or perhaps /names:asis.

Steve
0 Kudos
johnnyp
Beginner
1,363 Views
Hi,

I tried using the attribute directives described in the online help etc., in the following way:

program testfmain


INTERFACE
SUBROUTINE CppFunction(ev,dh)
!DEC$ ATTRIBUTES DLLIMPORT :: CppFunction
!DEC$ ATTRIBUTES C, ALIAS: '_CppFunction@8':: CppFunction
REAL*4 ev(8), dh(5)
END SUBROUTINE evapdh
END INTERFACE

real*4 ev(8), dh(5)

ev(1) = .0
ev(2) = 23.0
ev(3) = 24.0
ev(4) = 24.0
ev(5) = 15.0
ev(6) = 5.0
ev(7) = 24.0
ev(8) = 14.0


call CppFunction(ev,dh)
end program testfmain
-------
However the directives:
!DEC$ ATTRIBUTES DLLIMPORT :: CppFunction
!DEC$ ATTRIBUTES C, ALIAS: '_CppFunction@8':: CppFunction

becomes commented out in CVF6.0 because a "!" is the comment symbol and comments out the rest of the line.

Is there anyway to get CompaqaVisualFortran to recognize
the directives? These directives should unmangle the c++ function name "CppFunction" correct?


Thanks
0 Kudos
Steven_L_Intel1
Employee
1,363 Views
The directive is being recognized - you can tell because the global name the linker is looking for is in mixed case, due to the ALIAS. If it weren't, it would be looking for _CPPFUNCTION@8.

The problem is in your C++ file, as suggested in my earlier reply. No, the directives don't "unmangle" the name. By default, C++ encodes a routine name and its arguments into a string of gibberish (to us), based on a known algorithm. If you don't know what that gibberish name is, you can't call it from languages other than C++. By adding "C" (in quotes) to the extern declaration, that tells C++ that it's a C routine and to not apply C++ name-mangling.

Steve
0 Kudos
Jugoslav_Dujic
Valued Contributor II
1,362 Views
You should put extern "C" in both definition and declaration (.h file). I'm not a C++ expert and I'm not exactly sure what disadvantages it can potentially bring;
I think in most "normal" cases you can safely use it.
As you see, you needn't use it necessarily -- personally I think using C++ mangled name is just ugly.

0 Kudos
Reply