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

Mixed languages

Giel_H_
Beginner
677 Views
Steve Lionel has modified the C_CALL_Fortran mixed language samplefrom CVF into an IVF sample.
The sample uses a FDLL project which builds the Fortran source of subroutine FSUBas a DLL. FSUB has an integer and a character string input argument and an character string output argument. FSUB concatenates the input arguments tooneoutput string.
A c/c++ main programUSEDLL calls the dll version of FSUB.
As an extension to this sample I am interested in ac/c++DLL project (say CDLL) in whichthe cdllsource codecontains (an entry of)a character function that calls the FSUB dll and returns thec string constructed by FSUB.
Is that possible?
Giel
0 Kudos
5 Replies
Steven_L_Intel1
Employee
677 Views
Most anything is possible, but I don't fully understand your specification. How would you declare this C function in C? (Pretend Fortran isn't part of this.) What does it mean to be a "character function" in C?
0 Kudos
Giel_H_
Beginner
677 Views
I admit that the use of "function" and "subroutine" is indeed confusing.
Iwould suggest anything like:
LPTSTR CFUN
LPSTR CFUN
char *CFUN
And for the definition of the function CFUN:
LPTSTR CFUN(int *int_arg,LPTSTR *str_in)
LPSTR CFUN(int *int_arg,LPSTR *str_in)
char *CFUN(int *int_arg, char *str_in)
The body of the latter CFUN wouldbe something like
{
char *str_out;
int STR_OUT_LEN;
STR_OUT_LEN= 512;
FSUB(&int_arg,str_in, str_out, strlen(str_in),STR_OUT_LEN);
return str_out;
}
Giel
0 Kudos
Jugoslav_Dujic
Valued Contributor II
677 Views
Uh-oh. You see, the problem with Fortran strings is that they-re two-fold: the passed information must always contain both the address and the length. Now, since both cannot be returned through registers, Fortran compiler "internally" turns the character-valued function into:
FUNCTION StrRet()
CHARACTER(10):: StrRet
StrRet="1234567890"
END FUNCTION StrRet

extern "C" char* strret(char*, int)
Well, maybe, I didn't care to do complete reverse-engineering. There are similar issues with functions returning TYPEs, arrays etc. So, it's better to avoid functions returning anything other than plain vanilla ints or floats in mixed-language programming.
0 Kudos
Giel_H_
Beginner
677 Views

OK. Exactly, therefore I would like to usethe CFUN.

Giel

0 Kudos
Steven_L_Intel1
Employee
677 Views
In C, your CFUN returns a pointer to a string, one that is presumably allocated within the function. You can do the same in Fortran. The equivalent Fortran function would be something like this:


function CFUN()
integer(int_ptr_kind()) :: CFUN
character(10) :: retstr = 'abcdefghij'
integer(1), dimension(*) :: temp
pointer (p, temp)

p = malloc (len(retstr)+1)
do i = 1,len(retstr)
temp(i) = ichar(retstr(i:i)
end do
temp(ubound(temp)) = 0
CFUN = P
return
end

It's then the caller's responsibility to deallocate the result.
0 Kudos
Reply