Mercurial > hg > octave-nkf
changeset 17949:3341d2f1e5db
Document calling DEFUN functions in C++.
* examples/standalonebuiltin.cc: new file.
* doc/interpreter/external.txi: add a paragraph about calling DEFUN
functions in C++.
author | Carlo de Falco <cdf@users.sourceforge.net> |
---|---|
date | Mon, 18 Nov 2013 09:23:15 +0100 |
parents | 73f46593a51c |
children | e036b96133ec |
files | doc/interpreter/external.txi examples/standalonebuiltin.cc |
diffstat | 2 files changed, 67 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/doc/interpreter/external.txi +++ b/doc/interpreter/external.txi @@ -1773,3 +1773,37 @@ @end group @end example +It is worth noting that, if only builtin funcions are to be calle from +a C++ standalone program, then it does not need to initialize the +interpreter to do so. The general rule is that, for a builtin +function named @code{function_name} in the interpreter, there will be +a C++ function named @code{Ffunction_name} (note the prepended capital +@code{F}) accessible in the C++ API. The declarations for all builtin +functions are collected in the header file @code{builtin-defun-decls.h}. +This feature should be used with care as the list of built-in functions can change. +No guarantees can be made that a function that is currently built in won't be implemented +as a .m file or as a dynamically linked function in the future. +An example of how to call builtin functions from C++ can be seen in the code + +@example +@EXAMPLEFILE(standalonebuiltin.cc) +@end example + +@noindent +which, again, is compiled and run as a standalone application with + +@example +@group +$ mkoctfile --link-stand-alone standalonebuiltin.cc -o standalonebuiltin +$ ./standalonebuiltin +This is a matrix: + 11 12 + 21 22 + +This is the norm of the matrix: +34.4952 + +@end group +@end example + +
new file mode 100644 --- /dev/null +++ b/examples/standalonebuiltin.cc @@ -0,0 +1,33 @@ +#include <iostream> +#include <octave/oct.h> +#include <octave/builtin-defun-decls.h> + +int +main (void) +{ + + int n = 2; + Matrix a_matrix = Matrix (n, n); + + for (octave_idx_type i = 0; i < n; i++) + for (octave_idx_type j = 0; j < n; j++) + a_matrix(i,j) = (i + 1) * 10 + (j + 1); + + std::cout << "This is a matrix:" + << std::endl + << a_matrix + << std::endl; + + octave_value_list in; + in(0) = a_matrix; + + octave_value_list out = Fnorm (in, 1); + double norm_of_the_matrix = out(0).double_value (); + + std::cout << "This is the norm of the matrix:" + << std::endl + << norm_of_the_matrix + << std::endl; + + return 0; +}