17

I am trying to create and then add the dylib to a project. I created it by using the "Cocoa-Library" template and setting the type to "Dynamic" (not sure if it should be dynamic or static...). Then I created a simple obj-c class called Test and wrote a method in it that prints out something to console.

I compiled and used the generated .dylib file and put it in another project. Now whenever I try to use it, I get this message on runtime"

dyld: Library not loaded: /usr/local/lib/TESTLib.dylib
  Referenced from: /Users/***/Library/Developer/Xcode/DerivedData/TestingDYLIB-axmoocnxbwznoyerfogosumqufxc/Build/Products/Debug/TestingDYLIB.app/Contents/MacOS/TestingDYLIB
  Reason: image not found

I also created a Copy File phase and set the destination to "Frameworks". I still get the same error. What am I doing wrong?

Cœur
  • 37,241
  • 25
  • 195
  • 267
user635064
  • 6,219
  • 12
  • 54
  • 100

2 Answers2

37

The issue is not where Xcode is looking for the library at compile time, which is what Simon Whitaker's answer addresses.

The issue is that the application which uses the dylib cannot find it at runtime. When an application is built that uses a dynamic library, it copies the install_name of the dylib into the output binary.

Creating a copy files phase and copying the dylib to the Frameworks subdirectory of the app's bundle is the right thing do do.

However, you need to do an additional step. You need to compile the dynamic library with an install_name appropriate for a bundle app. By default, a dynamic library is created with an install_name of /usr/local/lib. The app can't find your library there because it doesn't exist. Even if you put the library there, your users certainly won't have it, so that would be the wrong solution.

The right solution is bundling the library with the app. To set the install name for the dynamic library, go into the dynamic library project and set the "Dynamic Library Install Name" option to: @executable_path/../Frameworks/libmydynamiclibrary.dylib

wadesworld
  • 13,535
  • 14
  • 60
  • 93
  • I m having the same issue. I m not able to do the last step of the solution. Where is that option for setting up name to some frameworkpath. – JiteshW Nov 08 '11 at 05:08
  • Not understanding your problem. The option you set is the "Dynamic Library Install Name" – wadesworld Nov 09 '11 at 22:42
  • set the "Dynamic Library Install Name" option to: @executable_path/../Frameworks/libmydynamiclibrary.dylib.I did not understand how to get this path.from where I can fetch? – Akbar Apr 24 '12 at 11:18
  • 1
    how do i "go into the dynamic library project" ?, because i downloaded a .dylib and i don't get how to link it to the project to be portable – Fady Kamal Dec 21 '12 at 20:18
  • Yea same here i can't even figure out the first step where is the "dynamic library project"? – Steve Ham Oct 17 '13 at 12:01
  • In the original poster's case, he was creating his own dynamic library project, so he had the Xcode project file. If you're working with a downloaded dynamic library, if there's source code, there will be some sort of project file or Makefile. If there is no source code and only the library, you can use install_name_tool (see the man page) to change the install path recorded in the library – wadesworld Oct 24 '13 at 12:27
  • 1
    If I have a library and another library that has a dependency on the first library, is it possible to make a .framework for each, with the headers and a dylib with corrected install names included? This would be having only the dylib, not the source code, for the two libraries. – Farski Apr 16 '14 at 22:03
  • I would also like to know for the case where one dylib depends on another dylib. @farski did you figure that out? – apalopohapa Nov 04 '14 at 20:53
  • @apalopohapa I did not, though I haven't looked it again since iOS 8 came out, which has better support for things like this. – Farski Nov 04 '14 at 22:11
2

Xcode is looking in /usr/local/lib/ for the library. If the library is in another location, add that location to your Library Search Paths:

  1. Select project file in Xcode 4
  2. Select the target, then click the Build Settings tab
  3. Make sure All is selected in the filter bar (not Basic)
  4. Scroll down to the Search Paths section and you'll find Library Search Paths in there
Simon Whitaker
  • 20,506
  • 4
  • 62
  • 79
  • I dragged the .dylib into the frameworks folder in xcode. The library is located in: /Users/*****/Desktop/TestingDYLIB which is already there in library search paths – user635064 Jul 23 '11 at 16:34
  • i did that and set the location to my location which contains the .dylib and still can't find it – Fady Kamal Dec 21 '12 at 20:20