简体   繁体   English

尝试从 Octave 的独立程序示例中编译示例代码,在第一行出现段错误

[英]Trying to compile example code from Octave's Standalone Programs example, getting segfault on first line

I am trying to learn how to embed Octave in my C++ code.我正在尝试学习如何在我的 C++ 代码中嵌入 Octave。 When running the second example from here , the code compiles fine, but when running the code, a segmentation fault appears in the first line, when trying to initialize the interpreter.这里运行第二个示例时,代码编译良好,但在运行代码时,尝试初始化解释器时,第一行出现分段错误。 I'm not extremely adept at C++ but even when looking it up I can't find any answers.我不是非常擅长 C++,但即使在查找它时我也找不到任何答案。

The original code had octave::feval instead of feval, that threw a different, namespace error, so I just got rid of that and added the parse.h in the includes.原始代码有octave::feval 而不是feval,这引发了一个不同的命名空间错误,所以我只是摆脱了它并在包含中添加了parse.h。 I doubt this is at all related to the issue but that is a modification I did do.我怀疑这与该问题完全相关,但这是我所做的修改。

#include <iostream>
#include <octave/oct.h>
#include <octave/octave.h>
#include <octave/parse.h>
#include <octave/interpreter.h>

int
main (void)
{
  // Create interpreter.

  octave::interpreter interpreter;

  try
    {
      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 = 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;
}

The actual output is supposed to be:实际输出应该是:

GCD of [10, 15] is 5

I am using Linux Ubuntu 18.04 with Octave 4.2.2我使用的是带有 Octave 4.2.2 的 Linux Ubuntu 18.04

The documentation looked at is a different version than the version I have installed on my computer.查看的文档版本与我在计算机上安装的版本不同。 I have 4.2, but I was looking at 4.4 docs, which has different code for the task I was trying to accomplish.我有 4.2,但我正在查看 4.4 文档,它有不同的代码用于我试图完成的任务。

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

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