12.07.2015 Views

PGI User's Guide

PGI User's Guide

PGI User's Guide

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Inter-language Calling ConsiderationsInter-language Calling Considerations152In general, when argument data types and function return values agree, you can call a C or C++ function fromFortran as well as call a Fortran function from C or C++. When data types for arguments do not agree, you mayneed to develop custom mechanisms to handle them. For example, the Fortran COMPLEX type has a matchingtype in C99 but does not have a matching type in C90; however, it is still possible to provide inter-language callsbut there are no general calling conventions for such cases.Note• If a C++ function contains objects with constructors and destructors, calling such a function fromeither C or Fortran is not possible unless the initialization in the main program is performed from aC++ program in which constructors and destructors are properly initialized.• In general, you can call a C or Fortran function from C++ without problems as long as you use theextern "C" keyword to declare the function in the C++ program. This declaration prevents namemangling for the C function name. If you want to call a C++ function from C or Fortran, you alsohave to use the extern "C" keyword to declare the C++ function. This keeps the C++ compilerfrom mangling the name of the function.• You can use the __cplusplus macro to allow a program or header file to work for both C and C++.For example, the following defines in the header file stdio.h allow this file to work for both C and C++.#ifndef _STDIO_H#define _STDIO_H#ifdef __cplusplusextern "C" {#endif /* __cplusplus */.. /* Functions and data types defined... */.#ifdef __cplusplus}#endif /* __cplusplus */#endif• C++ member functions cannot be declared extern, since their names will always be mangled.Therefore, C++ member functions cannot be called from C or Fortran.Functions and SubroutinesFortran, C, and C++ define functions and subroutines differently.For a Fortran program calling a C or C++ function, observe the following return value convention:• When a C or C++ function returns a value, call it from Fortran as a function.• When a C or C++ function does not return a value, call it as a subroutine.For a C/C++ program calling a Fortran function, the call should return a similar type. Table 12.1, “Fortranand C/C++ Data Type Compatibility” lists compatible types. If the call is to a Fortran subroutine, a FortranCHARACTER function, or a Fortran COMPLEX function, call it from C/C++ as a function that returns void. The

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!