I am so fond of plots and oscilloscope traces, and one day I
it would be nice to have plots while debugging my small C programs. I
use Xcode and it has great tools like Shark and Instruments, but these
are performance tools and can not show user data as far as I know. So I
started googling. A solution was found
pretty soon: gnuplot can do it. But to get that really working took
quite a couple of steps, being described in the following.
Gnuplot is a command line tool for plotting functions
and discrete data. Functions or data can be entered directly from the
command line, and gnuplot can read data from files. Further, it can
data and commands from other programs via the popen() 'pipe fitting'
function. GNU Octave, a free alternative for Matlab, uses
gnuplot as it's interface this way. Fortunately, there is a collection
of functions to do the pipe fitting from C to gnuplot, under the name
gnuplot_i. Below is a description of how to get these things working
together on OSX.
Gnuplot sourcefiles can be downloaded from
http://www.gnuplot.info/. At the
moment of this writing, there was no
reference to a binary version for OSX. But I already had gnuplot on my
computer and for sure I did not compile it. How did it get here? With
GNU Octave probably. Download GNU Octave, from
In the extras folder of the
mounted octave.dmg, you will find gnuplot.dmg. When you mount this one,
you will find a gnuplot binary, ready to install.
That is very convenient. For gnuplot to work, you need either X11 or
AquaTerm as graphics terminal. X11 is (an optional) part of the OSX
installation. AquaTerm can be downloaded free of charge, from http://aquaterm.sourceforge.net/.
I have this app installed.
Starting gnuplot opens a terminal window:
The gnuplot app had it's preference already set to AquaTerm. Enter
something like 'plot sin(x)' and AquaTerm opens swiftly with a plot:
Now comes the question of how to pipe commands and data from your
own C programs into gnuplot. I really know nothing about that, but
other people have found solutions and made these available.
A C library source titled gnuplot_i is published by N. Devillard, at
The download contains gnuplot_i.c,
gnuplot_i.h, and three example C files to demonstrate the interface.
There is also a makefile, plus instructions how to compile from the
command line without the makefile.
Here is where I got stuck for a while. Compiling gnuplot_i.c from
the command line, following the instructions in the readme, gives
gnuplot_i.o, as it should. But mixing Xcode and command line gcc is not
a good idea. Xcode will not include functions from an .o file. I decide
to build gnuplot_i.c as a static library, even though it is only one
file. This can be done using a BSD Static Library
The output is libgnuplot_i.a, if the project were named gnuplot_i.
It can be named differently as well.
Now the gnuplot_i example files can be
compiled and linked to libgnuplot_i.a. The examples should be built as
command line tools and Xcode has a template for that as well, the
Add the example.c file, the .a library file and the header file to
the project, using the menu 'project > add to project'.
The library appears with the usual suitcase icon. It does not matter if it has a name different from libgnuplot_i.a, because it is not referenced by name in the header file. If only the path is set correctly. Even with the path set correctly, this may sometimes need confirmation before Xcode can find the added files. Quite obscure and annoying.
The example files were flawlessly compiled and linked into an
executable. However, starting up this executable resulted in an
Why? The path to gnuplot could not be found, and a NULL pointer is
returned. I was not the first one to
have this problem. On
has described his solution. The essence is: create a .plist file with
path, that can
be found systemwide and not only from the terminal app. This requires a
couple of steps.
First you need to create a folder of the name .MacOSX in your home
directory. This is an invisible folder, how can you create and see
these? By default you can't. On my computer I always have them visible.
That can comfortably be done with TinkerTool. There is also a cool
dashboard widget by Matthew Hansen and Trevor Sayre that can switch
from invisible to visible and vice versa with a simple mouseclick. Or
it can be done from the terminal with the command: 'defaults write
com.apple.finder AppleShowAllFiles Yes'.
Then, in this .MacOSX folder, create a property list with the name
environment.plist. Xcode has a property list editor.
Create the following:
The paths are: /usr/bin:/bin:/usr/sbin:/usr/local/bin. These are
places where executables can reside. With hidden files and folders
invisible these usr directories are not accessible via the Finder, by
And does it work now? No, not yet. On my computer, gnuplot lives as
an application in the applications folder, so there is no gnuplot
executable in one of the regular bin directories. Fortunately, the app
is a bundle
which can be opened to show it's contents. The executables are in
To be honest, I did not perceive that gnuplot-4.2.3 is the
executable and gnuplot is the shortcut file. I just copied both into
/usr/local/bin. The administrator password is required to put or remove
files there. I started the gnuplot_i example command once more,
and.... wow! One after the other,
functions were automatically plotted in AquaTerm windows, driven by the
the example file. Phew, result at last, and a great result it is.
The real hard work of course is
done by the people who made gnuplot and gnuplot_i, free for everyone to
use. Gnuplot_i is well documented, and very user-friendly. With an
absolute minimum of extra coding you can have your data and commands
There remains one issue to be resolved, regarding the configuration.
Gnuplot cannot be opened any more from the Applications folder, now
that the path is set to the bin directories and a copy of gnuplot
resides there. It is possible to call the gnuplot copy from the command
line. But the help file and man page cannot be retrieved. The
directories were these files are expected to reside do not even exist
on my computer. I could create these, but instead, I have the property
list modified once more: