Xcode: Easy Shared & Static Lib Management and Linking

In Xcode, you can add existing frameworks (binary shared/static libraries or Apple's packaged framework) to a target in your project by choosing from the framework list or drag'n'drop from Finder.

Several libraries under "Frameworks" group (e.g. libBusted.a)

Benefit of doing this is obvious: you can manage and see what libraries your program is linked to. However, after doing that, I noticed one thing. Xcode does not link shared/static libraries in a simple way like:
gcc -o qux libfoo.dylib libbar.a buz.o
Instead, when libraries are going to be added, Xcode obtains paths to the directories containing these libraries, and appends these to the build setting "Library Search Path" of the target. So that the actual linking looks like:
gcc -L/path/to/foobar -o qux -lfoo libbar.a buz.o

Compared to the former, even though orthodox, linking in the latter is indirect. I like the former method because it's simple and error-free even though it somewhat lacks portability: if multiple versions of .dylib or .a with the same names exist somewhere under a set of paths defined by the build setting "Library Search Path", depending on the order of search, a version of .dylib or .a your program linked against changes. This happens automagically, but it may cause hard-to-find link or runtime errors.


I don't know how to change this Xcode behavior, but by changing "File Type" of libraries to compiled.mach-o.objfile, you can link libraries directly (image below). For this, you can manage libraries more visually and intuitively inside your Xcode project.


For a shared library, remember the argument passed by -install_name at its compile time. In the host your program runs, the shared library must be installed under the specified directory with the specified name according to that argument, or dynamic linking fails. If things go wrong with your shared library about its install name, try command line tools otool and install_name_tool to correct the argument.

Comments

Popular posts from this blog

Hardcoding Data Like Images and Sounds into HTML or CSS

DDS for GIMP (Mountain Lion, Native, no X11 GUI)

Make It Work: Global .gitattributes