File: Getting-Started-with-Oct_002dFiles.html

package info (click to toggle)
octave 6.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 124,192 kB
  • sloc: cpp: 322,665; ansic: 68,088; fortran: 20,980; objc: 8,121; sh: 7,719; yacc: 4,266; lex: 4,123; perl: 1,530; java: 1,366; awk: 1,257; makefile: 424; xml: 147
file content (342 lines) | stat: -rw-r--r-- 14,022 bytes parent folder | download
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- Created by GNU Texinfo 6.7, 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 6.2.0))</title>

<meta name="description" content="Getting Started with Oct-Files (GNU Octave (version 6.2.0))">
<meta name="keywords" content="Getting Started with Oct-Files (GNU Octave (version 6.2.0))">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<link href="index.html" rel="start" title="Top">
<link href="Concept-Index.html" rel="index" title="Concept Index">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="Oct_002dFiles.html" rel="up" title="Oct-Files">
<link href="Matrices-and-Arrays-in-Oct_002dFiles.html" rel="next" title="Matrices and Arrays in Oct-Files">
<link href="Oct_002dFiles.html" rel="prev" title="Oct-Files">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.indentedblock {margin-right: 0em}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.lisp {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}
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">
<span id="Getting-Started-with-Oct_002dFiles"></span><div class="header">
<p>
Next: <a href="Matrices-and-Arrays-in-Oct_002dFiles.html" accesskey="n" rel="next">Matrices and Arrays in Oct-Files</a>, Up: <a href="Oct_002dFiles.html" accesskey="u" rel="up">Oct-Files</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<span id="Getting-Started-with-Oct_002dFiles-1"></span><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>
<span id="XREFmkoctfile"></span><dl>
<dt id="index-mkoctfile-1">: <em></em> <strong>mkoctfile</strong> <em>[-options] file &hellip;</em></dt>
<dt id="index-mkoctfile-2">: <em>[<var>output</var>, <var>status</var>] =</em> <strong>mkoctfile</strong> <em>(&hellip;)</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.  Any output is stored in the <var>output</var> variable and
the exit status in the <var>status</var> variable.  If called with no outputs
and the compilation fails then Octave will emit an error.  If the programmer
requests <var>output</var> or <var>status</var>, however, Octave will merely issue
a warning and it is the programmer&rsquo;s responsibility to verify the command
was successful.
</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>&lsquo;<samp>-I DIR</samp>&rsquo;</dt>
<dd><p>Add the include directory DIR to compile commands.
</p>
</dd>
<dt>&lsquo;<samp>-D DEF</samp>&rsquo;</dt>
<dd><p>Add the definition DEF to the compiler call.
</p>
</dd>
<dt>&lsquo;<samp>-l LIB</samp>&rsquo;</dt>
<dd><p>Add the library LIB to the link command.
</p>
</dd>
<dt>&lsquo;<samp>-L DIR</samp>&rsquo;</dt>
<dd><p>Add the library directory DIR to the link command.
</p>
</dd>
<dt>&lsquo;<samp>-M</samp>&rsquo;</dt>
<dt>&lsquo;<samp>--depend</samp>&rsquo;</dt>
<dd><p>Generate dependency files (.d) for C and C++ source files.
</p>
</dd>
<dt>&lsquo;<samp>-R DIR</samp>&rsquo;</dt>
<dd><p>Add the run-time path to the link command.
</p>
</dd>
<dt>&lsquo;<samp>-Wl,&hellip;</samp>&rsquo;</dt>
<dd><p>Pass options to the linker like &quot;-Wl,-rpath=&hellip;&quot;.
The quotes are needed since commas are interpreted as command
separators.
</p>
</dd>
<dt>&lsquo;<samp>-W&hellip;</samp>&rsquo;</dt>
<dd><p>Pass options to the assembler like &quot;-Wa,OPTION&quot;.
</p>
</dd>
<dt>&lsquo;<samp>-c</samp>&rsquo;</dt>
<dd><p>Compile but do not link.
</p>
</dd>
<dt>&lsquo;<samp>-g</samp>&rsquo;</dt>
<dd><p>Enable debugging options for compilers.
</p>
</dd>
<dt>&lsquo;<samp>-o FILE</samp>&rsquo;</dt>
<dt>&lsquo;<samp>--output FILE</samp>&rsquo;</dt>
<dd><p>Output filename.  Default extension is <samp>.oct</samp> (or <samp>.mex</samp> if
&lsquo;<samp>--mex</samp>&rsquo; is specified) unless linking a stand-alone executable.
</p>
</dd>
<dt>&lsquo;<samp>-p VAR</samp>&rsquo;</dt>
<dt>&lsquo;<samp>--print VAR</samp>&rsquo;</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                  INCLUDEDIR
   ALL_CXXFLAGS                LAPACK_LIBS
   ALL_FFLAGS                  LDFLAGS
   ALL_LDFLAGS                 LD_STATIC_FLAG
   BLAS_LIBS                   LFLAGS
   CC                          LIBDIR
   CFLAGS                      LIBOCTAVE
   CPICFLAG                    LIBOCTINTERP
   CPPFLAGS                    OCTAVE_LINK_OPTS
   CXX                         OCTINCLUDEDIR
   CXXFLAGS                    OCTAVE_LIBS
   CXXLD                       OCTAVE_LINK_DEPS
   CXXPICFLAG                  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
</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 &lsquo;<samp>OCTAVE_HOME</samp>&rsquo; and &lsquo;<samp>OCTAVE_EXEC_HOME</samp>&rsquo;,
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>&lsquo;<samp>--link-stand-alone</samp>&rsquo;</dt>
<dd><p>Link a stand-alone executable file.
</p>
</dd>
<dt>&lsquo;<samp>--mex</samp>&rsquo;</dt>
<dd><p>Assume creation of a MEX file.  Set the default output extension to
<samp>.mex</samp>.
</p>
</dd>
<dt>&lsquo;<samp>-s</samp>&rsquo;</dt>
<dt>&lsquo;<samp>--strip</samp>&rsquo;</dt>
<dd><p>Strip the output file.
</p>
</dd>
<dt>&lsquo;<samp>-v</samp>&rsquo;</dt>
<dt>&lsquo;<samp>--verbose</samp>&rsquo;</dt>
<dd><p>Echo commands as they are executed.
</p>
</dd>
<dt>&lsquo;<samp>file</samp>&rsquo;</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 &lt;octave/oct.h&gt;

DEFUN_DLD (helloworld, args, nargout,
           &quot;Hello World Help String&quot;)
{
  octave_stdout &lt;&lt; &quot;Hello World has &quot;
                &lt;&lt; args.length () &lt;&lt; &quot; input arguments and &quot;
                &lt;&lt; nargout &lt;&lt; &quot; output arguments.\n&quot;;

  // Return empty matrices for any outputs
  octave_value_list retval (nargout);
  for (int i = 0; i &lt; nargout; i++)
    retval(i) = octave_value (Matrix ());

  return retval;
}
</pre></div>

<p>The first critical line is <code>#include &lt;octave/oct.h&gt;</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>&rsquo;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&mdash;and often is&mdash;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</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&rsquo;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" accesskey="n" rel="next">Matrices and Arrays in Oct-Files</a>, Up: <a href="Oct_002dFiles.html" accesskey="u" rel="up">Oct-Files</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html" title="Index" rel="index">Index</a>]</p>
</div>



</body>
</html>