简体   繁体   English

从C ++调用带有2个参数的Octave内置函数

[英]Calling Octave built-in function taking 2 arguments from C++

I am getting a segfault when running the following code. 运行以下代码时出现段错误。 Calling an Octave built-in function taking 1 argument works fine, eg Fsize. 调用带有1个参数的Octave内置函数可以正常工作,例如Fsize。 But with any of the 2 argument ones I get a segfault. 但是使用这两个参数中的任何一个,我都会遇到段错误。 I have tried Frdivide and Fplus. 我已经尝试过Frdivide和Fplus。

#include <octave/oct.h>
#include <octave/builtin-defun-decls.h>

int main() {

  octave_value_list args;
  octave_value_list res;

  Matrix l(3,1,1.0);
  Matrix r(3,1,1.0);

  args(0) = l;
  args(1) = r;

  res = Fplus(args); // BOOM !!!

} }

Seems one has to initialize the interpreter, even thought the Octave documentation indicates that this is not necessary for built-in Octave functions. 似乎必须初始化解释器,甚至认为Octave文档指出对于内置的Octave函数而言,这不是必需的。 The following code works: 以下代码有效:

#include <octave/oct.h>
#include <octave/octave.h>
#include <octave/builtin-defun-decls.h>
#include <octave/toplev.h>

int main() {

  string_vector argv(2);
  argv(0) = "embedded";
  argv(1) = "-q";

  octave_main(2, argv.c_str_vec(), 1);

  octave_value_list args;
  octave_value_list res;

  Matrix l(3,1,1.0);
  Matrix r(3,1,1.0);

  args(0) = l;
  args(1) = r;

  res = Fplus(args);

  std::cout << res(0).matrix_value();

  clean_up_and_exit(0);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM