I am using the Boost library on OS X using Xcode. Boost was installed on my system using macports. I have successfully built my app by adding the 3 boost libraries I need (for example, libboost_thread-mt.a) to that Targets ‘Link Binary With Libraries’ list. However I need to link these libraries statically so that the app will run on other computers without the boost library needing to be installed.
How do I do this exactly? Through my numerous google searches I’m finding I might need to add ‘-static’ – where do I add this in Xcode?
If you’ve linked with a
.alibrary, then you have already linked statically. You never need to ship.alibraries. They’re just bundles of objects.EDIT: Your error strongly suggests that you’re linking the dylib rather than the .a. If you have
libfoo.dylibandlibfoo.ain your library path, even if you say “linklibfoo.a” in Xcode, and even iflibfoo.ais earlier in the search path, it will still linklibfoo.dylib. This is because Xcode’s linking is totally broken and passes-lfooto the linker (you should never use-lfor something you built and have the exact path to). I always recommend linking libraries you built inLDFLAGSin an xcconfig file rather than using the build pane. You pass the exact path you want rather than using-l. See Abandoning the Build Panel for more of my thoughts on xcconfig. It’s out of date now, since it was written for Xcode3, but the basics still apply.Using the build pane, you can also pass the entire path to the library in “Other Linker Flags.” But this still has all the problems of the build pane.
The quicker (but less robust) solution is sometimes to add
-Wl,-search_paths_firstto the “Other Linker Flags.” This changes the behavior so that each library path is searched for both.dyliband.abefore going on (the default behavior is to search everywhere for.dyliband only then search for.a). So if your.ais in a different directory from your.dylib, and that directory is earlier in the search path, this will work.This question finally got me to open a radar on this, which I should have done years ago. I recommend that others open duplicates.