(octave.info)Getting Started with Oct-Files


Next: Matrices and Arrays in Oct-Files Up: Oct-Files
Enter node , (file) or (file)node

A.1.1 Getting Started with Oct-Files
------------------------------------

Oct-files are pieces of C++ code that have been compiled with the Octave
API into a dynamically loadable object.  They take their name from the
file which contains the object which has the extension ‘.oct’.

   Finding a C++ compiler, using the correct switches, adding the right
include paths for header files, etc. is a difficult task.  Octave
automates this by providing the ‘mkoctfile’ command with which to build
oct-files.  The command is available from within Octave or at the shell
command line.

 -- : mkoctfile [-options] file ...
 -- : [OUTPUT, STATUS] = mkoctfile (...)

     The ‘mkoctfile’ function compiles source code written in C, C++, or
     Fortran.  Depending on the options used with ‘mkoctfile’, the
     compiled code can be called within Octave or can be used as a
     stand-alone application.

     ‘mkoctfile’ can be called from the shell prompt or from the Octave
     prompt.  Calling it from the Octave prompt simply delegates the
     call to the shell prompt.  The output is stored in the OUTPUT
     variable and the exit status in the STATUS variable.

     ‘mkoctfile’ accepts the following options, all of which are
     optional except for the filename of the code you wish to compile:

     ‘-I DIR’
          Add the include directory DIR to compile commands.

     ‘-D DEF’
          Add the definition DEF to the compiler call.

     ‘-l LIB’
          Add the library LIB to the link command.

     ‘-L DIR’
          Add the library directory DIR to the link command.

     ‘-M’
     ‘--depend’
          Generate dependency files (.d) for C and C++ source files.

     ‘-R DIR’
          Add the run-time path to the link command.

     ‘-Wl,...’
          Pass options to the linker like "-Wl,-rpath=...".  The quotes
          are needed since commas are interpreted as command separators.

     ‘-W...’
          Pass options to the assembler like "-Wa,OPTION".

     ‘-c’
          Compile but do not link.

     ‘-g’
          Enable debugging options for compilers.

     ‘-o FILE’
     ‘--output FILE’
          Output filename.  Default extension is .oct (or .mex if
          ‘--mex’ is specified) unless linking a stand-alone executable.

     ‘-p VAR’
     ‘--print VAR’
          Print the configuration variable VAR.  Recognized variables
          are:

                  ALL_CFLAGS                  INCFLAGS
                  ALL_CXXFLAGS                INCLUDEDIR
                  ALL_FFLAGS                  LAPACK_LIBS
                  ALL_LDFLAGS                 LD_CXX
                  AR                          LDFLAGS
                  BLAS_LIBS                   LD_STATIC_FLAG
                  CC                          LFLAGS
                  CFLAGS                      LIBDIR
                  CPICFLAG                    LIBOCTAVE
                  CPPFLAGS                    LIBOCTINTERP
                  CXX                         LIBS
                  CXXFLAGS                    OCTAVE_HOME
                  CXXPICFLAG                  OCTAVE_LIBS
                  DEPEND_EXTRA_SED_PATTERN    OCTAVE_LINK_DEPS
                  DEPEND_FLAGS                OCTAVE_LINK_OPTS
                  DL_LD                       OCTAVE_PREFIX
                  DL_LDFLAGS                  OCTINCLUDEDIR
                  F77                         OCTLIBDIR
                  F77_INTEGER8_FLAG           OCT_LINK_DEPS
                  FFLAGS                      OCT_LINK_OPTS
                  FFTW3F_LDFLAGS              RANLIB
                  FFTW3F_LIBS                 RDYNAMIC_FLAG
                  FFTW3_LDFLAGS               READLINE_LIBS
                  FFTW3_LIBS                  SED
                  FFTW_LIBS                   SPECIAL_MATH_LIB
                  FLIBS                       XTRA_CFLAGS
                  FPICFLAG                    XTRA_CXXFLAGS

     ‘--link-stand-alone’
          Link a stand-alone executable file.

     ‘--mex’
          Assume we are creating a MEX file.  Set the default output
          extension to ".mex".

     ‘-s’
     ‘--strip’
          Strip the output file.

     ‘-v’
     ‘--verbose’
          Echo commands as they are executed.

     ‘file’
          The file to compile or link.  Recognized file types are

                  .c    C source
                  .cc   C++ source
                  .C    C++ source
                  .cpp  C++ source
                  .f    Fortran source (fixed form)
                  .F    Fortran source (fixed form)
                  .f90  Fortran source (free form)
                  .F90  Fortran source (free form)
                  .o    object file
                  .a    library file

   Consider the following short example which introduces the basics of
writing a C++ function that can be linked to Octave.

     #include <octave/oct.h>
     
     DEFUN_DLD (helloworld, args, nargout,
                "Hello World Help String")
     {
       octave_stdout << "Hello World has "
                     << args.length () << " input arguments and "
                     << nargout << " output arguments.\n";
     
       // Return empty matrices for any outputs
       octave_value_list retval (nargout);
       for (int i = 0; i < nargout; i++)
         retval(i) = octave_value (Matrix ());
     
       return retval;
     }

   The first critical line is ‘#include <octave/oct.h>’ which makes
available most of the definitions necessary for a C++ oct-file.  Note
that ‘octave/oct.h’ is a C++ header and cannot be directly ‘#include’’ed
in a C source file, nor any other language.

   Included by ‘oct.h’ is a definition for the macro ‘DEFUN_DLD’ which
creates a dynamically loaded function.  This macro takes four arguments:

  1. The function name as it will be seen in Octave,

  2. The list of arguments to the function of type ‘octave_value_list’,

  3. The number of output arguments, which can be—and often is—omitted
     if not used, and

  4. The string to use for the help text of the function.

   The return type of functions defined with ‘DEFUN_DLD’ is always
‘octave_value_list’.

   There are a couple of important considerations in the choice of
function name.  First, it must be a valid Octave function name and so
must be a sequence of letters, digits, and underscores not starting with
a digit.  Second, as Octave uses the function name to define the
filename it attempts to find the function in, the function name in the
‘DEFUN_DLD’ macro must match the filename of the oct-file.  Therefore,
the above function should be in a file ‘helloworld.cc’, and would be
compiled to an oct-file using the command

     mkoctfile helloworld.cc

   This will create a file called ‘helloworld.oct’ that is the compiled
version of the function.  It should be noted that it is perfectly
acceptable to have more than one ‘DEFUN_DLD’ function in a source file.
However, there must either be a symbolic link to the oct-file for each
of the functions defined in the source code with the ‘DEFUN_DLD’ macro
or the ‘autoload’ (Note: Function Files) function should be used.

   The rest of the function shows how to find the number of input
arguments, how to print through the Octave pager, and how to return from
the function.  After compiling this function as above, an example of its
use is

     helloworld (1, 2, 3)
     ⊣ Hello World has 3 input arguments and 0 output arguments.

   Subsequent sections show how to use specific classes from Octave’s
core internals.  Base classes like ‘dMatrix’ (a matrix of double values)
are found in the directory ‘liboctave/array’.  The definitive reference
for how to use a particular class is the header file itself.  However,
it is often enough simply to study the examples in the manual in order
to be able to use a class.


automatically generated by info2www version 1.2.2.9