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!
using dll's in CVF
using dll's in CVF
Nähere Informationen zur Compiler-Optimierung finden Sie in unserem Optimierungshinweis.
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
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
Jugoslav
www.xeffort.com
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:
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
Steve
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
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
Steve
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.
Jugoslav
www.xeffort.com



