Fortran: Difference between revisions

603 bytes added ,  29 November 2012
Example of writing F2003 Code linking to Octave
No edit summary
(Example of writing F2003 Code linking to Octave)
Line 1: Line 1:
This page describes an example of how to call liboctave functions from a Fortran program.
In the example we will load an Octave array from a file in Octave's native ASCII format,
it consists of two steps:
- write a C++ function with a C compatible interface and C linkage that reads a variable from an Octave ASCII file
- write Fortran code using the "iso_c_binding" intrinsic module to call the C++ function


=== C++ function ===


{{Code|octave_file_io.cc: C++ function to load a matrix from an ASCII file in Octave native format|<syntaxhighlight lang="C" style="font-size:13px">
{{Code|octave_file_io.cc: C++ function to load a matrix from an ASCII file in Octave native format|<syntaxhighlight lang="C" style="font-size:13px">
Line 48: Line 55:
</syntaxhighlight>}}
</syntaxhighlight>}}


=== Header file ===


{{Code|octave_file_io.h: header file with C interface to octave_file_io.cc|<syntaxhighlight lang="C" style="font-size:13px">
{{Code|octave_file_io.h: header file with C interface to octave_file_io.cc|<syntaxhighlight lang="C" style="font-size:13px">
Line 67: Line 76:




Compile the function
=== Fortran Code ===
 
  mkoctfile -I. octave_file_io.cc


{{Code|octave_file_io.cc: C++ function to load a matrix from an ASCII file in Octave native format|<syntaxhighlight lang="fortran" style="font-size:13px">
{{Code|octave_file_io.cc: C++ function to load a matrix from an ASCII file in Octave native format|<syntaxhighlight lang="fortran" style="font-size:13px">
Line 108: Line 115:
end program octave_file_io_example
end program octave_file_io_example
</syntaxhighlight>}}
</syntaxhighlight>}}
=== Compiling the code ===
  mkoctfile -I. octave_file_io.cc
  mkoctfile -I. --mkoctfile --link-stand-alone octave_file_io_example.f90 octave_file_io.o -o octave_file_io_example
349

edits