Compiling, installing and using AntTweakBar on Mac OS X, as a static library

Alec Jacobson

October 04, 2010

weblog/

I recently posted about compiling and installing the very cool prototyping UI library, AntTweakBar, on Mac OS X. That post describes how to compile as a dynamic library, which is great for development of many different projects on the same machine. But when I want to show my app to others the last thing I want is for them to have to compile all of the dependencies as global dynamic libraries. Instead I'd rather package up my binary with statically linked dependency libraries. So after a bit of a struggle over my lack of experience with gcc and even contacting AntTweakBar's developer, I've found at least one way to compile AntTweakBar as a static library on Mac OS X, universal for 32-bit or 64-bit apps. Dowload the source from the AntTweakBar site. Go to the source directory:
cd AntTweakBar/src
Make some changes to the Makefile.osx. Edit the lines to look like this
SO_EXT          = .a

#---- Release
CXXCFG          = -O3 -arch i386 -arch x86_64
and
$(TARGET): $(OBJS)
        #@echo "===== Link $@ ====="
        #$(LINK) $(LFLAGS) -dynamiclib -Wl,-undefined -Wl,dynamic_lookup  -o $(OUT_DIR)/lib$(TARGET)$(SO_EXT) $(OBJS) $(LIBS)
        @echo "===== Archive $@ ====="
        $(AR) $(OUT_DIR)/lib$(TARGET)$(SO_EXT) $(OBJS)
Depending on your OS version you may have to adjust this line, too:
BASE            = /Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks
Copy Makefile.osx over the exisiting Makefile:
cp Makefile.osx Makefile
Then build with
make
You may see some suspicious looking output like:
ranlib: archive library: ../lib/libAntTweakBar.a will be fat and ar(1) will not be able to operate on it
ranlib: for architecture: i386 file: ../lib/libAntTweakBar.a(TwPrecomp.o) has no symbols
ranlib: for architecture: x86_64 file: ../lib/libAntTweakBar.a(TwPrecomp.o) has no symbols
But I haven't run into any problems with this yet... Now go over to the examples:
cd ../examples
You can compile the GLUT example with:
gcc -O3 -arch i386 -arch x86_64 -Wall -fno-strict-aliasing -D_MACOSX TwSimpleGLUT.c -framework OpenGL -framework GLUT -framework AppKit -L../lib -lAntTweakBar -lstdc++ -o TwSimpleGLUT -I ../include
You'll notice a couple more links in this compile command than when using the dynamic library. The most important to notice is the -lstdc++ which is a little weird but definitely needed (you could switch gcc to g++, instead, too). There were other link options in the original Makefile.osx that don't seem necessary for this example, but maybe for others... I'm not sure. Update: A couple people have been getting the following error on the final ar line of the makefile:
===== Archive AntTweakBar =====
ar cqs ../lib/libAntTweakBar.a TwColors.o TwFonts.o TwOpenGL.o TwBar.o TwMgr.o TwPrecomp.o LoadOGL.o TwEventGLFW.o TwEventGLUT.o TwEventSDL.o
ar: ../lib/libAntTweakBar.a: Inappropriate file type or format
make: *** [AntTweakBar] Error 1
Here's how I fixed this with in at least one case. Delete the incomplete libAntTweakBar.a file:
rm ../lib/libAntTweakBar.a 
Then issue:
ar r ../lib/libAntTweakBar.a TwColors.o TwFonts.o TwOpenGL.o TwBar.o TwMgr.o TwPrecomp.o LoadOGL.o TwEventGLFW.o TwEventGLUT.o TwEventSDL.o
You may see a warning like:
ar r ../lib/libAntTweakBar.a TwColors.o TwFonts.o TwOpenGL.o TwBar.o TwMgr.o TwPrecomp.o LoadOGL.o TwEventGLFW.o TwEventGLUT.o TwEventSDL.o
ar: creating archive ../lib/libAntTweakBar.a
/usr/bin/ranlib: file: ../lib/libAntTweakBar.a(TwPrecomp.o) has no symbols
This is OK, I've been told by the AntTweakBar developer that this file is ignored anyway if you're not on windows. Then you should be able to compile and run the example above.