Sophie

Sophie

distrib > Mageia > 4 > x86_64 > by-pkgid > b38d2da330d1936e5ab1307c039c4941 > files > 283

octave-doc-3.6.4-3.mga4.noarch.rpm

<html lang="en">
<head>
<title>Getting Started with Oct-Files - GNU Octave</title>
<meta http-equiv="Content-Type" content="text/html">
<meta name="description" content="GNU Octave">
<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:&nbsp;<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:&nbsp;<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.

<!-- mkoctfile scripts/miscellaneous/mkoctfile.m -->
   <p><a name="doc_002dmkoctfile"></a>

<div class="defun">
&mdash; Command:  <b>mkoctfile</b> [<var>-options</var>]<var> file <small class="dots">...</small><a name="index-mkoctfile-3361"></a></var><br>
&mdash; Function File: [<var>output</var>, <var>status</var> = <b>mkoctfile</b> (<var><small class="dots">...</small></var>)<var><a name="index-mkoctfile-3362"></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.  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><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>&lsquo;<samp><span class="samp">-I DIR</span></samp>&rsquo;<dd>Add the include directory DIR to compile commands.

          <br><dt>&lsquo;<samp><span class="samp">-D DEF</span></samp>&rsquo;<dd>Add the definition DEF to the compiler call.

          <br><dt>&lsquo;<samp><span class="samp">-l LIB</span></samp>&rsquo;<dd>Add the library LIB to the link command.

          <br><dt>&lsquo;<samp><span class="samp">-L DIR</span></samp>&rsquo;<dd>Add the library directory DIR to the link command.

          <br><dt>&lsquo;<samp><span class="samp">-M</span></samp>&rsquo;<dt>&lsquo;<samp><span class="samp">--depend</span></samp>&rsquo;<dd>Generate dependency files (.d) for C and C++ source files.

          <br><dt>&lsquo;<samp><span class="samp">-R DIR</span></samp>&rsquo;<dd>Add the run-time path to the link command.

          <br><dt>&lsquo;<samp><span class="samp">-Wl,...</span></samp>&rsquo;<dd>Pass flags though the linker like "-Wl,-rpath=<small class="dots">...</small>". 
The quotes are needed since commas are interpreted as command
separators.

          <br><dt>&lsquo;<samp><span class="samp">-W...</span></samp>&rsquo;<dd>Pass flags though the compiler like "-Wa,OPTION".

          <br><dt>&lsquo;<samp><span class="samp">-c</span></samp>&rsquo;<dd>Compile but do not link.

          <br><dt>&lsquo;<samp><span class="samp">-g</span></samp>&rsquo;<dd>Enable debugging options for compilers.

          <br><dt>&lsquo;<samp><span class="samp">-o FILE</span></samp>&rsquo;<dt>&lsquo;<samp><span class="samp">--output FILE</span></samp>&rsquo;<dd>Output file name.  Default extension is .oct
(or .mex if &lsquo;<samp><span class="samp">--mex</span></samp>&rsquo; is specified) unless linking
a stand-alone executable.

          <br><dt>&lsquo;<samp><span class="samp">-p VAR</span></samp>&rsquo;<dt>&lsquo;<samp><span class="samp">--print VAR</span></samp>&rsquo;<dd>Print the configuration variable VAR.  Recognized variables are:

          <pre class="example">                  ALL_CFLAGS                FFTW3F_LIBS
                  ALL_CXXFLAGS              FLIBS
                  ALL_FFLAGS                FPICFLAG
                  ALL_LDFLAGS               INCFLAGS
                  BLAS_LIBS                 LAPACK_LIBS
                  CC                        LDFLAGS
                  CFLAGS                    LD_CXX
                  CPICFLAG                  LD_STATIC_FLAG
                  CPPFLAGS                  LFLAGS
                  CXX                       LIBCRUFT
                  CXXFLAGS                  LIBOCTAVE
                  CXXPICFLAG                LIBOCTINTERP
                  DEPEND_EXTRA_SED_PATTERN  LIBS
                  DEPEND_FLAGS              OCTAVE_LIBS
                  DL_LD                     OCTAVE_LINK_DEPS
                  DL_LDFLAGS                OCT_LINK_DEPS
                  EXEEXT                    RDYNAMIC_FLAG
                  F77                       READLINE_LIBS
                  F77_INTEGER_8_FLAG        SED
                  FFLAGS                    XTRA_CFLAGS
                  FFTW3_LDFLAGS             XTRA_CXXFLAGS
                  FFTW3_LIBS
                  FFTW3F_LDFLAGS
               
</pre>
          <br><dt>&lsquo;<samp><span class="samp">--link-stand-alone</span></samp>&rsquo;<dd>Link a stand-alone executable file.

          <br><dt>&lsquo;<samp><span class="samp">--mex</span></samp>&rsquo;<dd>Assume we are creating a MEX file.  Set the default output extension
to ".mex".

          <br><dt>&lsquo;<samp><span class="samp">-s</span></samp>&rsquo;<dt>&lsquo;<samp><span class="samp">--strip</span></samp>&rsquo;<dd>Strip the output file.

          <br><dt>&lsquo;<samp><span class="samp">-v</span></samp>&rsquo;<dt>&lsquo;<samp><span class="samp">--verbose</span></samp>&rsquo;<dd>Echo commands as they are executed.

          <br><dt>&lsquo;<samp><span class="samp">file</span></samp>&rsquo;<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 (fixed form)
                  .F    Fortran source (fixed form)
                  .f90  Fortran source (free form)
                  .F90  Fortran source (free form)
                  .o    object file
                  .a    library file
</pre>
          </dl>
        </p></blockquote></div>

   <p>Consider the short C++ example:

<pre class="example"><pre class="verbatim">     #include &lt;octave/oct.h>
     
     DEFUN_DLD (helloworld, args, nargout,
       "Hello World Help String")
     {
       int nargin = args.length ();
       octave_stdout &lt;&lt; "Hello World has " &lt;&lt; nargin
             &lt;&lt; " input arguments and "
             &lt;&lt; nargout &lt;&lt; " 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 a C++
oct-file in Octave is to use the <code>#include &lt;octave/oct.h&gt;</code> header. 
Note that <samp><span class="file">octave/oct.h</span></samp> is a C++ header and cannot be directly
<code>#include</code>'ed in a C source file, nor any other language.  What
follows is mostly C++, with a discussion of other languages in section
<a href="Calling-External-Code-from-Oct_002dFiles.html#Calling-External-Code-from-Oct_002dFiles">Calling External Code from Oct-Files</a>.

   <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>