2134
|
1 // hello.cc -- example of a dynamically linked function for Octave. |
|
2 |
|
3 // To use this file, your version of Octave must support dynamic |
2153
|
4 // linking. To find out if it does, type the command |
2134
|
5 // |
2217
|
6 // x = octave_config_info; x.DEFS |
2134
|
7 // |
|
8 // at the Octave prompt. Support for dynamic linking is included if |
2217
|
9 // the output contains the string -DWITH_DYNAMIC_LINKING=1. |
2134
|
10 // |
|
11 // To compile this file, type the command |
|
12 // |
2137
|
13 // mkoctfile hello.cc |
2134
|
14 // |
|
15 // at the shell prompt. The script mkoctfile should have been |
|
16 // installed along with Octave. Running it will create a file called |
|
17 // hello.oct that can be loaded by Octave. To test the hello.oct |
|
18 // file, start Octave and type the command |
|
19 // |
|
20 // hello ("easy as", 1, 2, 3) |
|
21 // |
|
22 // at the Octave prompt. Octave should respond by printing |
|
23 // |
|
24 // Hello, world! |
|
25 // easy as |
|
26 // 1 |
|
27 // 2 |
|
28 // 3 |
|
29 // ans = 3 |
|
30 |
|
31 // Additional examples are available in the files in the src directory |
|
32 // of the Octave distribution that use the macro DEFUN_DLD_BUILTIN. |
|
33 // Currently, this includes the files |
|
34 // |
3045
|
35 // balance.cc fft.cc ifft.cc minmax.cc sort.cc |
|
36 // chol.cc fft2.cc ifft2.cc pinv.cc svd.cc |
|
37 // colloc.cc filter.cc inv.cc qr.cc syl.cc |
|
38 // dassl.cc find.cc log.cc quad.cc |
|
39 // det.cc fsolve.cc lsode.cc qzval.cc |
|
40 // eig.cc givens.cc lu.cc rand.cc |
|
41 // expm.cc hess.cc minmax.cc schur.cc |
2134
|
42 // |
|
43 // The difference between DEFUN_DLD and DEFUN_DLD_BUILTIN is that |
|
44 // DEFUN_DLD_BUILTIN can define a built-in function that is not |
|
45 // dynamically loaded if the operating system does not support dynamic |
|
46 // linking. To define your own dynamically linked functions you |
|
47 // should use DEFUN_DLD. |
|
48 |
|
49 #include <octave/config.h> |
|
50 |
|
51 #include <iostream.h> |
|
52 |
|
53 #include <octave/defun-dld.h> |
|
54 #include <octave/error.h> |
|
55 #include <octave/oct-obj.h> |
|
56 #include <octave/pager.h> |
|
57 #include <octave/symtab.h> |
|
58 #include <octave/variables.h> |
|
59 |
2152
|
60 // DEFUN_DLD and the macros that it depends on are defined in the |
|
61 // files defun-dld.h, defun.h, and defun-int.h. |
|
62 |
|
63 // Note that the third parameter (nargout) is not used, so it is |
|
64 // omitted from the list of arguments to DEFUN_DLD in order to avoid |
|
65 // the warning from gcc about an unused function parameter. |
2134
|
66 |
|
67 DEFUN_DLD (hello, args, , |
|
68 "[...] = hello (...)\n\ |
|
69 \n\ |
|
70 Print greeting followed by the values of all the arguments passed.\n\ |
|
71 Returns all arguments in reverse order.") |
|
72 { |
|
73 // The list of values to return. See the declaration in oct-obj.h |
|
74 |
|
75 octave_value_list retval; |
|
76 |
|
77 // This stream is normally connected to the pager. |
|
78 |
|
79 octave_stdout << "Hello, world!\n"; |
|
80 |
|
81 // The arguments to this function are available in args. |
|
82 |
|
83 int nargin = args.length (); |
|
84 |
|
85 // The octave_value_list class is a zero-based array of octave_value |
|
86 // objects. The declaration for the octave_value class is in the |
3242
|
87 // file ov.h. The print() method will send its output to |
2134
|
88 // octave_stdout, so it will also end up going through the pager. |
|
89 |
|
90 for (int i = 0; i < nargin; i++) |
2142
|
91 { |
|
92 octave_value tmp = args (i); |
3242
|
93 tmp.print (octave_stdout); |
2142
|
94 retval (nargin-i-1) = tmp; |
|
95 } |
2134
|
96 |
|
97 return retval; |
|
98 } |