我正在尝试学习如何在我的C++代码中嵌入 Octave 。从here运行第二个示例时,代码可以正常编译,但是在运行代码时,尝试初始化解释器时,第一行出现分段错误。我不是非常擅长C++,但是即使查找它,我也找不到任何答案。
原始代码使用octave::feval而不是feval,这引发了一个不同的 namespace 错误,因此我摆脱了该错误并在include中添加了parse.h。我怀疑这完全与问题有关,但这是我所做的修改。
#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;
}
实际的输出应该是:
GCD of [10, 15] is 5
我正在将Linux Ubuntu 18.04与Octave 4.2.2结合使用
最佳答案
查看的文档是与我在计算机上安装的版本不同的版本。我有4.2,但是我在看4.4文档,该文档针对我要完成的任务具有不同的代码。