How to run a Fortran program within GNU Octave?

后端 未结 1 375
说谎
说谎 2021-01-24 08:14

I want to run a Fortran program within Octave. I would like to do this for automation purposes and use Octave for all the data processing.

Is it possible to run a Fortran

相关标签:
1条回答
  • 2021-01-24 08:35

    Obviously your particular use-case may be a lot more complex than this, but here's a simple example to get you started (or to help you decide whether it's worth going down that route at all...)

    Let's start with a simple octfile which performs simple integer addition, no fortran involved for now.

    // in: simple_addition.cpp 
    #include <octave/oct.h>
    
    DEFUN_DLD (simple_addition, args, ,"Add two integers via C++")
    {
      octave_value retval = args(0).int_value() + args(1).int_value();
      return retval;
    }
    

    Compile:

    mkoctfile -c simple_addition.cpp                 # compiles a simple_addition.o file
    mkoctfile -o simple_addition simple_addition.o   # links .o file to named output file
    

    In octave:

    octave:1> simple_addition(1,2)
    ans =  3
    

    Now let's put this aside for a minute, and see how we might call a fortran-defined function from pure c++. First let's create a simple integer addition function:

     ! in fortran_addition.f90 
     function fortran_addition(a,b) result(Out)
        integer, intent(in) :: a,b ! input
        integer             :: Out ! output
        Out = a + b
     end function fortran_addition
    

    and compile it using gfortran:

    gfortran -c fortran_addition.f90    # creates fortran_addition.o
    

    You can see (e.g. using nm fortran_addition.o) that the generated object contains a reference to a symbol under the name fortran_addition_ (note the added underscore at the end).

    Now let's create a normal (i.e. non-octave-related) c++ wrapper program which calls the function defined via this symbol:

    // in generic_fortran_addition_wrapper.cpp
    #include <iostream>
    extern "C" { int fortran_addition_( int *, int * ); }
    int main() {
        int a = 1, b = 2, fortran_result;
        fortran_result = fortran_addition_( &a, &b );
        std::cout << a << " + " << b << " = " << fortran_result << std::endl;
    }
    

    compile:

    g++ -c generic_fortran_addition_wrapper.cpp
    g++ -o addints generic_fortran_addition_wrapper.o fortran_addition.o
    ./addints   # outputs `1 + 2 = 3` on the terminal
    

    Now we have all the ingredients to create an octfile that wraps a fortran function:

    // in fortran_addition_wrapper.cpp
    #include <octave/oct.h>
    extern "C" { int fortran_addition_( int *, int *); }
    DEFUN_DLD (fortran_addition_wrapper, args, ,"Add two integers via fortran")
    {
      int a, b, fortran_result;
      a = args(0).int_value();
      b = args(1).int_value();
      fortran_result = fortran_addition_( &a, &b );
      octave_value retval(fortran_result);
      return retval;
    }
    

    compile with mkoctfile:

    mkoctfile -c fortran_addition_wrapper.cpp
    mkoctfile -o fortran_addition_wrapper fortran_addition_wrapper.o fortran_addition.o
    

    and then in octave:

    octave:1> fortran_addition_wrapper(1,2)
    ans =  3
    

    Having said all this, obviously if you have a fully defined fortran program, rather than just linkable functions, and you have a running compiled executable on your system, then you can skip all the above 'formalities' and just call your executable via the system() command from octave. Obviously in this scenario it's up to you to pass the data in an octave-agnostic way ... but presumably if you have a standalone fortran executable, then presumably it already has a way of reading input data from the operating system.

    EDIT as per the comments below, I've been reminded that I got side-tracked and answered the question that was asked in the comments to the original question, and forgot to address the error messages in the original question. As mentioned in my comment there, mkoctave is a generic wrapper to the gnu compiler collection. Those messages do not sound specific to octave, but rather that the compiler/linker complains that you're missing the fortran runtime libraries that define these basic functions.

    0 讨论(0)
提交回复
热议问题