gpt4 book ai didi

c++ - 从应用程序调用时 Gnuplot 崩溃

转载 作者:IT王子 更新时间:2023-10-29 01:27:13 26 4
gpt4 key购买 nike

我想使用 gnuplot 在控制台应用程序(C++、Eclipse CDT、Linux)中绘制我的结果。我创建了一个简单的类来使事情变得更容易(见下面的代码)。我尝试在我的 main 中绘制一个测试图:

int main() {

Gnuplot plot;

plot("plot sin(x)") ;

cout<<"Press button:";
cin.get();

return 0;
}

我的问题是,如果我正常启动我的应用程序,我会收到一条运行时错误消息“无法初始化 wxWidgets。执行线图(“plot sin(x)”)后出现段错误(核心已转储)。但是,如果我在 Debug模式下单步执行代码,代码可以正常工作,并且我的绘图窗口会按预期显示正弦曲线。欢迎任何帮助。

#ifndef GNUPLOT_H_
#define GNUPLOT_H_

#include <string>
using namespace std;

class Gnuplot {

public:
Gnuplot() ;
~Gnuplot();
void operator ()(const string & command); // send any command to gnuplot

protected:
FILE *gnuplotpipe;
};
#endif

和来源:

#include "gnuplot.h"
#include <iostream>
#include <string>
#include "stdio.h"

Gnuplot::Gnuplot() {

gnuplotpipe=popen("gnuplot -persist","w");
if (!gnuplotpipe) {
cerr<< ("Gnuplot not found !");
}
}

Gnuplot::~Gnuplot() {

fprintf(gnuplotpipe,"exit\n");
pclose(gnuplotpipe);
}

void Gnuplot::operator()(const string & command) {

fprintf(gnuplotpipe,"%s\n",command.c_str());
fflush(gnuplotpipe);// flush is neccessary, nothing gets plotted else
};

最佳答案

在没有链接到 X 服务器的情况下执行将导致此问题。通常 ssh 不会为您提供到 X 服务器的链接(但可以配置或切换为这样做)。我发现我可以复制“ssh localhost”引用的错误并输入 gnuplot 和绘图命令,它会假定 wxt 是终端类型并给出无法初始化 wxWidgets 错误和段错误。

不过,如果我先这样做,我发现它对我有用。

警告:第一个命令“xhost +”是危险的,它会禁用 X 安全并允许互联网上任何地方的任何东西连接到您的屏幕、键盘或鼠标。这可能不太重要如果机器位于网络地址转换路由器后面,例如家庭网络中使用的路由器,则会出现问题。

从外壳:

xhost +
export DISPLAY=:0.0

以编程方式启动 gnuplot,然后正常发送 gnuplot 命令。
应该管用。目前在 ssh 登录中为我工作。如果没有,请检查您用来启动新进程的环境,并明确地将“DISPLAY=:0.0”放在那里。这意味着连接到本地显示器。可以在 :

之前添加主机名

在 Linux 下,gnuplot 通常会寻找 X 服务器。它可能找不到它。

如果目标是将图表保存在文件中,那么添加:

set terminal png
set output 'graph.png'

在“plot”命令之前的 gnuplot 命令。这甚至可以在 headless 服务器上工作。

如果您想控制输出文件名,只需发送其他名称而不是 graph.png

关于c++ - 从应用程序调用时 Gnuplot 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16129842/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com