-
Fortran function in C++
Hi there,
I have been given a fortran90 .dll, of which I want to call a function. Now I know some things:
- one should pass by pointer (or this is simply the signature, I forgot)
- one should declare the function "extern"
- one should add an '_' to the function name
Now everything's ok, except for the characters. Apparently the fortran standard does not prescribe a pass-by-pointer for character arrays. Several variations exist, which make use of the fact that the size of a fortran string is hidden in the string itself.
Could anyone share his/her experience on this? I'm thinking ofmy own string class with a conversion constructor.
Thanks a lot!!
-
As you say, the Fortran standards have a different notion of strings than does C/C++: when a Fortran string is passed to a function, the length of the string is also passed (and there is no null termination either, as in a C-style string).
The mechanism of passing a string as a function or subroutine argument is compiler dependent: some Fortran compilers implicitly pass a second argument with the length. Other compilers represent a Fortran string using (effectively) a data structure that is passed by reference.
The rules of pass by pointer to an extern function, and adorning the function name with underscores is specific to your compiler. Other compilers can (and do) behave differently. In general you need to read the documentation for both your C/C++ compiler and the Fortran compiler to
a) determine if the two compilers are even compatible (i.e. code from one can call a function from the other).
b) determine what the mapping is for different types between compilers (adornment of function names, argument passing mechanisms, techniques for handling Fortran common blocks, etc etc).