1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- Created by GNU Texinfo 5.2, http://www.gnu.org/software/texinfo/ -->
<head>
<title>GNU Octave: Getting Started with Oct-Files</title>
<meta name="description" content="GNU Octave: Getting Started with Oct-Files">
<meta name="keywords" content="GNU Octave: Getting Started with Oct-Files">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<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.smallquotation {font-size: smaller}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.indentedblock {margin-left: 3.2em}
div.lisp {margin-left: 3.2em}
div.smalldisplay {margin-left: 3.2em}
div.smallexample {margin-left: 3.2em}
div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
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.nocodebreak {white-space:nowrap}
span.nolinebreak {white-space:nowrap}
span.roman {font-family:serif; font-weight:normal}
span.sansserif {font-family:sans-serif; font-weight:normal}
ul.no-bullet {list-style: none}
-->
</style>
</head>
<body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
<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>Command: <em></em> <strong>mkoctfile</strong> <em>[-options] file …</em></dt>
<dt><a name="index-mkoctfile-2"></a>Function File: <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 file name 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 flags though 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 flags though the compiler 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 file name. 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 the configuration variable VAR. Recognized variables are:
</p>
<div class="example">
<pre class="example"> ALL_CFLAGS INCFLAGS
ALL_CXXFLAGS INCLUDEDIR
ALL_FFLAGS LAPACK_LIBS
ALL_LDFLAGS LD_CXX
AR LDFLAGS
BLAS_LIBS LD_STATIC_FLAG
CC LFLAGS
CFLAGS LIBDIR
CPICFLAG LIBOCTAVE
CPPFLAGS LIBOCTINTERP
CXX LIBS
CXXFLAGS OCTAVE_HOME
CXXPICFLAG OCTAVE_LIBS
DEPEND_EXTRA_SED_PATTERN OCTAVE_LINK_DEPS
DEPEND_FLAGS OCTAVE_LINK_OPTS
DL_LD OCTAVE_PREFIX
DL_LDFLAGS OCTINCLUDEDIR
F77 OCTLIBDIR
F77_INTEGER8_FLAG OCT_LINK_DEPS
FFLAGS OCT_LINK_OPTS
FFTW3F_LDFLAGS RANLIB
FFTW3F_LIBS RDYNAMIC_FLAG
FFTW3_LDFLAGS READLINE_LIBS
FFTW3_LIBS SED
FFTW_LIBS SPECIAL_MATH_LIB
FLIBS XTRA_CFLAGS
FPICFLAG XTRA_CXXFLAGS
</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
.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></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")
{
int nargin = args.length ();
octave_stdout << "Hello World has "
<< nargin << " input arguments and "
<< nargout << " output arguments.\n";
return octave_value_list ();
}
</pre><pre class="example">
</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 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 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 dMatrix (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 just to study the examples in the manual in order to be able
to use the 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>
|