Gnuplot crash when calling from application

I would like to use gnuplot to build my results in a console application (C ++, Eclipse CDT, Linux). I created a simple class to simplify the task (see code below). I am trying to build a test chart in my main:

int main() { Gnuplot plot; plot("plot sin(x)") ; cout<<"Press button:"; cin.get(); return 0; } 

My problem is that if I run my application, I get a runtime error that says "WxWidgets failed to initialize. Segmentation error (kernel reset) after executing the line graph (" plot sin (x) "). However, if I go through the lines in debug mode, the code works fine and the graph window is displayed as expected using sine. Any help would be appreciated.

 #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 

and source:

 #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 }; 
+4
source share
2 answers

Running without reference to the X server will cause this problem. Usually ssh does not give you a link to the X server (but can be configured or switched for this). I find that I can duplicate the error quoted by "ssh localhost" and enter gnuplot and the graphing command, it will assume that wxt is a terminal type and will fail to initialize the wxWidgets and segfault error.

I think this will work for me if I do it first.

Warning: the first "xhost +" command is dangerous, it disables X protection and allows anything, anywhere on the Internet, to connect to a screen, keyboard or mouse. This may be less of a problem because the machine is located behind a network address router, such as that used on a home network.

From the shell:

 xhost + export DISPLAY=:0.0 

Run gnuplot programmatically, then send the gnuplot commands as usual.
It should work. Work for me at the moment in ssh login. If this is not the case, check the env that you use to start a new process and set "DISPLAY =: 0.0" there explicitly. This means connecting to a local display. Hostname can be added before:

On Linux, gnuplot usually searches for an X server. Perhaps he will not find it.

Perhaps if the goal is to save your charts in files, add:

 set terminal png set output 'graph.png' 

to your gnuplot commands before the "plot" command. This should work even on dumb servers.

If you want to control the output file name, just send another name instead of graph.png

+2
source

The following code (in C, not C ++) works fine for me (when starting from a terminal inside some X11 session, so DISPLAY set to :0.0 ):

 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> int main(int argc, char**argv) { FILE *gp = NULL; if (!getenv("DISPLAY")) {fprintf(stderr, "no display\n"); exit(EXIT_FAILURE);}; gp = popen("gnuplot -persist", "w"); if (!gp) {perror("gnuplot popen"); exit(EXIT_FAILURE);}; //sleep (1); fprintf(gp, "plot sin(x)\n"); fflush(gp); fprintf(gp, "exit\n"); fflush(gp); //sleep (1); pclose (gp); return 0; } 

(works on Debian / Sid x86-64 with gnuplot 4.6 patchlevel 0)

I suggest that sleep -ing is practically useful to allow gnuplot to get enough time to work. And do not forget fflush after each command.

Applications:

You must have DISPLAY . If you get a no display error message, then you are launching your program in the wrong environment. In this case, no software tricks will help, since gnuplot needs some X11 server to talk to.

So you have to explain a lot more how you start your application. I assume this comes from Eclipse simply because Eclipse works with some X11 server, and without Eclipse you won’t have an available X11 server. (I cannot explain why this depends on how you run your program. If ussing ssh do not forget ssh -X and configure your ssh accordingly).

In fact, my calls to sleep useless. But checking for the existence of DISPLAY is important.

This is actually some kind of bug in gnuplot , which might fail better than DISPLAY ; I added a ticket to my bug tracker. You can reproduce the error with unset DISPLAY; echo 'plot sin(x); exit' | gnuplot -persist unset DISPLAY; echo 'plot sin(x); exit' | gnuplot -persist

+1
source

All Articles