我想在控制台应用程序(C ++,Eclipse CDT,Linux)中使用gnuplot绘制我的结果。 我创建了一个简单的类来使事情更容易(请参见下面的代码)。 我尝试在我的主函数中绘制一个测试图:
int main() {
Gnuplot plot;
plot("plot sin(x)") ;
cout<<"Press button:";
cin.get();
return 0;
}
我的问题是,如果我正常启动应用程序,执行线条plot("plot sin(x)")后,我会收到一个运行时错误消息,“无法初始化wxWidgets。分段错误(core dumped)”。但是,如果我在调试模式下逐行执行代码,则代码可以正常工作,并且我的绘图窗口按预期显示正弦波。欢迎任何帮助。
#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
};
g++ -Wall -g
进行编译,并学会使用gdb
调试器。 - Basile Starynkevitchpopen
后面加上sleep(1)
会有所帮助... 显然,gnuplot
正在使用 WxWidgets,并且对您来说正在崩溃... - Basile StarynkevitchDISPLAY
吗? - Basile Starynkevitch