Sophie

Sophie

distrib > Mandriva > 2010.0 > i586 > media > contrib-release > by-pkgid > 3e60ff9d4d6f58c8fbd17208f14089fa > files > 382

octave-doc-3.2.3-3mdv2010.0.i586.rpm

<html lang="en">
<head>
<title>Standalone Programs - 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="Dynamically-Linked-Functions.html#Dynamically-Linked-Functions" title="Dynamically Linked Functions">
<link rel="prev" href="Mex_002dFiles.html#Mex_002dFiles" title="Mex-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="Standalone-Programs"></a>
<p>
Previous:&nbsp;<a rel="previous" accesskey="p" href="Mex_002dFiles.html#Mex_002dFiles">Mex-Files</a>,
Up:&nbsp;<a rel="up" accesskey="u" href="Dynamically-Linked-Functions.html#Dynamically-Linked-Functions">Dynamically Linked Functions</a>
<hr>
</div>

<h3 class="section">A.3 Standalone Programs</h3>

<p>The libraries Octave itself uses, can be utilized in standalone
applications.  These applications then have access, for example, to the
array and matrix classes as well as to all the Octave algorithms.  The
following C++ program, uses class Matrix from liboctave.a or
liboctave.so.

<pre class="example"><pre class="verbatim">     #include &lt;iostream>
     #include &lt;octave/oct.h>
     
     int
     main (void)
     {
       std::cout &lt;&lt; "Hello Octave world!\n";
       int n = 2;
       Matrix a_matrix = Matrix (n, n);
       for (octave_idx_type i = 0; i &lt; n; i++)
         {
           for (octave_idx_type j = 0; j &lt; n; j++)
             {
               a_matrix (i, j) = (i + 1) * 10 + (j + 1);
             }
         }
       std::cout &lt;&lt; a_matrix;
       return 0;
     }
</pre>
</pre>
   <p class="noindent">mkoctfile can then be used to build a standalone application with a
command like

<pre class="example">     $ mkoctfile --link-stand-alone standalone.cc -o standalone
     $ ./standalone
     Hello Octave world!
       11 12
       21 22
     $
</pre>
   <p>Note that the application <code>hello</code> will be dynamically linked
against the octave libraries and any octave support libraries.  The above
allows the Octave math libraries to be used by an application.  It does
not however allow the script files, oct-files or builtin functions of
Octave to be used by the application.  To do that the Octave interpreter
needs to be initialized first.  An example of how to do this can then be
seen in the code

<pre class="example"><pre class="verbatim">     #include &lt;iostream>
     #include &lt;octave/oct.h>
     #include &lt;octave/octave.h>
     #include &lt;octave/parse.h>
     
     int
     main (void)
     {
       string_vector argv (2);
       argv(0) = "embedded";
       argv(1) = "-q";
     
       octave_main (2, argv.c_str_vec(), 1);
     
       octave_idx_type n = 2;
       Matrix a_matrix = Matrix (1, 2);
     
       std::cout &lt;&lt; "GCD of [";
       for (octave_idx_type i = 0; i &lt; n; i++)
         {
           a_matrix (i) = 5 * (i + 1); 
           if (i != 0)
     	std::cout &lt;&lt; ", " &lt;&lt; 5 * (i + 2);
           else
     	std::cout &lt;&lt; 5 * (i + 2);
         }
       std::cout &lt;&lt; "] is ";
     
       octave_value_list in = octave_value (a_matrix);
       octave_value_list out = feval ("gcd", in, 1);
     
       if (!error_state &amp;&amp; out.length () > 0)
         {
           a_matrix = out(0).matrix_value ();
           if (a_matrix.numel () == 1)
     	std::cout &lt;&lt; a_matrix(0) &lt;&lt; "\n";
           else
     	std::cout &lt;&lt; "invalid\n";
         }
       else
         std::cout &lt;&lt; "invalid\n";
     
       return 0;
     }
</pre>
</pre>
   <p class="noindent">which is compiled and run as before as a standalone application with

<pre class="example">     $ mkoctfile --link-stand-alone embedded.cc -o embedded
     $ ./embedded
     GCD of [10, 15] is 5
     $
</pre>
   <!-- DO NOT EDIT!  Generated automatically by munge-texi. -->
<!-- Copyright (C) 2005, 2007, 2009 David Bateman -->
<!-- Copyright (C) 2002, 2003, 2004, 2005 Paul Kienzle -->
<!-- This file is part of Octave. -->
<!-- Octave is free software; you can redistribute it and/or modify it -->
<!-- under the terms of the GNU General Public License as published by the -->
<!-- Free Software Foundation; either version 3 of the License, or (at -->
<!-- your option) any later version. -->
<!-- Octave is distributed in the hope that it will be useful, but WITHOUT -->
<!-- ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -->
<!-- FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License -->
<!-- for more details. -->
<!-- You should have received a copy of the GNU General Public License -->
<!-- along with Octave; see the file COPYING.  If not, see -->
<!-- <http://www.gnu.org/licenses/>. -->
   </body></html>