<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <!-- Created by GNU Texinfo 6.5, http://www.gnu.org/software/texinfo/ --> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Getting Started with Oct-Files (GNU Octave (version 5.1.0))</title> <meta name="description" content="Getting Started with Oct-Files (GNU Octave (version 5.1.0))"> <meta name="keywords" content="Getting Started with Oct-Files (GNU Octave (version 5.1.0))"> <meta name="resource-type" content="document"> <meta name="distribution" content="global"> <meta name="Generator" content="makeinfo"> <link href="index.html#Top" rel="start" title="Top"> <link href="Concept-Index.html#Concept-Index" rel="index" title="Concept Index"> <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents"> <link href="Oct_002dFiles.html#Oct_002dFiles" rel="up" title="Oct-Files"> <link href="Matrices-and-Arrays-in-Oct_002dFiles.html#Matrices-and-Arrays-in-Oct_002dFiles" rel="next" title="Matrices and Arrays in Oct-Files"> <link href="Oct_002dFiles.html#Oct_002dFiles" rel="prev" title="Oct-Files"> <style type="text/css"> <!-- a.summary-letter {text-decoration: none} blockquote.indentedblock {margin-right: 0em} blockquote.smallindentedblock {margin-right: 0em; font-size: smaller} blockquote.smallquotation {font-size: smaller} div.display {margin-left: 3.2em} div.example {margin-left: 3.2em} div.lisp {margin-left: 3.2em} div.smalldisplay {margin-left: 3.2em} div.smallexample {margin-left: 3.2em} div.smalllisp {margin-left: 3.2em} kbd {font-style: oblique} pre.display {font-family: inherit} pre.format {font-family: inherit} pre.menu-comment {font-family: serif} pre.menu-preformatted {font-family: serif} pre.smalldisplay {font-family: inherit; font-size: smaller} pre.smallexample {font-size: smaller} pre.smallformat {font-family: inherit; font-size: smaller} pre.smalllisp {font-size: smaller} span.nolinebreak {white-space: nowrap} span.roman {font-family: initial; font-weight: normal} span.sansserif {font-family: sans-serif; font-weight: normal} ul.no-bullet {list-style: none} --> </style> <link rel="stylesheet" type="text/css" href="octave.css"> </head> <body lang="en"> <a name="Getting-Started-with-Oct_002dFiles"></a> <div class="header"> <p> Next: <a href="Matrices-and-Arrays-in-Oct_002dFiles.html#Matrices-and-Arrays-in-Oct_002dFiles" accesskey="n" rel="next">Matrices and Arrays in Oct-Files</a>, Up: <a href="Oct_002dFiles.html#Oct_002dFiles" accesskey="u" rel="up">Oct-Files</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p> </div> <hr> <a name="Getting-Started-with-Oct_002dFiles-1"></a> <h4 class="subsection">A.1.1 Getting Started with Oct-Files</h4> <p>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 <samp>.oct</samp>. </p> <p>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 <code>mkoctfile</code> command with which to build oct-files. The command is available from within Octave or at the shell command line. </p> <a name="XREFmkoctfile"></a><dl> <dt><a name="index-mkoctfile-1"></a><em></em> <strong>mkoctfile</strong> <em>[-options] file …</em></dt> <dt><a name="index-mkoctfile-2"></a><em>[<var>output</var>, <var>status</var>] =</em> <strong>mkoctfile</strong> <em>(…)</em></dt> <dd> <p>The <code>mkoctfile</code> function compiles source code written in C, C++, or Fortran. Depending on the options used with <code>mkoctfile</code>, the compiled code can be called within Octave or can be used as a stand-alone application. </p> <p><code>mkoctfile</code> 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 <var>output</var> variable and the exit status in the <var>status</var> variable. </p> <p><code>mkoctfile</code> accepts the following options, all of which are optional except for the filename of the code you wish to compile: </p> <dl compact="compact"> <dt>‘<samp>-I DIR</samp>’</dt> <dd><p>Add the include directory DIR to compile commands. </p> </dd> <dt>‘<samp>-D DEF</samp>’</dt> <dd><p>Add the definition DEF to the compiler call. </p> </dd> <dt>‘<samp>-l LIB</samp>’</dt> <dd><p>Add the library LIB to the link command. </p> </dd> <dt>‘<samp>-L DIR</samp>’</dt> <dd><p>Add the library directory DIR to the link command. </p> </dd> <dt>‘<samp>-M</samp>’</dt> <dt>‘<samp>--depend</samp>’</dt> <dd><p>Generate dependency files (.d) for C and C++ source files. </p> </dd> <dt>‘<samp>-R DIR</samp>’</dt> <dd><p>Add the run-time path to the link command. </p> </dd> <dt>‘<samp>-Wl,…</samp>’</dt> <dd><p>Pass options to the linker like "-Wl,-rpath=…". The quotes are needed since commas are interpreted as command separators. </p> </dd> <dt>‘<samp>-W…</samp>’</dt> <dd><p>Pass options to the assembler like "-Wa,OPTION". </p> </dd> <dt>‘<samp>-c</samp>’</dt> <dd><p>Compile but do not link. </p> </dd> <dt>‘<samp>-g</samp>’</dt> <dd><p>Enable debugging options for compilers. </p> </dd> <dt>‘<samp>-o FILE</samp>’</dt> <dt>‘<samp>--output FILE</samp>’</dt> <dd><p>Output filename. Default extension is .oct (or .mex if ‘<samp>--mex</samp>’ is specified) unless linking a stand-alone executable. </p> </dd> <dt>‘<samp>-p VAR</samp>’</dt> <dt>‘<samp>--print VAR</samp>’</dt> <dd><p>Print configuration variable VAR. There are three categories of variables: </p> <p>Octave configuration variables that users may override with environment variables. These are used in commands that <code>mkoctfile</code> executes. </p> <div class="example"> <pre class="example"> ALL_CFLAGS LAPACK_LIBS ALL_CXXFLAGS LDFLAGS ALL_FFLAGS LD_CXX ALL_LDFLAGS LD_STATIC_FLAG BLAS_LIBS LFLAGS CC LIBDIR CFLAGS LIBOCTAVE CPICFLAG LIBOCTINTERP CPPFLAGS OCTAVE_LINK_OPTS CXX OCTINCLUDEDIR CXXFLAGS OCTAVE_LIBS CXXPICFLAG OCTAVE_LINK_DEPS DL_LD 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 INCLUDEDIR </pre></div> <p>Octave configuration variables as above, but currently unused by <code>mkoctfile</code>. </p> <div class="example"> <pre class="example"> AR DEPEND_EXTRA_SED_PATTERN DEPEND_FLAGS FFTW3F_LDFLAGS FFTW3F_LIBS FFTW3_LDFLAGS FFTW3_LIBS FFTW_LIBS FLIBS LIBS RANLIB READLINE_LIBS </pre></div> <p>Octave configuration variables that are provided for informational purposes only. Except for ‘<samp>OCTAVE_HOME</samp>’ and ‘<samp>OCTAVE_EXEC_HOME</samp>’, users may not override these variables. </p> <p>If <code><span class="nolinebreak">OCTAVE_HOME</span></code><!-- /@w --> or <code><span class="nolinebreak">OCTAVE_EXEC_HOME</span></code><!-- /@w --> are set in the environment, then other variables are adjusted accordingly with <code><span class="nolinebreak">OCTAVE_HOME</span></code><!-- /@w --> or <code><span class="nolinebreak">OCTAVE_EXEC_HOME</span></code><!-- /@w --> substituted for the original value of the directory specified by the <samp>--prefix</samp> or <samp>--exec-prefix</samp> options that were used when Octave was configured. </p> <div class="example"> <pre class="example"> API_VERSION LOCALFCNFILEDIR ARCHLIBDIR LOCALOCTFILEDIR BINDIR LOCALSTARTUPFILEDIR CANONICAL_HOST_TYPE LOCALVERARCHLIBDIR DATADIR LOCALVERFCNFILEDIR DATAROOTDIR LOCALVEROCTFILEDIR DEFAULT_PAGER MAN1DIR EXEC_PREFIX MAN1EXT EXEEXT MANDIR FCNFILEDIR OCTAVE_EXEC_HOME IMAGEDIR OCTAVE_HOME INFODIR OCTAVE_VERSION INFOFILE OCTDATADIR LIBEXECDIR OCTDOCDIR LOCALAPIARCHLIBDIR OCTFILEDIR LOCALAPIFCNFILEDIR OCTFONTSDIR LOCALAPIOCTFILEDIR STARTUPFILEDIR LOCALARCHLIBDIR </pre></div> </dd> <dt>‘<samp>--link-stand-alone</samp>’</dt> <dd><p>Link a stand-alone executable file. </p> </dd> <dt>‘<samp>--mex</samp>’</dt> <dd><p>Assume we are creating a MEX file. Set the default output extension to ".mex". </p> </dd> <dt>‘<samp>-s</samp>’</dt> <dt>‘<samp>--strip</samp>’</dt> <dd><p>Strip the output file. </p> </dd> <dt>‘<samp>-v</samp>’</dt> <dt>‘<samp>--verbose</samp>’</dt> <dd><p>Echo commands as they are executed. </p> </dd> <dt>‘<samp>file</samp>’</dt> <dd><p>The file to compile or link. Recognized file types are: </p> <div class="example"> <pre class="example"> .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 </pre></div> </dd> </dl> </dd></dl> <p>Consider the following short example which introduces the basics of writing a C++ function that can be linked to Octave. </p> <div class="example"> <pre class="verbatim">#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; } </pre></div> <p>The first critical line is <code>#include <octave/oct.h></code> which makes available most of the definitions necessary for a C++ oct-file. Note that <samp>octave/oct.h</samp> is a C++ header and cannot be directly <code>#include</code>’ed in a C source file, nor any other language. </p> <p>Included by <samp>oct.h</samp> is a definition for the macro <code><span class="nolinebreak">DEFUN_DLD</span></code><!-- /@w --> which creates a dynamically loaded function. This macro takes four arguments: </p> <ol> <li> The function name as it will be seen in Octave, </li><li> The list of arguments to the function of type <code>octave_value_list</code>, </li><li> The number of output arguments, which can be—and often is—omitted if not used, and </li><li> The string to use for the help text of the function. </li></ol> <p>The return type of functions defined with <code><span class="nolinebreak">DEFUN_DLD</span></code><!-- /@w --> is always <code>octave_value_list</code>. </p> <p>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 <code><span class="nolinebreak">DEFUN_DLD</span></code><!-- /@w --> macro must match the filename of the oct-file. Therefore, the above function should be in a file <samp>helloworld.cc</samp>, and would be compiled to an oct-file using the command </p> <div class="example"> <pre class="example">mkoctfile helloworld.cc </pre></div> <p>This will create a file called <samp>helloworld.oct</samp> that is the compiled version of the function. It should be noted that it is perfectly acceptable to have more than one <code><span class="nolinebreak">DEFUN_DLD</span></code><!-- /@w --> 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 <code><span class="nolinebreak">DEFUN_DLD</span></code><!-- /@w --> macro or the <code>autoload</code> (<a href="Function-Files.html#Function-Files">Function Files</a>) function should be used. </p> <p>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 </p> <div class="example"> <pre class="example">helloworld (1, 2, 3) -| Hello World has 3 input arguments and 0 output arguments. </pre></div> <p>Subsequent sections show how to use specific classes from Octave’s core internals. Base classes like <code>dMatrix</code> (a matrix of double values) are found in the directory <samp>liboctave/array</samp>. 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. </p> <hr> <div class="header"> <p> Next: <a href="Matrices-and-Arrays-in-Oct_002dFiles.html#Matrices-and-Arrays-in-Oct_002dFiles" accesskey="n" rel="next">Matrices and Arrays in Oct-Files</a>, Up: <a href="Oct_002dFiles.html#Oct_002dFiles" accesskey="u" rel="up">Oct-Files</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p> </div> </body> </html>