#### 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. Any output is stored in the output variable and the exit status in the status variable. If called with no outputs and the compilation fails then Octave will emit an error. If the programmer requests output or status, however, Octave will merely issue a warning and it is the programmer’s responsibility to verify the command was successful.

  `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 configuration variable VAR. There are three categories of variables:

    Octave configuration variables that users may override with environment variables. These are used in commands that `mkoctfile` executes.

           ALL_CFLAGS                  INCLUDEDIR
           ALL_CXXFLAGS                LAPACK_LIBS
           ALL_FFLAGS                  LDFLAGS
           ALL_LDFLAGS                 LD_STATIC_FLAG
           BLAS_LIBS                   LIBDIR
           CC                          LIBOCTAVE
           CFLAGS                      LIBOCTINTERP
           CPICFLAG                    LIBOCTMEX
           CPPFLAGS                    OCTAVE_LINK_OPTS
           CXX                         OCTINCLUDEDIR
           CXXFLAGS                    OCTAVE_LIBS
           CXXLD                       OCTAVE_LINK_DEPS
           CXXPICFLAG                  OCTLIBDIR
           DL_LDFLAGS                  OCT_LINK_DEPS
           F77                         OCT_LINK_OPTS
           F77_INTEGER8_FLAG           RDYNAMIC_FLAG
           FFLAGS                      SPECIAL_MATH_LIB
           FPICFLAG                    XTRA_CFLAGS
           INCFLAGS                    XTRA_CXXFLAGS

    Octave configuration variables as above, but currently unused by `mkoctfile`.

           AR
           DEPEND_EXTRA_SED_PATTERN
           DEPEND_FLAGS
           FFTW3F_LDFLAGS
           FFTW3F_LIBS
           FFTW3_LDFLAGS
           FFTW3_LIBS
           FFTW_LIBS
           FLIBS
           LIBS
           RANLIB
           READLINE_LIBS

    Octave configuration variables that are provided for informational purposes only. Except for ‘`OCTAVE_HOME`’ and ‘`OCTAVE_EXEC_HOME`’, users may not override these variables.

    If `OCTAVE_HOME` or `OCTAVE_EXEC_HOME` are set in the environment, then other variables are adjusted accordingly with `OCTAVE_HOME` or `OCTAVE_EXEC_HOME` substituted for the original value of the directory specified by the `--prefix` or `--exec-prefix` options that were used when Octave was configured.

           API_VERSION                 LOCALARCHLIBDIR
           ARCHLIBDIR                  LOCALFCNFILEDIR
           BINDIR                      LOCALOCTFILEDIR
           CANONICAL_HOST_TYPE         LOCALSTARTUPFILEDIR
           DATADIR                     LOCALVERARCHLIBDIR
           DATAROOTDIR                 LOCALVERFCNFILEDIR
           DEFAULT_PAGER               LOCALVEROCTFILEDIR
           EXEEXT                      MAN1DIR
           FCNFILEDIR                  MAN1EXT
           IMAGEDIR                    MANDIR
           INCLUDEDIR                  OCTAVE_EXEC_HOME
           INFODIR                     OCTAVE_HOME
           INFOFILE                    OCTDATADIR
           LIBDIR                      OCTDOCDIR
           LIBEXECDIR                  OCTFILEDIR
           LOCALAPIARCHLIBDIR          OCTFONTSDIR
           LOCALAPIFCNFILEDIR          OCTINCLUDEDIR
           LOCALAPIOCTFILEDIR          OCTLIBDIR
           LOCALAPIPKGDIR              STARTUPFILEDIR
                                       VERSION

  ‘`--link-stand-alone`’

    Link a stand-alone executable file.

  ‘`--mex`’

    Assume creation of a MEX file. Set the default output extension to `.mex`. Link to liboctmex instead of liboctinterp and liboctave.

  ‘`-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
           .cp   C++ source
           .cpp  C++ source
           .CPP  C++ source
           .cxx  C++ source
           .c++  C++ source
           .C    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` (see 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.