« GNU Radio with gr-osmosdr on OS X | Main | Cypress FX2 USB Microcontroller »
Wednesday
Oct172012

Embedding a Dynamic Library in a Cocoa Application

Objective

Your application depends on a Dynamic Library that may not be on the users machine, and you don’t want to ship your application with an installer. The solution is to include the dylib in your application’s bundle. The problem is that dylibs have their path hard-coded in them. The solution is to use the Apple supplied command line tool install_name_tool to modify the path inside the dylib to match its location inside your application bundle.

Steps

  1. Add a new Copy Files build phase
  2. Add a Shell Script build phase

Note: These build phases need be added before the Link Binary Libraries build phase

Copy Files Build Phase

Click the + button, click on the dynamic library already added to the Xcode project.

Destination               = Shared Support
Subpath                   = leave blank
Copy Only When Installing = unchecked

Shell Script Build Phase

This buid phase needs to be after the Copy Files phase.

Here’s the soure code, which I downloaded from here. I had to play around with ynniv’s script to accound for spaces in path names. I’m guessing his projects didn’t have any spaces and he never ran into that problem.

#!/bin/bash
# In Xcode you actually enter the path for the shell
# you want to use in a text field about the shell source.
# So, !/bin/bash might not be necessary.

# Space separated list of libraries.
# Enter any dylibs you have in your Copy File Phase.
TARGETS=libusb-1.0.0.dylib

EXECFILE=${BUILT_PRODUCTS_DIR}/${EXECUTABLE_PATH}
LIBPATH=${BUILT_PRODUCTS_DIR}/${SHARED_SUPPORT_FOLDER_PATH}
NEWLIBPATH=@executable_path/../SharedSupport

for TARGET in ${TARGETS} ; do
  LIBFILE=${LIBPATH}/${TARGET}
  TARGETID=`otool -DX "${LIBPATH}/$TARGET"`
  NEWTARGETID=${NEWLIBPATH}/${TARGET}
  install_name_tool -id "${NEWTARGETID}" "${LIBFILE}"
  install_name_tool -change ${TARGETID} ${NEWTARGETID} "${EXECFILE}"
done

References (31)

References allow you to track sources for this article, as well as articles that were written in response to this article.
  • Response
    stomach exercises to lose weight
  • Response
    I signed up to your blog RSS. Will you post more about this subject?
  • Response
    Response: bianchi-morley.com
    Excellent post! I've bookmark this site to return later. thanks!
  • Response
    Response: lineage casio
    デジタル時計 腕時計
  • Response
    ルミノックス 腕時計
  • Response
    NFL is genuinely one of the greatest sports in America. It has a significant following.
  • Response
    Response: dumpsters
    Chasing 'trons - Notes to Self - Embedding a Dynamic Library in a Cocoa Application
  • Response
    Response: Saleh Stevens
    Chasing 'trons - Notes to Self - Embedding a Dynamic Library in a Cocoa Application
  • Response
    Response: Saleh Stevens
    Chasing 'trons - Notes to Self - Embedding a Dynamic Library in a Cocoa Application
  • Response
    Response: Saleh Stevens
    Chasing 'trons - Notes to Self - Embedding a Dynamic Library in a Cocoa Application
  • Response
    Response: Saleh Stevens
    Chasing 'trons - Notes to Self - Embedding a Dynamic Library in a Cocoa Application
  • Response
    Response: Saleh Stevens
    Chasing 'trons - Notes to Self - Embedding a Dynamic Library in a Cocoa Application
  • Response
    Response: Saleh Stevens
    Chasing 'trons - Notes to Self - Embedding a Dynamic Library in a Cocoa Application
  • Response
    Response: Saleh Stevens
    Chasing 'trons - Notes to Self - Embedding a Dynamic Library in a Cocoa Application
  • Response
    Response: Saleh Stevens
    Chasing 'trons - Notes to Self - Embedding a Dynamic Library in a Cocoa Application
  • Response
    Response: Saleh Stevens
    Chasing 'trons - Notes to Self - Embedding a Dynamic Library in a Cocoa Application
  • Response
    Response: Saleh Stevens
    Chasing 'trons - Notes to Self - Embedding a Dynamic Library in a Cocoa Application
  • Response
    Response: saleh stevens
    Chasing 'trons - Notes to Self - Embedding a Dynamic Library in a Cocoa Application
  • Response
    Response: Saleh Stevens
    Chasing 'trons - Notes to Self - Embedding a Dynamic Library in a Cocoa Application
  • Response
    Chasing 'trons - Notes to Self - Embedding a Dynamic Library in a Cocoa Application
  • Response
    Response: chaplain jobs
    Chasing 'trons - Notes to Self - Embedding a Dynamic Library in a Cocoa Application
  • Response
    Response: chaplain jobs
    Chasing 'trons - Notes to Self - Embedding a Dynamic Library in a Cocoa Application
  • Response
    Chasing 'trons - Notes to Self - Embedding a Dynamic Library in a Cocoa Application
  • Response
    Response: reading is knowing
  • Response
    Chasing 'trons - Notes to Self - Embedding a Dynamic Library in a Cocoa Application
  • Response
    Chasing 'trons - Notes to Self - Embedding a Dynamic Library in a Cocoa Application
  • Response
    Response: gta online money
    Chasing 'trons - Notes to Self - Embedding a Dynamic Library in a Cocoa Application
  • Response
    Response: Carports For Sale
    Chasing
  • Response
    Chasing
  • Response
    Chasing
  • Response
    Response: Metal carports
    Chasing

Reader Comments

There are no comments for this journal entry. To create a new comment, use the form below.

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>