Operating System - OpenVMS
cancel
Showing results for 
Search instead for 
Did you mean: 

How to call Fortran routine from C++ code ?

 
Abdelali
Occasional Contributor

How to call Fortran routine from C++ code ?

I'm trying it but I got "%ILINK-I-UDFSYM" when linking !! Any special option I need to add to the link to work ?
NB: I can call the same routine from C code without any issue. Also tried to have C function as an intermediate call (between C++ and Fortran) but I'm having the same Link issue.
5 REPLIES 5
Steven Schweda
Honored Contributor

Re: How to call Fortran routine from C++ code ?

Do I understand this correctly? You're
hiding all the code, you're hiding the
compile and link commands, and you're hiding
almost all of the error message(s), and you
expect a useful answer?

> NB: I can call the same routine from C [...]

Ever look at the system header files and see
things like this?:

[...]
#ifdef __cplusplus
extern "C" {
#endif
[...]
#ifdef __cplusplus
}
#endif
[...]

I'm not a C++ expert, but I believe that
those things are in there for a reason. (And
it's related to those funny names.)
Robert Gezelter
Honored Contributor

Re: How to call Fortran routine from C++ code ?

Abdelali,

The complex external name is caused by "name mangling". Somewhat simplified, name mangling encodes the parameter list type information and the external name.

I do not have the time to check Steve's syntax, but the approach is correct, having constructed cross-language calls to/fro C++ many times.

- Bob Gezelter, http://www.rlgsc.com
Abdelali
Occasional Contributor

Re: How to call Fortran routine from C++ code ?

Thank you Steven & Bob ... Adding the define __cplusplus solved my problem.
Dennis Handly
Acclaimed Contributor

Re: How to call Fortran routine from C++ code ?

>Adding the define __cplusplus solved my problem.

The define should automatically be added by the compiler. And if your header is always used by C++, you don't need that #ifdef, just create an extern "C" block.

Unfortunately there is no: extern "fortran" :-)

>Robert: I do not have the time to check Steve's syntax

The syntax is correct for a file that is shared between C and C++.
Steven Schweda
Honored Contributor

Re: How to call Fortran routine from C++ code ?

> Adding the define __cplusplus solved my
> problem.

I assume that adding the
extern "C" {[...]}
part is what made the difference.

> The define should automatically be added by
> the compiler. [...]

You seem to assume that the solution report
was somehow connected with reality. Ha!

> The syntax is correct [...]

I expect correctness when I steal from

(SYS$COMMON:[DECC$LIB.REFERENCE.DECC$RTLDEF]STDIO.H).