# HG changeset patch # User David Bateman # Date 1220982222 14400 # Node ID 3f15a11ec4171d73f5e7c8074d01ef901cb8817e # Parent 24c6e1e76adda8f435704f87569cb37a8ad7e3c8 Add explanationation of initializing the interpreter in a standalone program diff -r 24c6e1e76add -r 3f15a11ec417 doc/interpreter/dynamic.txi --- a/doc/interpreter/dynamic.txi Tue Sep 09 12:41:32 2008 -0400 +++ b/doc/interpreter/dynamic.txi Tue Sep 09 13:43:42 2008 -0400 @@ -1621,28 +1621,7 @@ following C++ program, uses class Matrix from liboctave.a or liboctave.so. -@example -@group -#include -#include -int -main (void) -@{ - std::cout << "Hello Octave world!\n"; - 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 << a_matrix; - return 0; -@} -@end group -@end example +@examplefile{standalone.cc} @noindent mkoctfile can then be used to build a standalone application with a @@ -1650,8 +1629,8 @@ @example @group -$ mkoctfile --link-stand-alone hello.cc -o hello -$ ./hello +$ mkoctfile --link-stand-alone standalone.cc -o standalone +$ ./standalone Hello Octave world! 11 12 21 22 @@ -1660,4 +1639,24 @@ @end example Note that the application @code{hello} will be dynamically linked -against the octave libraries and any octave support libraries. +against the octave libraries and any octave support libraries. The above +allows the Octave math libraries to be used by an application. It does +not however allow the script files, oct-files or builtin functions of +Octave to be used by the application. To do that the Octave interpreter +needs to be initialized first. An example of how to do this can then be +seen in the code + +@examplefile{embedded.cc} + +@noindent +which is compiled and run as before as a standalone application with + +@example +@group +$ mkoctfile --link-stand-alone embedded.cc -o embedded +$ ./embedded +GCD of [10, 15] is 5 +$ +@end group +@end example + diff -r 24c6e1e76add -r 3f15a11ec417 examples/Makefile.in --- a/examples/Makefile.in Tue Sep 09 12:41:32 2008 -0400 +++ b/examples/Makefile.in Tue Sep 09 13:43:42 2008 -0400 @@ -39,6 +39,7 @@ SOURCES = \ addtwomatrices.cc \ celldemo.cc \ + embedded.cc \ firstmexdemo.c \ fortdemo.cc \ fortsub.f \ @@ -62,6 +63,7 @@ oregonator.cc \ oregonator.m \ paramdemo.cc \ + standalone.cc \ stringdemo.cc \ structdemo.cc \ unwinddemo.cc diff -r 24c6e1e76add -r 3f15a11ec417 examples/embedded.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/embedded.cc Tue Sep 09 13:43:42 2008 -0400 @@ -0,0 +1,43 @@ +#include +#include +#include +#include +int +main (void) +{ + string_vector argv (2); + argv(0) = "embedded"; + argv(1) = "-q"; + + octave_main (2, argv.c_str_vec(), 1); + + octave_idx_type n = 2; + Matrix a_matrix = Matrix (1, 2); + + std::cout << "GCD of ["; + for (octave_idx_type i = 0; i < n; i++) + { + a_matrix (i) = 5 * (i + 1); + if (i != 0) + std::cout << ", " << 5 * (i + 2); + else + std::cout << 5 * (i + 2); + } + std::cout << "] is "; + + octave_value_list in = octave_value (a_matrix); + octave_value_list out = feval ("gcd", in, 1); + + if (!error_state && out.length () > 0) + { + a_matrix = out(0).matrix_value (); + if (a_matrix.numel () == 1) + std::cout << a_matrix(0) << "\n"; + else + std::cout << "invalid\n"; + } + else + std::cout << "invalid\n"; + + return 0; +} diff -r 24c6e1e76add -r 3f15a11ec417 examples/standalone.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/standalone.cc Tue Sep 09 13:43:42 2008 -0400 @@ -0,0 +1,18 @@ +#include +#include +int +main (void) +{ + std::cout << "Hello Octave world!\n"; + 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 << a_matrix; + return 0; +}