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

mixed language

teplick
Beginner
553 Views
I am trying to call a C++ procedure in a static lib from Intel FORTRAN using VS2003.

The C++ project code is

#include

extern "C" void _stdcall _CDEMO(void)
{

}

and the FORTRAN project code is

program Console1

implicit none
interface exp
subroutine CDEMO()
!DEC$ ATTRIBUTES C, ALIAS:'_CDEMO':: CDEMO
end subroutine CDEMO
end interface exp

print *, 'Calling C++'

call CDEMO()

end program Console1

The FORTRAN linker has the full path and filename of the C++ CDEMO.LIB but no matter what I try I continue to get the linker error _CDEMO undefined from the FORTRAN program. I'm sure I'm missing something simple and would appreciate any help.
0 Kudos
4 Replies
Jugoslav_Dujic
Valued Contributor II
553 Views
1) You mismatched calling convention -- it's stdcall in C++ but cdecl in Fortran

2) You didn't get the alias right. Stdcall routines have alias "_FuncName@n", where "_" and "@n" are appended; cdecl routines have "_FuncName". If you don't want to care about decoration, you can use DECORATE attribute. So, the following combinations are appropriate:

extern "C" void _stdcall CDEMO(void)
!DEC$ ATTRIBUTES STDCALL, ALIAS: '_CDEMO@0':: CDEMO
!DEC$ ATTRIBUTES STDCALL, DECORATE, ALIAS: 'CDEMO':: CDEMO
.
extern "C" void _cdecl CDEMO(void)
!DEC$ ATTRIBUTES C, ALIAS: '_CDEMO':: CDEMO
!DEC$ ATTRIBUTES C, DECORATE, ALIAS: 'CDEMO':: CDEMO
.
extern "C" void _stdcall _CDEMO(void)
!DEC$ ATTRIBUTES STDCALL, ALIAS: '__CDEMO@0':: CDEMO
!DEC$ ATTRIBUTES STDCALL, DECORATE, ALIAS: '_CDEMO':: CDEMO


Leading underscores in function names are not allowed in Fortran, and not recommended in C++ either (reserved for system and compiler-specific names), so it's better to avoid them.

Jugoslav
0 Kudos
teplick
Beginner
553 Views
Thanks for your help. However, I still can't get it to work. It seems as if it can't find the C++ library. I have used dumpbin to insure that_CDEMO@0 is present in CDEMO.lib (it is) & think I have included the correct path in the additional library property of the FORTRAN program linker. I have even tried moving CDEMO.lib into the source and Debug files of the FORTRAN driver but it still won't find _CDEMO@0. Also, I can't find any documentation to the "DECORATE" attribute & the compiler won't allow it.

I appreciate greatly your efforts and your continuing help
Dick
0 Kudos
Jugoslav_Dujic
Valued Contributor II
553 Views
Ah, sorry, I missed that you mentioned "Intel Fortran" -- I thought it was CVF, which has DECORATE attribute.

With Intel Fortran, your options are more limited. If there is indeed _CDEMO@0 exported from cdemo.dll, one of the following options works (I don't recall which one at the moment, I believe the first one):

1) Type "cdemo.lib" in Project/Settings/Link/Input/Object-libarary modules (it looks as if you forgot this step). Type the directory of cdemo.lib into "Additional library path" edit box.

2) Project->Add to project->Files->cdemo.lib. If I recall correctly, this does not work for IF, but only for CVF (but I'm not certain).

ALIAS attribute must be "_CDEMO@0".

Jugoslav
0 Kudos
teplick
Beginner
553 Views
Thanks. All I had to do is add CDEMO.lib to "additional dependencies" for the linker and it works!. Thank you so much for all of your help.
Dick
0 Kudos
Reply