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

Create successful ePaper yourself

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

Notes:<br />

1. In interlanguage communication, it is often necessary to use the %VAL and<br />

%REF built-in functions that are defined in “Passing Arguments By Reference<br />

or By Value” on page 353.<br />

2. C programs automatically convert float values to double and short integer<br />

values to integer when calling an unprototyped C function. Because <strong>XL</strong> <strong>Fortran</strong><br />

does not per<strong>for</strong>m a conversion on REAL(4) quantities passed by value, you<br />

should not pass REAL(4) and INTEGER(2) values as arguments to C functions<br />

that you have not declared with function prototypes.<br />

3. The <strong>Fortran</strong>-derived type, the Pascal RECORD, and the C structure must match<br />

in the number, data type, and length of subobjects to be compatible data types.<br />

Related In<strong>for</strong>mation: One or more sample programs under the directory<br />

/usr/lpp/xlf/samples illustrate how to call from <strong>Fortran</strong> to C.<br />

To use the <strong>Fortran</strong> 2003 Draft Standard interoperability<br />

features provided by <strong>XL</strong> <strong>Fortran</strong>, see the Language<br />

Interoperability Features section in the <strong>XL</strong> <strong>Fortran</strong> <strong>Enterprise</strong><br />

<strong>Edition</strong> <strong>for</strong> <strong>AIX</strong> Language Reference.<br />

Passing Global Variables between Languages<br />

To access a C data structure from within a <strong>Fortran</strong> program or to access a common<br />

block from within a C program, follow these steps:<br />

1. Create a named common block that provides a one-to-one mapping of the C<br />

structure members. If you have an unnamed common block, change it to a<br />

named one. Name the common block with the name of the C structure.<br />

2. Declare the C structure as a global variable by putting its declaration outside<br />

any function or inside a function with the extern qualifier.<br />

3. Compile the C source file with -qalign=packed.<br />

program cstruct struct mystuff {<br />

real(8) a,d double a;<br />

integer b,c<br />

int b,c;<br />

. double d;<br />

. };<br />

common /mystuff/ a,b,c,d<br />

. main() {<br />

.<br />

end }<br />

If you do not have a specific need <strong>for</strong> a named common block, you can create a<br />

sequence-derived type with the same one-to-one mapping as a C structure and<br />

pass it as an argument to a C function. You must compile the C source file with<br />

-qalign=packed.<br />

Common blocks that are declared THREADLOCAL are thread-specific data areas<br />

that are dynamically allocated by compiler-generated code. A static block is still<br />

reserved <strong>for</strong> a THREADLOCAL common block, but the compiler and the<br />

compiler’s run-time environment use it <strong>for</strong> control in<strong>for</strong>mation. If you need to<br />

share THREADLOCAL common blocks between <strong>Fortran</strong> and C procedures, your<br />

C source must be aware of the implementation of the THREADLOCAL common<br />

block. For more in<strong>for</strong>mation, see THREADLOCAL common blocks, the Directives<br />

section in the <strong>XL</strong> <strong>Fortran</strong> <strong>Enterprise</strong> <strong>Edition</strong> <strong>for</strong> <strong>AIX</strong> Language Reference, and<br />

Appendix A, “Sample <strong>Fortran</strong> Programs,” on page 405.<br />

350 <strong>XL</strong> <strong>Fortran</strong> <strong>Enterprise</strong> <strong>Edition</strong> <strong>for</strong> <strong>AIX</strong> : User’s <strong>Guide</strong>

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

Saved successfully!

Ooh no, something went wrong!