Fortran SFF API to data I/O streams in C++
Interlanguage programming

In the case of this library Fortran programs will call C++ functions. A simple example is given here.

The code

The C++ header file cxxfunc.h contains:

#include <string>
namespace fapidxx {
int cxxfunction(const std::string& s);
} // namespace fapidxx

The corresponding definition of the function is provided in cxxfunc.cc:

#include <cxxfunc.h>
namespace fapidxx {
int cxxfunction(const std::string& s)
{
int retval=s.size();
return(retval);
} // int cxxfunction(const std::string& s)
} // namespace fapidxx

The Fortran interface function is declared in cxxiface.h:

extern "C" int interfacesub_(float *value);

The body of the function is defined in cxxiface.cc:

#include <cxxfunc.h>
#include <cxxiface.h>
int interfacesub_(float *value)
{
int retval=0;
std::string chars="hi there 12345678";
*value=int(fapidxx::cxxfunction(chars));
return(retval);
} // int interfacesub_(float *value)

In the Fortran main program fprog.f this function is called:

program fapidtest
call interfacesub(a)
print *,a
stop
end

Compiling and linking the code

Compiling the library:

P> g++ -c -o cxxfunc.o cxxfunc.cc
P> g++ -c -o cxxiface.o cxxiface.cc
P> ar rc libiface.a cxxfunc.o cxxiface.o
P> gfortran -ff2c -Wall -c -o fprog.o fprog.f
P> gfortran -o fprog fprog.o -liface ­lstdc++

Calling fprog will produce the output:

P> fprog
   17.000000