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 |
|
4 // linking. To find out if your version of Octave supports dynamic |
|
5 // linking, type the command |
|
6 // |
|
7 // octave_config_info |
|
8 // |
|
9 // at the Octave prompt. Support for dynamic linking is included if |
|
10 // the output contains either of the lines |
|
11 // |
|
12 // Dynamic Linking (dlopen/dlsym): yes |
|
13 // Dynamic Linking (shl_load/shl_findsym): yes |
|
14 |
|
15 // To compile this file, type the command |
|
16 // |
2137
|
17 // mkoctfile hello.cc |
2134
|
18 // |
|
19 // at the shell prompt. The script mkoctfile should have been |
|
20 // installed along with Octave. Running it will create a file called |
|
21 // hello.oct that can be loaded by Octave. To test the hello.oct |
|
22 // file, start Octave and type the command |
|
23 // |
|
24 // hello ("easy as", 1, 2, 3) |
|
25 // |
|
26 // at the Octave prompt. Octave should respond by printing |
|
27 // |
|
28 // Hello, world! |
|
29 // easy as |
|
30 // 1 |
|
31 // 2 |
|
32 // 3 |
|
33 // ans = 3 |
|
34 |
|
35 // Additional examples are available in the files in the src directory |
|
36 // of the Octave distribution that use the macro DEFUN_DLD_BUILTIN. |
|
37 // Currently, this includes the files |
|
38 // |
|
39 // balance.cc fft.cc hess.cc lu.cc schur.cc |
|
40 // chol.cc fft2.cc ifft.cc minmax.cc sort.cc |
|
41 // colloc.cc filter.cc ifft2.cc pinv.cc svd.cc |
|
42 // dassl.cc find.cc inv.cc qr.cc syl.cc |
|
43 // det.cc fsolve.cc log.cc quad.cc |
|
44 // eig.cc fsqp.cc lsode.cc qzval.cc |
|
45 // expm.cc givens.cc lu.cc rand.cc |
|
46 // |
|
47 // The difference between DEFUN_DLD and DEFUN_DLD_BUILTIN is that |
|
48 // DEFUN_DLD_BUILTIN can define a built-in function that is not |
|
49 // dynamically loaded if the operating system does not support dynamic |
|
50 // linking. To define your own dynamically linked functions you |
|
51 // should use DEFUN_DLD. |
|
52 |
|
53 #include <octave/config.h> |
|
54 |
|
55 #include <iostream.h> |
|
56 |
|
57 #include <octave/defun-dld.h> |
|
58 #include <octave/error.h> |
|
59 #include <octave/oct-obj.h> |
|
60 #include <octave/pager.h> |
|
61 #include <octave/symtab.h> |
|
62 #include <octave/variables.h> |
|
63 |
|
64 // Note, nargout is not used, so it is omitted from the argument list |
|
65 // to avoid the warning from gcc about an unused function parameter. |
|
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 |
|
87 // file pt-const.h. The print() method will send its output to |
|
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); |
|
93 tmp.print (); |
|
94 retval (nargin-i-1) = tmp; |
|
95 } |
2134
|
96 |
|
97 return retval; |
|
98 } |