static link library to replace dylib in mac os x

This post on the xcode-users list explains a neat trick on how to get xcode to link a static library instead of a dynamic library.

Imagine the circumstance: you want to use a particular non-standard version of a native library which is already part of the system; perhaps you want to use an old version with better compatibility, or a newer version with more features. The obvious thing to do is to take a static build of the library and add it your xcode project.

However this typically won’t work. The linker will choose the system version instead because by default it looks in all possible locations for a dynamic library first before looking through the same list of locations for a static library. If there is a dynamic library in any of the search locations it will always get chosen. There are some good reasons for this, but what if you really want to include your own statically linked version?

The answer is to add the -Wl,-search_paths_first flag to the other link flags option (under linking in target settings).

When this is set each possible location for a library is inspected first for a dynamic library and then for a static library. This means that the static library will get linked correctly.

It’s not exactly something that will be needed frequently though.

Edit: Apple have a technical Q&A article (1393) on this very subject which appeared a couple of days back and which I somehow missed.
It offers this exact method. [Link]

This entry was posted in Macintosh, SQLEditor and tagged , , , , . Bookmark the permalink.

One Response to static link library to replace dylib in mac os x

  1. Angus Hardie says:

    There is also some more information on this here:

    http://lists.apple.com/archives/Unix-porting/2005/Jan/msg00066.html

Leave a Reply