File: Getting-Started-with-Oct_002dFiles.html

package info (click to toggle)
octave 10.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 145,388 kB
  • sloc: cpp: 335,976; ansic: 82,241; fortran: 20,963; objc: 9,402; sh: 8,756; yacc: 4,392; lex: 4,333; perl: 1,544; java: 1,366; awk: 1,259; makefile: 659; xml: 192
file content (341 lines) | stat: -rw-r--r-- 15,088 bytes parent folder | download | duplicates (2)
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
<!DOCTYPE html>
<html>
<!-- Created by GNU Texinfo 7.1.1, https://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 10.3.0))</title>

<meta name="description" content="Getting Started with Oct-Files (GNU Octave (version 10.3.0))">
<meta name="keywords" content="Getting Started with Oct-Files (GNU Octave (version 10.3.0))">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<meta name="viewport" content="width=device-width,initial-scale=1">

<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">
<style type="text/css">
<!--
a.copiable-link {visibility: hidden; text-decoration: none; line-height: 0em}
div.example {margin-left: 3.2em}
span:hover a.copiable-link {visibility: visible}
strong.def-name {font-family: monospace; font-weight: bold; font-size: larger}
-->
</style>
<link rel="stylesheet" type="text/css" href="octave.css">


</head>

<body lang="en">
<div class="subsection-level-extent" id="Getting-Started-with-Oct_002dFiles">
<div class="nav-panel">
<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>
<h4 class="subsection" id="Getting-Started-with-Oct_002dFiles-1"><span>A.1.1 Getting Started with Oct-Files<a class="copiable-link" href="#Getting-Started-with-Oct_002dFiles-1"> &para;</a></span></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 class="file">.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 class="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 class="anchor" id="XREFmkoctfile"></a><span style="display:block; margin-top:-4.5ex;">&nbsp;</span>


<dl class="first-deftypefn">
<dt class="deftypefn" id="index-mkoctfile-1"><span><strong class="def-name">mkoctfile</strong> <code class="def-code-arguments">[-options] file &hellip;</code><a class="copiable-link" href="#index-mkoctfile-1"> &para;</a></span></dt>
<dt class="deftypefnx def-cmd-deftypefn" id="index-mkoctfile-2"><span><code class="def-type">[<var class="var">output</var>, <var class="var">status</var>] =</code> <strong class="def-name">mkoctfile</strong> <code class="def-code-arguments">(&hellip;)</code><a class="copiable-link" href="#index-mkoctfile-2"> &para;</a></span></dt>
<dd>
<p>The <code class="code">mkoctfile</code> function compiles source code written in C, C++, or
Fortran.  Depending on the options used with <code class="code">mkoctfile</code>, the
compiled code can be called within Octave or can be used as a stand-alone
application.
</p>
<p><code class="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 class="var">output</var> variable and
the exit status in the <var class="var">status</var> variable.  If called with no outputs
and the compilation fails then Octave will emit an error.  If the programmer
requests <var class="var">output</var> or <var class="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 class="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 class="table">
<dt>&lsquo;<samp class="samp">-I DIR</samp>&rsquo;</dt>
<dd><p>Add the include directory DIR to compile commands.
</p>
</dd>
<dt>&lsquo;<samp class="samp">-D DEF</samp>&rsquo;</dt>
<dd><p>Add the definition DEF to the compiler call.
</p>
</dd>
<dt>&lsquo;<samp class="samp">-l LIB</samp>&rsquo;</dt>
<dd><p>Add the library LIB to the link command.
</p>
</dd>
<dt>&lsquo;<samp class="samp">-L DIR</samp>&rsquo;</dt>
<dd><p>Add the library directory DIR to the link command.
</p>
</dd>
<dt>&lsquo;<samp class="samp">-M</samp>&rsquo;</dt>
<dt>&lsquo;<samp class="samp">--depend</samp>&rsquo;</dt>
<dd><p>Generate dependency files (.d) for C and C++ source files.
</p>
</dd>
<dt>&lsquo;<samp class="samp">-R DIR</samp>&rsquo;</dt>
<dd><p>Add the run-time path to the link command.
</p>
</dd>
<dt>&lsquo;<samp class="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 class="samp">-W&hellip;</samp>&rsquo;</dt>
<dd><p>Pass options to the assembler like &quot;-Wa,OPTION&quot;.
</p>
</dd>
<dt>&lsquo;<samp class="samp">-c</samp>&rsquo;</dt>
<dd><p>Compile but do not link.
</p>
</dd>
<dt>&lsquo;<samp class="samp">-g</samp>&rsquo;</dt>
<dd><p>Enable debugging options for compilers.
</p>
</dd>
<dt>&lsquo;<samp class="samp">-o FILE</samp>&rsquo;</dt>
<dt>&lsquo;<samp class="samp">--output FILE</samp>&rsquo;</dt>
<dd><p>Output filename.  Default extension is <samp class="file">.oct</samp> (or <samp class="file">.mex</samp> if
&lsquo;<samp class="samp">--mex</samp>&rsquo; is specified) unless linking a stand-alone executable.
</p>
</dd>
<dt>&lsquo;<samp class="samp">-p VAR</samp>&rsquo;</dt>
<dt>&lsquo;<samp class="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 class="code">mkoctfile</code> executes.
</p>
<div class="example">
<pre class="example-preformatted">   ALL_CFLAGS                  INCLUDEDIR
   ALL_CXXFLAGS                LAPACK_LIBS
   ALL_FFLAGS                  LDFLAGS
   ALL_LDFLAGS                 LD_STATIC_FLAG
   BLAS_LIBS                   LIBDIR
   CC                          LIBOCTAVE
   CFLAGS                      LIBOCTINTERP
   CPICFLAG                    LIBOCTMEX
   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 class="code">mkoctfile</code>.
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">   AR
   DEPEND_EXTRA_SED_PATTERN
   DEPEND_FLAGS
   FFTW3F_LDFLAGS
   FFTW3F_LIBS
   FFTW3_LDFLAGS
   FFTW3_LIBS
   FFTW_LIBS
   FLIBS
   LIBS
   RANLIB
   READLINE_LIBS
</pre></div></div>

<p>Octave configuration variables that are provided for informational
purposes only.  Except for &lsquo;<samp class="samp">OCTAVE_HOME</samp>&rsquo; and &lsquo;<samp class="samp">OCTAVE_EXEC_HOME</samp>&rsquo;,
users may not override these variables.
</p>
<p>If <code class="env">OCTAVE_HOME</code><!-- /@w -->&nbsp;or <code class="env">OCTAVE_EXEC_HOME</code><!-- /@w -->&nbsp;are set in the
environment, then other variables are adjusted accordingly with
<code class="env">OCTAVE_HOME</code><!-- /@w -->&nbsp;or <code class="env">OCTAVE_EXEC_HOME</code><!-- /@w -->&nbsp;substituted for the
original value of the directory specified by the <samp class="option">--prefix</samp> or
<samp class="option">--exec-prefix</samp> options that were used when Octave was configured.
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">   API_VERSION                 LOCALARCHLIBDIR
   ARCHLIBDIR                  LOCALFCNFILEDIR
   BINDIR                      LOCALOCTFILEDIR
   CANONICAL_HOST_TYPE         LOCALSTARTUPFILEDIR
   DATADIR                     LOCALVERARCHLIBDIR
   DATAROOTDIR                 LOCALVERFCNFILEDIR
   DEFAULT_PAGER               LOCALVEROCTFILEDIR
   EXEC_PREFIX                 MAN1DIR
   EXEEXT                      MAN1EXT
   FCNFILEDIR                  MANDIR
   IMAGEDIR                    OCTAVE_EXEC_HOME
   INCLUDEDIR                  OCTAVE_HOME
   INFODIR                     OCTDATADIR
   INFOFILE                    OCTDOCDIR
   LIBDIR                      OCTFILEDIR
   LIBEXECDIR                  OCTFONTSDIR
   LOCALAPIARCHLIBDIR          OCTINCLUDEDIR
   LOCALAPIFCNFILEDIR          OCTLIBDIR
   LOCALAPIOCTFILEDIR          STARTUPFILEDIR
   LOCALAPIPKGDIR              VERSION
</pre></div></div>

</dd>
<dt>&lsquo;<samp class="samp">--link-stand-alone</samp>&rsquo;</dt>
<dd><p>Link a stand-alone executable file.
</p>
</dd>
<dt>&lsquo;<samp class="samp">--mex</samp>&rsquo;</dt>
<dd><p>Assume creation of a MEX file.  Set the default output extension to
<samp class="file">.mex</samp>.  Link to liboctmex instead of liboctinterp and liboctave.
</p>
</dd>
<dt>&lsquo;<samp class="samp">-s</samp>&rsquo;</dt>
<dt>&lsquo;<samp class="samp">--strip</samp>&rsquo;</dt>
<dd><p>Strip the output file.
</p>
</dd>
<dt>&lsquo;<samp class="samp">-v</samp>&rsquo;</dt>
<dt>&lsquo;<samp class="samp">--verbose</samp>&rsquo;</dt>
<dd><p>Echo commands as they are executed.
</p>
</dd>
<dt>&lsquo;<samp class="samp">file</samp>&rsquo;</dt>
<dd><p>The file to compile or link.  Recognized file types are:
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">   .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></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">
<div class="group"><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></div>

<p>The first critical line is <code class="code">#include &lt;octave/oct.h&gt;</code> which makes available
most of the definitions necessary for a C++ oct-file.  Note that
<samp class="file">octave/oct.h</samp> is a C++ header and cannot be directly <code class="code">#include</code>&rsquo;ed
in a C source file, nor any other language.
</p>
<p>Included by <samp class="file">oct.h</samp> is a definition for the macro
<code class="code">DEFUN_DLD</code><!-- /@w -->&nbsp;which creates a dynamically loaded function.  This macro
takes four arguments:
</p>
<ol class="enumerate">
<li> The function name as it will be seen in Octave,

</li><li> The list of arguments to the function of type <code class="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 class="code">DEFUN_DLD</code><!-- /@w -->&nbsp;is always
<code class="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 class="code">DEFUN_DLD</code><!-- /@w -->&nbsp;macro must match the filename
of the oct-file.  Therefore, the above function should be in a file
<samp class="file">helloworld.cc</samp>, and would be compiled to an oct-file using the command
</p>
<div class="example">
<pre class="example-preformatted">mkoctfile helloworld.cc
</pre></div>

<p>This will create a file called <samp class="file">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 class="code">DEFUN_DLD</code><!-- /@w -->&nbsp;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 class="code">DEFUN_DLD</code><!-- /@w -->&nbsp;macro or the
<code class="code">autoload</code> (<a class="ref" 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">
<div class="group"><pre class="example-preformatted">helloworld (1, 2, 3)
-| Hello World has 3 input arguments and 0 output arguments.
</pre></div></div>

<p>Subsequent sections show how to use specific classes from Octave&rsquo;s core
internals.  Base classes like <code class="code">dMatrix</code> (a matrix of double values) are
found in the directory <samp class="file">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>
</div>
<hr>
<div class="nav-panel">
<p>
Next: <a href="Matrices-and-Arrays-in-Oct_002dFiles.html">Matrices and Arrays in Oct-Files</a>, Up: <a href="Oct_002dFiles.html">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>