Skip to main content

Part Six - Using gnuplot from a C++ Program

These instructions have not been fully tested yet

  1. Download this ZIP File
  2. Unzip this to C:\Cygwin\Home\UserName\gnuplot_i where UserName is your Windows User Name.
  3. Run the "Start X Windows" shortcut (created in Part Four)
  4. Type the following command to change to this directory:

    cd gnuplot_i

  5. Type the following command to list the contents of the directory:

    ls

  6. This should list the following files:
    • Makefile
    • example.cpp
    • gnuplot_i.cpp
    • gnuplot_i.hpp
  7. Type the following command to compile the example program (uses the instructions found in the Makefile):

    make

  8. Your screen should now look like this:

    Screenshot

  9. Again, type the following command to list the contents of the directory:

    ls

  10. This should list the following files:
    • Makefile
    • example.cpp
    • example.exe (the new program)
    • example.o (temporary object file)
    • gnuplot_i.cpp
    • gnuplot_i.hpp
    • gnuplos_o.o (temporary object file)
  11. Your screen should now look like this:

    Screenshot

  12. Now try running the example program we have just compiled by typing:

    ./example

    or

    ./example.exe

  13. It should print text messages in the command prompt window, and draw assorted graphs in additional gnuplot windows, e.g.

    Screenshot
    (Click here for full sized image)

  14. The example program will finish and close all the gnuplot windows on its own
  15. Close Cygwin

P.S. - Using the Windows Command Prompt
As long as the X Windows server is running (its started when you run the "Start X Windows" shortcut) you can compile and run C++ programs that use gnuplot from the Windows Command Prompt (instead of the Cygwin prompt). You can tell if the X Windows server is running by looking for its icon in the Windows System Tray (bottom right of screen):

Screenshot

Credits
The example here uses a C++ interface to gnuplot written by Rajarshi Guha (a graduate student in the department of chemistry at the Pennsylvania State University).

The code (in my ZIP file) is identical to that provided by Rajarshi Guha (in this tarball, which seems to have moved server to here) except I renamed the .cc files to .cpp, and provided it as a ZIP file because they are easier to use on Windows than tarballs.

This code was based on the C interface to gnuplot written by Nicolas Devillard.

Both interfaces talk to gnuplot using "POSIX pipes" which are supported on Unix and Linux. We can use this code under Windows XP with Cygwin to talk to the (Cgywin provided) gnuplot.

Nicolas' page also has information on ways of controlling gnuplot from other programming languages, for example gnuplot with Python, gnuplot with Perl and gnuplot with Fortran.


Next up, Part Seven - Remote Computing.