Search code examples
xcodelinkermach-o

Setting Xcode linker up correctly: library not found, but in "Link Binary With Libraries" list


I have a issue with dependencies in my dylibs.

I currently get the follwing error message:

dyld: Library not loaded: /opt/local/lib/libgvc.6.dylib
  Referenced from: /Users/klauskneupner/Library/Developer/Xcode/DerivedData/Visual_Thinking-bvgfcqjwnobabodenabpggrwnoet/Build/Products/Debug/Visual Thinking.app/Contents/Frameworks/libgvplugin_dot_layout.6.dylib
  Reason: image not found

The funny part is that I have that library (libgvc.6) in the list of dependencies. build settings But in this case, the libgvc.6 is in a project directory and not in /opt/local/lib.

What do I need to do? Many thanks in advance!


Solution

  • At build time the static linker on OS X, ld, writes the shared library identifier of each library that your application links against in to the application binary. At run time the dynamic linker, dyld, attempts to load each shared library from the paths specified in the application binary. You can see this information using otool -L YourApp.app/Contents/MacOS/YourApp.

    The reference to /opt/local/lib/libgvc.6.dylib in your crash output indicates that is the shared library identifier of libgvc.6.dylib in your project. To include the library in your .app bundle in a way that your application will use it rather than looking in /opt/local/lib you need to:

    1. Change the shared library identifier of libgvc.6.dylib so that dyld will look for the binary within your application bundle. This is typically done by running install_name_tool -id @rpath/libgvc.6.dylib libgvc.6.dylib. The @rpath placeholder tells dyld to try substituting each of the entries in the runpath search path of the binary that is loading the library. By default your app bundle's Frameworks directory is on this search path.

    2. Update any existing prebuilt libraries that link to libgvc.6.dylib to refer to it via the new name (the change made in step 1 only takes effect when a new binary is built against the library, so it may not be necessary if it is only prebuilt libraries that link against it). You can use install_name_tool -change /opt/local/lib/libgvc.6.dylib @rpath/libgvc.6.dylib path/to/other.dylib to update these other libraries. Your crash output shows that libgvplugin_dot_layout.6.dylib is one such library that would need updated. It may not be the only one.

    3. Ensure that your modified libraries are being copied in to the Frameworks subdirectory in your application bundle. This is typically done using a Copy Files build phase in your Xcode project.