How to configure gnuplot-iostream with C++? - c++

I'm a complete noob to gnuplot and linux in general. I need to plot scientific graphs for my project for which I will be using C++. After looking for various plotting options available, I've decided to use gnuplot for plotting due to its features and quality of graphs. So I downloaded gnuplot as a program and could plot the graphs using .dat files, however I need to plot the graphs within C++ without explicitly launching gnuplot. Is it possible to plot dynamic graphs using gnuplot? I would also like to plot the solution as is it computed for every time step!
I came to know that gnuplot-iostream interface makes this possible. However I did not understand how to install this library for C++ at all. I do not understand Git, or anything posted on the website to be able to configure that library. Can anybody point me to the tutorial/how to document for the same? I have Ubuntu 12.04 and also Windows 8.1.
Is it possible to configure this library with an IDE (I'm using code::blocks), if yes how that can be done?

First of all gnuplot-iostream relies on the Boost library, it is a very common library, but it doesn't come together with the C++ compiler, so make sure it is properly installed.
Obviously it also needs gnuplot: if it is properly installed you should be able to launch it from the terminal.
Then paste this minimal example in a file main.cpp:
#include <vector>
#include <utility>
#include "gnuplot-iostream.h"
int main() {
std::vector<std::pair<double,double>> data;
data.emplace_back(-2,-0.8);
data.emplace_back(-1,-0.4);
data.emplace_back(0,-0);
data.emplace_back(1,0.4);
data.emplace_back(1,0.8);
Gnuplot gp;
gp << "plot [-5:5] sin(x) tit 'sin(x)', '-' tit 'data'\n";
gp.send1d(data);
return 0;
}
Save the header gnuplot-iostream.h in the same folder and compile with:
g++ -std=c++11 main.cpp -o main -lboost_iostreams -lboost_system -lboost_filesystem
When running ./main you should get a plot of the sine function and of the few dots.

recently I was using Gnuplot to visualize data from an iterative solver. To run Gnuplot in "pseudo" real-time I did the following steps:
establish a pipeline from C++ to Gnuplot:
FILE *GnuPipe = popen("...\bin\pgnuplot -persist","w");
start the solver (or data source) and write to a file e.g. 'data.txt'
start a script which tells Gnuplot to replot the data.txt as long as flag is NOT set. In this case I created a text file 'flag.txt' and wrote in a=0, which serves as flag. The script for Gnuplot could look like:
load 'flag.txt'
plot 'data.txt' u 1:2 with lines
pause 0.1
if (a==0) reread
if the solver converges, or if there is no further data to plot, set a=1 in the 'flag.txt'
Gnuplot is loading the 'flag.txt' and sees that the flag is set and stops rereading.

Related

How to have GNUPlot open several Plots windows in Cascade Format

I have a C++ console program that calls GNUPlot for plotting data. It was working like a dream with numerous plot windows being plotted "cascade" style but now for no apparent reason when I run the console program GNUPlot windows are opened directly superimposed on each other so I have to manually separate them out. Is this a WIN 10 issue or a GNUPlot issue or even a VS2019 Console issue? I have not changed any of the GNUPlot settings (knowingly) and can see no way within GNUPlot 5.2 patchlevel 7 to restore the cascading. The only stuff I can find for WIN 10 is a cascade function on the Task Bar but this only refers to windows already open and is no help.
Hooray! Thanks a million heap! I found that deleting the gnuplot.ini file restored the cascading of windows. Then I found that restoring the gnuplot.ini file but commenting out the offending line cured the problem: thus #GraphOrigin=36 198. Interestingly, the gnuplot_history file in the same folder recorded "cascadingcascadingcascading."

Basic ping pong style C++ program, can't seem to get it to compile and run

To start off I must say I am an absolute n00b. I just started with C++ and I am trying to figure it out. To issue I am having is I found this source code on google for Ping Pong and I've saved it as "pong.cpp" from sublime text edit to my Desktop. I am under the impression that all C++ programs have to be run through the terminal? but I am not sure, again beginner problems. So in terminal I direct it to my desktop in from there I type in g++ pong.cpp. Then it says this:
pong.cpp:2:10: fatal error: 'allegro.h' file not found
#include <allegro.h>
1 error generated.
I am thinking this is because I found it on google and that I need a file to back up the processing on the program which is unavailable.
Does anyone know where I could get a proper Ping Pong C++ code that would suite what I need here? I just want to be able to run a basic pingpong style game through my terminal to get more familiarized with C++
You have to tell gcc where allegro.h is installed. Run this command in terminal and post the output.
find / -name 'allegro.h' 2>/dev/null

No such file or directory "ruby/config.h" when trying to compile C++ into Ruby using SWIG

I'm trying to get a basic example running using SWIG to convert a C++ file into Ruby. I have Ruby 2.0.0p451 (64 bit version) installed and I've also installed the 64-bit DevKit. I'm running Windows 7 and trying to use swigwin-2.0.12. Finally, I am using the GCC C++ compiler supplied by Mingw-builds for the 64-bit version of Windows.
I have the basic C++ hello world program as shown below.
#include <iostream>
#include <cstdlib>
using namespace std;
main()
{
cout << "Hello World!";
system("pause");
return 0;
}
At the command prompt, I use the command:
swig -module -c++ -ruby hello_world.cpp
This completes fine and produces a file titled hello_world_wrap.cxx. However, when I receive an error when I try to compile the .cxx file using the command:
g++ -fPIC -c hello_world_wrap.cxx -IC:\Ruby200-x64\include\ruby-2.0.0
The error I am receiving is:
All the research I've done has pointed me to an installation of the incorrect DevKit, but I don't think this is my issue. I've made sure to download the 64-bit version of Ruby and the DevKit. I've checked the folder specified in the error, and there is no config.h file. I'm not sure why the config.h file does not exist or why ruby.h is trying to load it.
Any thoughts?
Check that C:\Ruby200-x64\include\ruby-2.0.0\ruby\config.h exists. If not, find it and fix the path.
Update:
Check if there is a ruby.h in the same folder. If there is then just use -IC:\Ruby200-x64\include\ruby-2.0.0\ruby\x64-mingw instead. Otherwise try adding a second -I, for this extra path. I agree with you this is a little strange (not so much the former, but definitely if you have to have two -I). The script at https://groups.google.com/forum/m/#!topic/comp.lang.ruby/RpjuvXpFI30 suggests that this might be normal, I.e. you need one -I for platform-independent headers and one for platform-dependent.

Why Module['canvas'] is undefined after emcc?

I have a simple C program that draws triangle using opengl. I tried to port it to JS using emscripten:
./emcc func.c
it generates a file a.out.js in the emscripten dir. I tried to run it from the terminal using
node a.out.js
but had an error document is undefined. So I created html with included a.out.js, ran in the Chrome and got an error Can't execute addEventListener of undefined. It was about Module['canvas'] object. I took a look at the a.out.js and didn't find an assignment to Module['canvas'].
Now I got an answer, so I'll share it here for easier search.
To use any graphics you should specify that output will be html, not just js.
./emcc func.c -o func.html
Also, this article could be helpful if you just started to use emscripten

What is a 'shebang' line?

Currently I'm trying to start programming on my new Mac. I installed TextWrangler, and chose C++ as my language of choice; since I have some prior knowledge of it, from when I used Windows.
So, I wrote the ever so common "Hello World" program. Although, when I tried to run it, I got an error:
"This file doesn’t appear to contain a valid ‘shebang’ line (application error code: 13304)"
I tried searching the error code to find out how to fix this, but I couldn't find anything.. I have no idea what a 'shebang' line is... Can someone help me out?
You need to compile it with a compiler first. I assume you tried to run the source file like ./source but C++ doesn't work this way.
With some compilers however, you can provide a shebang-line as the first line of the source file (the #! is known as shebang or crunchbang, hence the name), like so:
#!/path/to/compiler
So that the shell knows what application is used to run that sort of file, and when you attempt to run the source file by itself, the compiler will compile and run it for you. That's a compiler-dependent feature though, so I recommend just plain compiling with G++ or whatever Macs use to get an executable, then run that.
While I wouldn't recommend it for regular C++ development, I'm using a simple shell script wrapper for small C++ utilities. Here is a Hello World example:
#if 0 // -- build and run wrapper script for C++ ------------------------------
TMP=$(mktemp -d)
c++ -o ${TMP}/a.out ${0} && ${TMP}/a.out ${#:1} ; RV=${?}
rm -rf ${TMP}
exit ${RV}
#endif // ----------------------------------------------------------------------
#include <iostream>
int main(int argc, char *argv[])
{
std::cout << "Hello world" << std::endl;
return 0;
}
It does appear that you are trying to run the source file directly, however you will need to compile using a C++ compiler, such as that included in the gcc (GNU Compiler Collection) which contains the C++ compiler g++ for the Mac. It is not included with the Mac, you have to download it first:
from http://www.tech-recipes.com/rx/726/mac-os-x-install-gcc-compiler/ : "To install the gcc compiler, download the xcode package from http://connect.apple.com/. You’ll need to register for an Apple Developer Connection account. Once you’ve registered, login and click Download Software and then Developer Tools. Find the Download link next to Xcode Tools (version) – CD Image and click it!"
Once it's installed, if you are going for a quick Hello World, then, from a terminal window in the directory of your source file, you can execute the command g++ HelloWorld.cpp -o HelloWorld. Then you should be able to run it as ./HelloWorld.
Also, if you're coming from a Visual Studio world, you might want to give Mono and MonoDevelop a try. Mono is a free implementation of C# (and other languages), and MonoDevelop is an IDE which is very similar to Visual Studio. MonoDevelop supports C# and other .NET languages, including Visual Basic .NET, as well as C/C++ development. I have not used it extensively, but it does seem to be very similar to VS, so you won't have to learn new everything all in a day. I also have used KDevelop, which I liked a lot while I was using it, although that's been a while now. It has a lot of support for GNU-style development in C/C++, and was very powerful as I recall.
Good luck with your endeavors!
Links:
Mono: http://mono-project.com/Main_Page
MonoDevelop: http://monodevelop.com/
KDevelop: http://kdevelop.org/
shebang is http://en.wikipedia.org/wiki/Shebang_%28Unix%29.
not sure why your program is not running. you will need to compile and link to make an executable.
What I find confusing (/interesting) is C++ program giving "Shebang line" error. Shebang line is a way for the Unix like operating system to specify which program should be used to interpret the rest of the file. The shebang line usually points to the path of the interpreter. C++ is a compiled language and does not have interpreter for it.
To get the real technical details of how shebang lines work, do a man execve and get that man page online here - man execve.
If you're on a mac then doing something like this on the commandline:
g++ -o program program.cpp
Will compile and link your program into an executable called program. Then you can run it like:
./program
The reason you got the 'shebang' error is probably because you tried to run the cpp file like:
./program.cpp
And the shell tries to find an interpreter to run the code in the file. Because this is C++ there is no relevant interpreter but if your file contains Python or Bash then having a line like this
#!/usr/bin/python
at the 1st line in your source file will tell the shell to use the python interpreter
The lines that start with a pattern like this: #!/.../.../.. is called a shebang line. In other words, a shebang is the character sequence consisting of the characters number sign and exclamation mark (#!).In Unix-like operating systems, when a text file with a shebang is used as if it is an executable, the program loader mechanism parses the rest of the file's initial line as an interpreter directive. The loader executes the specified interpreter program, passing to it as an argument the path that was initially used when attempting to run the script, so that the program may use the file as input data.