3 Solutions Collect From Internet About “How can I link a dynamic library in Xcode?”

If I understand your problem correctly, your app is building fine, no errors when linking, but when you try to launch it the library cannot be found.

That’s not surprising, since the dylib file is in some arbitrary directory not on the system path. You either need to copy it into /usr/lib (probably not a good idea) or include it in the application bundle. The latter is probably the better approach.

I’ve never tried it, but apparently you need to use a Copy Files Build Phase to put the dylib inside your bundle and then configure Xcode so that your executable will know where to find it.

I’ve just done exactly this, with a Module Bundle project. This was being included into a larger project with a separate executable.

I added a “Copy dylibs to frameworks” step, which copied the dylibs to /Foobar.bundle/Contents/Frameworks/Foobar/. Then I added a Run Script Phase to run as the final step, to fix the install names of the dylibs in the executable:

I had over a dozen of these to link against; I had to persuade the supplier of the dylibs to pad their header to accommodate my many install_name changes …

You just drag the library from the directory onto your Xcode project, best into resources but it does not matter (I believe). I tried with and without checking the ‘copy files into bundle’ and it work in both cases, not sure if you need to include it in bundle for deployment.