Joe S
Joe S

Reputation: 75

How do I build and use a dynamic library on Mac OS X?

I have a very simple question that I've been trying to figure out for the last 6 hours or so. I want to simply build a dynamic library on Mac OS X, and then build an application using that library. I've created the .dylib and compiled the test application with it, but when I run the application I get:

Joes-Mac-Pro:Desktop Joe$ ./test dyld: Library not loaded: ./lib/simple_library.dylib Referenced from: /Users/Joe/Desktop/./test Reason: image not found Trace/BPT trap: 5

I've tried making a lib folder in the executable directory and putting the dylib inside, same error. I've tried putting the dylib in the executable path itself, same error. I've tried using install_name_tool to change the path to the dylib in the executable, nothing changes, same error. I've tried building the test application with -headerpad_max_install_names and then using install_name_tool to change the path. Still nothing changes. Same error.

Is what I'm trying to do not possible with the Mac operating system? I'm new to this platform, and am used to things like this working without a hitch on Windows and GNU/Linux. Also, I'm trying to do all this with the command line. I would very much prefer to avoid XCode.

Edit: Oops, I derped. Turns out I made a typo in my install_name_tool arguments. It's working fine now.

Upvotes: 0

Views: 2743

Answers (2)

cor3ntin
cor3ntin

Reputation: 916

install_name_tool is the right tool. Try otool your_binary to see which dylib are missing. Also be sure your binary and the linked library are build for the same architectures.

Upvotes: 1

Kaelin Colclasure
Kaelin Colclasure

Reputation: 3975

You'll need to ensure that DYLD_LIBRARY_PATH includes the directory where your library resides.

   DYLD_LIBRARY_PATH
          This is a colon separated list of directories that contain libraries. The dynamic linker searches these directo-
          ries before it searches the default locations for libraries. It allows you to  test  new  versions  of  existing
          libraries.

          For  each library that a program uses, the dynamic linker looks for it in each directory in DYLD_LIBRARY_PATH in
          turn. If it still  can't  find  the  library,  it  then  searches  DYLD_FALLBACK_FRAMEWORK_PATH  and  DYLD_FALL-
          BACK_LIBRARY_PATH in turn.

          Use  the  -L  option to otool(1).  to discover the frameworks and shared libraries that the executable is linked
          against.

The various details of dynamic linking are documented in the dyld man page.

Upvotes: 0

Related Questions