Octave 自身使用的库也可以被用于独立应用程序中。这些应用程序可以访问数组和矩阵类,以及所有 Octave 算法。以下 C++ 程序使用了来自 liboctave.a 或 liboctave.so 的 Matrix 类。
#include <iostream>
#include <octave/oct.h>
int
main ()
{
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;
}
可以使用 mkoctfile 通过以下命令构建一个独立的应用程序:
$ mkoctfile --link-stand-alone standalone.cc -o standalone $ ./standalone Hello Octave world! 11 12 21 22 $
请注意,应用程序 standalone 将动态链接到 Octave 库以及所有 Octave 支持库。上述方法允许应用程序使用 Octave 数学库,但并不允许应用程序使用 Octave 的脚本文件、oct 文件或内置函数。为此,需要首先初始化 Octave 解释器。以下代码展示了如何实现这一点:
#include <iostream>
#include <octave/oct.h>
#include <octave/octave.h>
#include <octave/parse.h>
#include <octave/interpreter.h>
int
main ()
{
// Create interpreter.
octave::interpreter interpreter;
try
{
// Inhibit reading history file by calling
//
// interpreter.initialize_history (false);
// Set custom load path here if you wish by calling
//
// interpreter.initialize_load_path (false);
// Perform final initialization of interpreter, including
// executing commands from startup files by calling
//
// interpreter.initialize ();
//
// if (! interpreter.is_initialized ())
// {
// std::cerr << "Octave interpreter initialization failed!"
// << std::endl;
// exit (1);
// }
//
// You may skip this step if you don't need to do anything
// between reading the startup files and telling the interpreter
// that you are ready to execute commands.
// Tell the interpreter that we're ready to execute commands:
int status = interpreter.execute ();
if (status != 0)
{
std::cerr << "creating embedded Octave interpreter failed!"
<< std::endl;
return status;
}
octave_idx_type n = 2;
octave_value_list in;
for (octave_idx_type i = 0; i < n; i++)
in(i) = octave_value (5 * (i + 2));
octave_value_list out = octave::feval ("gcd", in, 1);
if (out.length () > 0)
std::cout << "GCD of ["
<< in(0).int_value ()
<< ", "
<< in(1).int_value ()
<< "] is " << out(0).int_value ()
<< std::endl;
else
std::cout << "invalid\n";
}
catch (const octave::exit_exception& ex)
{
std::cerr << "Octave interpreter exited with status = "
<< ex.exit_status () << std::endl;
}
catch (const octave::execution_exception&)
{
std::cerr << "error encountered in Octave evaluator!" << std::endl;
}
return 0;
}
按照之前的方式,将其编译并运行为独立应用程序:
$ mkoctfile --link-stand-alone embedded.cc -o embedded $ ./embedded GCD of [10, 15] is 5 $
值得再次强调的是:如果仅需从 C++ 独立程序中调用内置函数,则无需初始化解释器。一般规则是,对于解释器中名为 function_name 的内置函数,在 C++ API 中将有一个对应的名为 Ffunction_name(注意前面的大写字母 F)的 C++ 函数。所有内置函数的声明都收集在头文件 builtin-defun-decls.h 中。使用此特性时需要谨慎,因为内置函数列表可能会发生变化。无法保证当前是内置函数的函数将来不会以 .m 文件或动态链接函数的形式实现。以下代码展示了如何从 C++ 调用内置函数:
#include <iostream>
#include <octave/oct.h>
#include <octave/builtin-defun-decls.h>
int
main ()
{
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 = octave::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;
}
将其编译并运行为独立应用程序:
$ 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 $
版权所有 © 2024-2026 Octave中文网
ICP备案/许可证号:黑ICP备2024030411号-2