简体   繁体   English

I have written some code in C++ using Octave API and this C++ function basically calls an Octave script, but it is giving segmentation fault

[英]I have written some code in C++ using Octave API and this C++ function basically calls an Octave script, but it is giving segmentation fault

I want to write C++ code using Octave API for C++ which will use an Octave function.我想为 C++ 使用八度音程 API 编写 C++ 代码,它将使用八度音程 ZC1C425268E683894F1AB5A I am attaching my code.我附上我的代码。

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

int main()
{
        // arguments for octave
        string_vector argv (2);
        argv(0) = "embedded";
        argv(1) = "-q"; // quiet

        // start octave, run embedded (third parameter == true)
        octave_main (2, argv.c_str_vec (), true);

        // read the script file
        source_file("calc_and_plot.m");

        // call the function with an argument
        octave_value_list in;
        in(0) = "Hello, world.";
        feval("calc_and_plot", in);

        std::cout << "octave (child process) done\n";
        clean_up_and_exit(0); // quit octave. This also quits the program,
                              // so use this together with atexit, if you 
                              // need to do something else after octave exits
    
    return 0;
}

My Octave script file is:我的 Octave 脚本文件是:

function calc_and_plot(str)
    printf('%s\n', str);
    x = linspace(0, 2*pi, 100);
    y = sin(x);
    it = plot(y);
    waitfor(it);
end

I am getting Segmentation fault (core dumped) .我收到分段错误(核心转储)
I execute the code using the terminal by running " mkoctfile --link-stand-alone file_name.cpp -L/usr/lib/octave-4.2.2 -I/usr/include/octave-4.0.2 -loctave -loctinterp -o file_name &&./file_name ".我通过运行“ mkoctfile --link-stand-alone file_name.cpp -L/usr/lib/octave-4.2.2 -I/usr/include/octave-4.0.2 -loctave -loctinterp - 使用终端执行代码 - o 文件名 &&./文件名“。 I am using Octave 4.2.2 and it is installed in Ubuntu 18.04.我使用的是 Octave 4.2.2,它安装在 Ubuntu 18.04 中。 Please help me this this matter.请帮我解决这个问题。

I don't know exactly what's wrong with your code, but you seem to be mixing styles and libraries from different versions of octave.我不知道您的代码到底有什么问题,但您似乎正在混合 styles 和来自不同八度音程版本的库。

Eg toplev.h is deprecated in newer versions of octave, and has been replaced by interpreter.h.例如,toplev.h 在 octave 的较新版本中已被弃用,并已被解释器.h 取代。 Yet you seem to be trying to include both headers...然而,你似乎试图同时包含两个标题......

Equally, I think octave_main is no longer used (but maybe it was back in 4.2.2?)同样,我认为 octave_main 不再使用(但也许它回到了 4.2.2?)

Also from -L/usr/lib/octave-4.2.2 -I/usr/include/octave-4.0.2 I can see you're trying mix octave v4.2.2 libraries with v4.0.2 headers, etc... no wonder it's segfaulting!同样来自-L/usr/lib/octave-4.2.2 -I/usr/include/octave-4.0.2我可以看到您正在尝试将 octave v4.2.2 库与 v4.0.2 标头等混合...不想知道它是段错误的!


Why are you even using such an old version of octave?你为什么还要使用这么旧的八度音阶? The latest is 6.2.0.最新的是 6.2.0。 Do you have a particular reason for needing to use an older version?您是否有特殊原因需要使用旧版本?

If not, my advice would be to download and install the latest octave from source, and follow the instructions on how to create a standalone program from the manual here: https://octave.org/doc/v6.2.0/Standalone-Programs.html如果没有,我的建议是从源代码下载并安装最新的 octave,并按照此处手册中有关如何创建独立程序的说明进行操作: https://octave.org/doc/v6.2.0/Standalone-Programs .html

If you require v4.2.2 specifically, then download and install that from source, and try to follow the instructions from the manual for that version specifically, eg: https://octave.org/doc/v4.2.2/Standalone-Programs.html如果您特别需要 v4.2.2,请从源代码下载并安装,并尝试按照版本手册中的说明进行操作,例如: https://octave.org/doc/v4.2.2/Standalone-Programs。 html

PS: note the changes in the manual between versions 4.02, 4.22, and 6.20... eg 4.02 uses toplev, but 4.22 does not. PS:请注意手册中 4.02、4.22 和 6.20 版本之间的更改...例如 4.02 使用 toplev,但 4.22 没有。 4,22 uses octave_main. 4,22 使用 octave_main。 but 6.20 does not...但是6.20不...

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

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