Using dynamic libraries in Mac OS X

I downloaded and built the excellent Qwt widget collection on my Mac. When I built the examples I had trouble getting them to find the libqwt dynamic library. They built and linked just fine, but unless I added the path to the qwt install dir to my DYLD_LIBRARY_PATH environment variable launching the application fails with a dyld: Library not loaded: libqwt.5.dylib error.

On Linux the solution would be to add a -Wl -rpath /usr/local/qwt-5.0.2/lib to my LIBS directive in my projects’s .pro file (in this case the relevant line is in the examples/examples.pri config file). This should add the path to qwt’s dylib to my application’s runtime search path. Unfortunately this doesn’t work in Mac OS X. As it turns out, the library itself has to know its full install path.

So far, the best webpage on this problem I’ve found so far is here. It seems that Mac OS X even comes with its own utility, install_name_tool, to manage libraries’ names if one needs to move them around.

I skipped install_name_tool and opted to simply rebuild Qwt’s dylib so that it knows its install_name. My solution was to modify the LDFLAGS directive in qwt’s src/Makefile so that

-install_name libqwt.5.dylib

Looks like this instead:

-install_name /usr/local/qwt-5.0.2/lib/libqwt.5.dylib

This is not the best solution as I had to modify my Makefile directly instead of the Qt .pro file. After another make and sudo make install to reinstall the dynamic library, oTool -L libqwt.5.dylib confirms that the library’s name contains its own full path:

> otool -L libqwt.5.dylib
/usr/local/qwt-5.0.2/lib/libqwt.5.dylib . . .

More importantly, the Qwt examples all run now and I didn’t have to modify my DYLD_LIBRARY_PATH!

The next step is to modify the .pro file or my qmake.conf so it handles all this install_name magic automatically at compile/install time, but this worked well for now…

Comments are closed.