Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boost library static linking on Xcode 4

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?

like image 737
Liam Lacey Avatar asked Mar 06 '12 15:03

Liam Lacey


1 Answers

If you've linked with a .a library, then you have already linked statically. You never need to ship .a libraries. 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.dylib and libfoo.a in your library path, even if you say "link libfoo.a" in Xcode, and even if libfoo.a is earlier in the search path, it will still link libfoo.dylib. This is because Xcode's linking is totally broken and passes -lfoo to the linker (you should never use -l for something you built and have the exact path to). I always recommend linking libraries you built in LDFLAGS in 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_first to the "Other Linker Flags." This changes the behavior so that each library path is searched for both .dylib and .a before going on (the default behavior is to search everywhere for .dylib and only then search for .a). So if your .a is 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.

like image 162
Rob Napier Avatar answered Oct 20 '22 19:10

Rob Napier