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

Defining a FORTRAN routine callable from C++

anthonyrichards
New Contributor III
601 Views
I use a third-party application which permits access to a user-writtenroutine via a DDE interface. The DDE interface is managed by a client program written in C++. I have access to the code. This client program assumes that the userroutine is written in C++, but since FORTRAN is my language, I want the client program to access a FORTRAN function. WIthin the client program code, the reference to the user function is thus:
void UserFunction(char *szCommandLine);
The argument is defined as a 260-character string variable. What I would be glad to know is what whould this definition statement be changed to in the C-codeand what matching compiler directive I should use in my FORTRAN version of USERFUNCTION? For the latter, I have in mind
SUBROUTINE USERFUNCTION( STRING)
!DEC$ ATTRIBUTES REFERENCE, C :: USERFUNCTION
CHARACTER*(*)STRING
I have a C++ compiler, so I envisage compiling the C-code (which contains the WINMAIN)seperately into an .OBJ file and includingthe .OBJ fileinto a FORTRAN project consisting of my USERFUNCTION and creating an executable from it. ANy other tips would be appreciated. Thanks in advance.
0 Kudos
2 Replies
Steven_L_Intel1
Employee
601 Views
You're on the right track. You will also need a:

!DEC$ ATTRIBUTES REFERENCE :: STRING

to tell the compiler not to look for a separate length argument. You;'ll then need to use INDEX to find the trailing NUL to find the string length or be sure to write a NUL (or CHAR(0) or ""C) to define the length.

Remember also that C is case sensitive, and that because you used attribute C, the external name will be _userfunction.
0 Kudos
anthonyrichards
New Contributor III
601 Views
Thanks, Steve, for that tip. I was getting access errors and crashing the program trying to play with the STRING. even though a MESSAGEBOX call in the FORTRAN routine displayed it OK. The following works for me
In the C++ code:(I made the function name UPPERCASE)
Code:
extern "C" { void __cdecl USERFUNCTION(char *szCommandLine); }
...
...
char szCommandLine[260];
...
...
 USERFUNCTION(szCommandLine);

(This produces an .OBJ with the symbol _USERFUNCTION in it)
In the FORTRAN:
Code:
SUBROUTINE USERFUNCTION(STRING)
!
!DEC$ ATTRIBUTES  C, REFERENCE, ALIAS : '_USERFUNCTION' :: USERFUNCTION
!DEC$ ATTRIBUTES REFERENCE :: STRING
CHARACTER(260) STRING

I found if I used '__stdcall' on the C++ side, the symbol _USERFUNCTION@4 was produced in the .OBJ, so I have to change the ALIAS on the FORTRAN side to match it. Thanks again.

0 Kudos
Reply