Most of the information in this chapter applies to Fortran, C, and Pascal -- languages with similar data types and naming schemes. However, to mix Fortran and C++ in the same program, you must add an extra level of indirection and pass the interlanguage calls through C "wrapper" functions.
Because the C++ compiler "mangles" the names of some C++ objects, you must use the xlC command to link the final program and include -L and -l options for the XL Fortran library directories and libraries as shown in Linking 32-Bit Non-SMP Object Files Using the ld Command.
Figure 1. Main Fortran Program That Calls C++ (main1.f)
program main
integer idim,idim1
idim = 35
idim1= 45
write(6,*) 'Inside Fortran calling first C function'
call cfun(idim)
write(6,*) 'Inside Fortran calling second C function'
call cfun1(idim1)
write(6,*) 'Exiting the Fortran program'
end
Figure 2. C Wrapper Functions for Calling C++ (cfun.C)
#include <stdio.h>
#include "cplus.h"
extern "C" void cfun(int *idim);
extern "C" void cfun1(int *idim1);
void cfun(int *idim){
printf("%%%Inside C function before creating C++ Object\n");
int i = *idim;
junk<int>* jj= new junk<int>(10,30);
jj->store(idim);
jj->print();
printf("%%%Inside C function after creating C++ Object\n");
delete jj;
return;
}
void cfun1(int *idim1) {
printf("%%%Inside C function cfun1 before creating C++ Object\n");
int i = *idim1;
temp<double> *tmp = new temp<double>(40, 50.54);
tmp->print();
printf("%%%Inside C function after creating C++ temp object\n");
delete tmp;
return;
}
Figure 3. C++ Code Called from Fortran (cplus.h)
#include <iostream.h>
template<class T> class junk {
private:
int inter;
T templ_mem;
T stor_val;
public:
junk(int i,T j): inter(i),templ_mem(j)
{cout <<"***Inside C++ constructor" << endl;}
~junk() {cout <<"***Inside C++ Destructor" << endl;}
void store(T *val){ stor_val = *val;}
void print(void) {cout << inter << "\t" << templ_mem ;
cout <<"\t" << stor_val << endl; }};
template<class T> class temp {
private:
int internal;
T temp_var;
public:
temp(int i, T j): internal(i),temp_var(j)
{cout <<"***Inside C++ temp Constructor" <<endl;}
~temp() {cout <<"***Inside C++ temp destructor" <<endl;}
void print(void) {cout << internal << "\t" << temp_var << endl;}};
Compiling this program, linking it with the xlC command, and running it produces this output:
Inside Fortran calling first C function %Inside C function before creating C++ Object ***Inside C++ constructor 10 30 35 %Inside C function after creating C++ Object ***Inside C++ Destructor Inside Fortran calling second C function %Inside C function cfun1 before creating C++ Object ***Inside C++ temp Constructor 40 50.54 %Inside C function after creating C++ temp object ***Inside C++ temp destructor Exiting the Fortran program