24.05.2014 Views

XL Fortran Enterprise Edition for AIX : User's Guide - IBM

XL Fortran Enterprise Edition for AIX : User's Guide - IBM

XL Fortran Enterprise Edition for AIX : User's Guide - IBM

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Mixing <strong>Fortran</strong> and C++<br />

Most of the in<strong>for</strong>mation in this section applies to <strong>Fortran</strong>, C, and Pascal —<br />

languages with similar data types and naming schemes. However, to mix <strong>Fortran</strong><br />

and C++ in the same program, you must add an extra level of indirection and pass<br />

the interlanguage calls through C wrapper functions.<br />

Because the C++ compiler mangles the names of some C++ objects, you must use<br />

the xlC command to link the final program and include -L and -l options <strong>for</strong> the<br />

<strong>XL</strong> <strong>Fortran</strong> library directories and libraries as shown in “Linking 32–Bit Non-SMP<br />

Object Files Using the ld Command” on page 44.<br />

program main<br />

integer idim,idim1<br />

idim = 35<br />

idim1= 45<br />

write(6,*) ’Inside <strong>Fortran</strong> calling first C function’<br />

call cfun(idim)<br />

write(6,*) ’Inside <strong>Fortran</strong> calling second C function’<br />

call cfun1(idim1)<br />

write(6,*) ’Exiting the <strong>Fortran</strong> program’<br />

end<br />

Figure 1. Main <strong>Fortran</strong> Program That Calls C++ (main1.f)<br />

#include <br />

#include "cplus.h"<br />

extern "C" void cfun(int *idim);<br />

extern "C" void cfun1(int *idim1);<br />

void cfun(int *idim){<br />

printf("%%%Inside C function be<strong>for</strong>e creating C++ Object\n");<br />

int i = *idim;<br />

junk* jj= new junk(10,30);<br />

jj->store(idim);<br />

jj->print();<br />

printf("%%%Inside C function after creating C++ Object\n");<br />

delete jj;<br />

return;<br />

}<br />

void cfun1(int *idim1) {<br />

printf("%%%Inside C function cfun1 be<strong>for</strong>e creating C++ Object\n");<br />

int i = *idim1;<br />

temp *tmp = new temp(40, 50.54);<br />

tmp->print();<br />

printf("%%%Inside C function after creating C++ temp object\n");<br />

delete tmp;<br />

return;<br />

}<br />

Figure 2. C Wrapper Functions <strong>for</strong> Calling C++ (cfun.C)<br />

Interlanguage Calls 347

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

Saved successfully!

Ooh no, something went wrong!