Tuesday, March 20, 2012

Installing xgraph on Mac OS/X Lion

After years on Thinkpads running Linux, I decided to take the plunge on an Apple Mac Air. Macs have always had a premium price for the hardware, and I never considered the cost justified, since you're buying a lot of software with that money and I have all the software I need with Linux. But the deal with the Macs is they simply work, and the Air is a beautifully designed machine.

After searching for a quick data plotting application, I was frustrated to find I still couldn't find anything which beat the old tried-and-true: xgraph, written at Berkeley in the 1980's It produces nice plots, data sets clearly differentiated in nice colors against a neutral grey background, with a super-simple data format trivially processed with scripting. The code's beauty is its simplicity and it's excellent defaults: no widgets or pull-down menus to deal with. At work I can tell people think I'm a bit strange for using such ancient code, but I get the last laugh when I toss well-scaled plots up on my screen in a fraction of the time it would take them using their tools.

The primary competition is almost as venerable: gnuplot. Gnuplot is even better, but I've never wrapped my head around the syntax. So I stick with xgraph. For publication-ready stuff I still use xmgrace, descendent of xmgr, another very old code. I'd love to hear options which do a better job in less time than either xmgrace or xgraph.

Anyway, I had to compile the xgraph source in OS/X. This was especially important to me because I've done a few custom modifications to the C source for myself (in particular, providing a "-aspect" command line option which keeps sets the autoscale ranges of the x and y axes to the same span, and also changing the tick-labels to a "%g" format).

First issue was getting it to compile. "dialog.c" contains a procedure called "getline". This is in conflict with a procedure in the standard library, so I changed the two references in "getline" in dialog.c (the definition and call) to "getline_xgraph". Now it compiled.

But then I faced a tougher problem: I was getting a persistent segmentation fault when I ran it. gdb wasn't turning up the problem, either.

The clue was in the compiler warnings: xgraph plays fast and loose with casting between addresses and integers. Sometimes that works, sometimes... So in desperation I checked the gcc man page and started reading, looking for an option which treated all integers as longs. I didn't think this would work, because external libraries might not cooperate.

I didn't find it. But what I found instead was the "-m32" option to compile the code in 32-bit mode even on a 64-bit machine. Hmm... seemed promising. I added this to the head of the "CFLAGS" definition in Makefile (Makefile having been generated with "configure", so this isn't the best approach). I tried it and, ding-ding-ding-ding, the code ran. My little victory shout was probably out of place, since I was on Caltrain at the time...

So now it works.

There's still an issue that when the window is resized the plot can become corrupted. But that's quickly fixed by minimizing then restoring the window.

Here's a screen grab, comparing speed from a ride to smoothed speed as processed with my Ruby script: plot


Alan said...

Thanks for your tip. The -m32 compile switch (along with -arch i386) was key. I found (on Mountain Lion) that I also had to add /opt/X11/lib/libX11.dylib to the linker flags in the generated Makefile.

It's just great to have xgraph back! Such a useful everyday plotting tool!

Wladek Grabinski said...

also for me the -m32 compile switch solved my problem.

and now I can use xgraph on my macbook air

Wladek Grabinski said...

the -m32 compile switch has solved my problem.

I can user xgraph on my macbook air!

John Szymanski said...

Yes - this worked for me as well with graph-12.1 on both Lion and Mountain Lion. Made the following mods to the Makefile after running ./configure :

Added (-m32 and -arch i386) below:


Added path to lib below:
X_EXTRA_LIBS =/usr/X11/lib/libX11.dylib

-lX11 flag from xgraph_LDADD

Thanks for the help guys!

Rohit Mordani said...

I did everything in this post but I still get errors while running make. The errors are like

In file included from xgraph.c:32:
xgraph.h:10:21: error: X11/Xos.h: No such file or directory


In file included from xgraph.c:32:
xgraph.h:138: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token


xtb.h:94: error: expected ‘)’ before ‘win’


In file included from xgraph.c:35:
params.h:33: error: expected specifier-qualifier-list before ‘XColor’

amongst other errors :(

Shaikha Al-Khuder said...

/Applications/Xcode.app/Contents/Developer/usr/bin/make all-am
source='xgraph.c' object='xgraph.o' libtool=no \
DEPDIR=.deps depmode=none /bin/sh ./depcomp \
gcc -DHAVE_CONFIG_H -I. -g -I/usr/X11/include -c xgraph.c
/bin/sh: ./depcomp: No such file or directory
make[1]: *** [xgraph.o] Error 127

Get the above error when I try to run "make" in my terminal...
Do you have any idea how to fix this?