Building GSL as a Universal Binary
GSL
The GSL (Gnu Scientific Library) is a robust set of math routines, very useful for scientific computing. The comet program uses the GSL to generate random numbers, and though we don’t absolutely require the GSL, it is better (more random) than using the standard rand() function.
If you want to link against the GSL and make code that runs on multiple architectures on OS X, you’ll need a universal binary of the library, like this precompiled universal binary of the GSL for OS X, including the ppc, ppc64, i386 and x86_64 architectures.
Building GSL as a Universal Binary
The script I used to build it is below, and is based on this post. If you want to compile it yourself, download the latest version of the GSL (.tar.gz file) from here or one of the mirrors, then run the script below in the same directory. If you’re worried about the compiler optimizations affecting accuracy, remove the ‘-O3′ from COMPFLAGS. The finished libraries are in the ‘libs’ directory and the headers are in ‘include’. The script makes universal libraries of all of the ‘.a’ files (though I only use libgsl.a)
#!/usr/bin/env bash
# possible archs: i386 ppc x86_64 ppc64
# note: ppc64 doesn't work in Snow Leopard
ARCHITECTURES="i386 ppc x86_64"
# number of cpus to use during compile
COMPILETHREADS=2
COMPILER=gcc-4.2
#COMPILER=/Developer/usr/bin/llvm-g++-4.2
# configure flags
CONFFLAGS="--disable-shared"
# compiler flags
COMPFLAGS="-O3"
# find the name of the source file
SOURCEFILE=`find . -name "gsl*.tar.gz" -depth 1`
SOURCEFILE=${SOURCEFILE:2:100}
GSLVER=${SOURCEFILE%.tar.gz}
# unzip the main source file
tar -xzf $SOURCEFILE
for ARCH in $ARCHITECTURES
do
echo Compiling for ${ARCH}...
# copy source to new directory
cp -r ${GSLVER} ${GSLVER}_$ARCH
cd ${GSLVER}_$ARCH
# configure for architecture
if [ "$ARCH" != "ppc64" ] ; then # not sure why we have to do cross compile for ppc64 but not ppc...
env CC="$COMPILER" CFLAGS="-arch $ARCH $COMPFLAGS" LDFLAGS="-arch $ARCH" ./configure $CONFFLAGS --build=$ARCH
else
env CC="$COMPILER" CFLAGS="-arch $ARCH $COMPFLAGS" LDFLAGS="-arch $ARCH" ./configure $CONFFLAGS --host=$ARCH
fi
# compile for architecture
make clean ; nice make -j $COMPILETHREADS
cd ..
done
mkdir include
mkdir libs
cp `find ${GSLVER} -name "*.h"` include
echo
echo
echo Making Universal Binaries...
find ${GSLVER}_$ARCH -name "*.a"
for LIBRARY in `find ${GSLVER}_$ARCH -name "*.a"`
do
echo
LIB=`basename $LIBRARY`
echo Library: $LIB
find . -name $LIB
lipo -create `find . -name $LIB | xargs` -output libs/$LIB
lipo -info libs/$LIB
done
echo
echo Universal Binaries:
echo
lipo -info libs/*.a
Related posts:
- Installing VTK on Mac OS X Download Universal Binaries of the VTK libraries for OS X here (also how to build them yourself). The comet program...






Thanks for this script! It builds all but ppc64 on snow leopard on my mbp. How do you install the resulting .a file, while also generating a .dylib file as a normal configure, make, make install? Thanks again!
You’re welcome!
I briefly played with building a shared library, and it looks like it isn’t straightforward. If you set ‘
--enable-shared‘ in the configure flags, it will still only compile the static library unless it’s for the native architecture. From the output of configure, it looks like libtool is balking at the cross-compile.I’m not doing a ‘make install’, because I don’t want it putting stuff into the system directories. I think it safer to just to manually copy the library and includes where you want them, then statically link the ‘.a’ file by dragging it into the libraries section of the project in XCode, and put the GSL include directory in the path.
As for the ppc64—it looks like they might have pulled support for cross-compiling ppc64 code from Snow Leopard.
Hi, I need to build both libgsl.a and libgslcblas.a as universal binaries, but I cannot modify your script to make that happen – how do I do this? Also, I need the *.h include files but in /include I only get a Makefile.in and Makefile.am. I’m on 10.6.1, XCode 3.2/GCC 4.2 and GSL 1.13.
Thanks for any advice!
I’ve updated the script to make all the ‘.a’ files (including ‘libgslcblas.a’) into universal binaries, and fixed the misplaced ‘.h’ files (moved in v1.13?) Thanks.
Mark, this worked absolutely perfectly. Thank you so much for this great post!
Ian