简体   繁体   中英

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. But with any of the 2 argument ones I get a segfault. I have tried Frdivide and 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. 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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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