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

C++ calling intel fortran subroutine

Deleted_U_Intel
Employee
919 Views
Dear Concern,
I've a problem in linking the c++ .obj file and fortran .obj file .
What I trying to do is that :-
I write a program in C++ as given below(file name is c.cpp)
#include
#include
extern "C"
{
void __stdcall FR1(int *,int *,char *);
}
void main()
{
int n=10,nSq;
char szCtest[20];
strcpy(szCtest,"teststring");
FR1(&n,&nSq,szCtest);
cout << "The square is:" << nSq << endl;
}
This programcalls a subroutine written in fortran 90 ( file name is fr.f90)
SUBROUTINE FR1(N,M,CSTR)
INTEGER*4 CSTR(1)
M=N*N
WRITE(6,20) (CSTR(L),L=1,3)
20 FORMAT(' CSTR=',3A4)
WRITE(6,*) 'DONE'
RETURN
END
I compile both the programs with the help of Intel Visual fortran compiler 8.0 for Windows as
c:>ifort -c fr.f90 (for fortran program)
c:>cl -c c.cpp(for C++ program)
Both programscompiled successfully.
But when I try to link the programs with the command
C:>XILINK c.obj fr.obj
it generates error as
C:>xilink c.obj fr.obj
xilink: executing 'link'
Microsoft Incremental Linker Version 6.00.8168
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.
c.obj
fr.obj
c.obj : error LNK2001: unresolved external symbol _FR1@12
c.exe : fatal error LNK1120: 1 unresolved externals
Please look into this problem.
Thanx
Vikramjit
0 Kudos
4 Replies
isn-removed200637
919 Views
Try adding the following to your FORTRAN file after the SUBROUTINE statement
!DEC$ ATTRIBUTES STDCALL, ALIAS : '_FR1@16' :: FR1

Hope this helps.
0 Kudos
isn-removed200637
919 Views
I'm sorry..that should be'_FR1@12', ( 12 bytes because 3 arguments, each 4bytes).HTH
0 Kudos
Jugoslav_Dujic
Valued Contributor II
919 Views

No -- Intel Fortran changed the default calling convention from __stdcall to __cdecl. It should work if you change the prototype to __cdecl. I'm not sure what happened with case (whether it's uppercase or lowercase) -- try "fr1" instead.

Jugoslav

0 Kudos
TimP
Honored Contributor III
919 Views

That looks more like f66 than f90. No matter, it will be fun mixing extended f66 with C++. You have already demonstrated on of the pitfalls of Microsoft-specific calling conventions; they change every 2 or 3 years, a mere moment considering that you are using a 30 year old programming style. A more portable way would be simply to use

extern "C" FR1(...

Then you could run dumpbin /symbols on both Fortran and C++ .obj files, to see if you need any adjustments on the C++ side.

To me, it's more than a little strange to pass a char*, treat it as a pointer to an array of 1 32-bit integer, then access out of bounds, using f66 notation.

You also create huge difficulties for yourself, attempting to write to stdout with 2 conflicting run-time libraries.

0 Kudos
Reply