<html lang="en"> <head> <title>Getting Started with Oct-Files - Untitled</title> <meta http-equiv="Content-Type" content="text/html"> <meta name="description" content="Untitled"> <meta name="generator" content="makeinfo 4.13"> <link title="Top" rel="start" href="index.html#Top"> <link rel="up" href="Oct_002dFiles.html#Oct_002dFiles" title="Oct-Files"> <link rel="next" href="Matrices-and-Arrays-in-Oct_002dFiles.html#Matrices-and-Arrays-in-Oct_002dFiles" title="Matrices and Arrays in Oct-Files"> <link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> <meta http-equiv="Content-Style-Type" content="text/css"> <style type="text/css"><!-- pre.display { font-family:inherit } pre.format { font-family:inherit } pre.smalldisplay { font-family:inherit; font-size:smaller } pre.smallformat { font-family:inherit; font-size:smaller } pre.smallexample { font-size:smaller } pre.smalllisp { font-size:smaller } span.sc { font-variant:small-caps } span.roman { font-family:serif; font-weight:normal; } span.sansserif { font-family:sans-serif; font-weight:normal; } --></style> </head> <body> <div class="node"> <a name="Getting-Started-with-Oct-Files"></a> <a name="Getting-Started-with-Oct_002dFiles"></a> <p> Next: <a rel="next" accesskey="n" href="Matrices-and-Arrays-in-Oct_002dFiles.html#Matrices-and-Arrays-in-Oct_002dFiles">Matrices and Arrays in Oct-Files</a>, Up: <a rel="up" accesskey="u" href="Oct_002dFiles.html#Oct_002dFiles">Oct-Files</a> <hr> </div> <h4 class="subsection">A.1.1 Getting Started with Oct-Files</h4> <p>The basic command to build oct-files is <code>mkoctfile</code> and it can be call from within octave or from the command line. <!-- ./miscellaneous/mkoctfile.m --> <p><a name="doc_002dmkoctfile"></a> <div class="defun"> — Function File: <b>mkoctfile</b> [<var>-options</var>]<var> file <small class="dots">...</small><a name="index-mkoctfile-2472"></a></var><br> <blockquote> <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><code>mkoctfile</code> can be called from the shell prompt or from the Octave prompt. <p><code>mkoctfile</code> accepts the following options, all of which are optional except for the file name of the code you wish to compile: <dl> <dt>‘<samp><span class="samp">-I DIR</span></samp>’<dd>Add the include directory DIR to compile commands. <br><dt>‘<samp><span class="samp">-D DEF</span></samp>’<dd>Add the definition DEF to the compiler call. <br><dt>‘<samp><span class="samp">-l LIB</span></samp>’<dd>Add the library LIB to the link command. <br><dt>‘<samp><span class="samp">-L DIR</span></samp>’<dd>Add the library directory DIR to the link command. <br><dt>‘<samp><span class="samp">-M</span></samp>’<dt>‘<samp><span class="samp">--depend</span></samp>’<dd>Generate dependency files (.d) for C and C++ source files. <br><dt>‘<samp><span class="samp">-c</span></samp>’<dd>Compile but do not link. <br><dt>‘<samp><span class="samp">-g</span></samp>’<dd>Enable debugging options for compilers. <br><dt>‘<samp><span class="samp">-o FILE</span></samp>’<dt>‘<samp><span class="samp">--output FILE</span></samp>’<dd>Output file name. Default extension is .oct (or .mex if –mex is specified) unless linking a stand-alone executable. <br><dt>‘<samp><span class="samp">-p VAR</span></samp>’<dt>‘<samp><span class="samp">--print VAR</span></samp>’<dd>Print the configuration variable VAR. Recognized variables are: <pre class="example"> ALL_CFLAGS FFTW_LIBS ALL_CXXFLAGS FLIBS ALL_FFLAGS FPICFLAG ALL_LDFLAGS INCFLAGS BLAS_LIBS LDFLAGS CC LD_CXX CFLAGS LD_STATIC_FLAG CPICFLAG LFLAGS CPPFLAGS LIBCRUFT CXX LIBOCTAVE CXXFLAGS LIBOCTINTERP CXXPICFLAG LIBREADLINE DEPEND_EXTRA_SED_PATTERN LIBS DEPEND_FLAGS OCTAVE_LIBS DL_LD RDYNAMIC_FLAG DL_LDFLAGS RLD_FLAG F2C SED F2CFLAGS XTRA_CFLAGS F77 XTRA_CXXFLAGS FFLAGS </pre> <br><dt>‘<samp><span class="samp">--link-stand-alone</span></samp>’<dd>Link a stand-alone executable file. <br><dt>‘<samp><span class="samp">--mex</span></samp>’<dd>Assume we are creating a MEX file. Set the default output extension to ".mex". <br><dt>‘<samp><span class="samp">-s</span></samp>’<dt>‘<samp><span class="samp">--strip</span></samp>’<dd>Strip the output file. <br><dt>‘<samp><span class="samp">-v</span></samp>’<dt>‘<samp><span class="samp">--verbose</span></samp>’<dd>Echo commands as they are executed. <br><dt>‘<samp><span class="samp">file</span></samp>’<dd>The file to compile or link. Recognized file types are <pre class="example"> .c C source .cc C++ source .C C++ source .cpp C++ source .f Fortran source .F Fortran source .o object file </pre> </dl> </p></blockquote></div> <p>Consider the short example <pre class="example"><pre class="verbatim"> #include <octave/oct.h> DEFUN_DLD (helloworld, args, nargout, "Hello World Help String") { int nargin = args.length (); octave_stdout << "Hello World has " << nargin << " input arguments and " << nargout << " output arguments.\n"; return octave_value_list (); } </pre> </pre> <p>This example although short introduces the basics of writing a C++ function that can be dynamically linked to Octave. The easiest way to make available most of the definitions that might be necessary for an oct-file in Octave is to use the <code>#include <octave/oct.h></code> header. <p>The macro that defines the entry point into the dynamically loaded function is <code>DEFUN_DLD</code><!-- /@w -->. This macro takes four arguments, these being <ol type=1 start=1> <li>The function name as it will be seen in Octave, <li>The list of arguments to the function of type <code>octave_value_list</code>, <li>The number of output arguments, which can and often is omitted if not used, and <li>The string that will be seen as the help text of the function. </ol> <p>The return type of functions defined with <code>DEFUN_DLD</code><!-- /@w --> is always <code>octave_value_list</code>. <p>There are a couple of important considerations in the choice of function name. Firstly, it must be a valid Octave function name and so must be a sequence of letters, digits and underscores, not starting with a digit. Secondly, as Octave uses the function name to define the filename it attempts to find the function in, the function name in the <code>DEFUN_DLD</code><!-- /@w --> macro must match the filename of the oct-file. Therefore, the above function should be in a file <samp><span class="file">helloworld.cc</span></samp>, and it should be compiled to an oct-file using the command <pre class="example"> mkoctfile helloworld.cc </pre> <p>This will create a file called <samp><span class="file">helloworld.oct</span></samp>, that is the compiled version of the function. It should be noted that it is perfectly acceptable to have more than one <code>DEFUN_DLD</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>DEFUN_DLD</code><!-- /@w --> macro or the autoload (<a href="Function-Files.html#Function-Files">Function Files</a>) function should be used. <p>The rest of this function then shows how to find the number of input arguments, how to print through the octave pager, and return from the function. After compiling this function as above, an example of its use is <pre class="example"> helloworld (1, 2, 3) -| Hello World has 3 input arguments and 0 output arguments. </pre> </body></html>