LoginSignup
12
12

More than 5 years have passed since last update.

Cocoaアプリケーションに dylibをバンドルする

Last updated at Posted at 2013-11-13

解決したい課題

あるdylibを使用するCocoaアプリケーションを開発する際、ユーザーがそのdylibをインストールしているか分からないのでアプリケーション中にバンドルして提供したい

この例では libusb-1.0.0.dylibをバンドルした時の方法について解説する

手順

  1. dylibをコピー
  2. Build Phase(Copy Files)を追加
  3. Build Phase(Run Script)を追加
  4. Link Binary with Librariesの変更

dylibをコピー

xcodeに libusb-1.0.0.dylibを追加する
スクリーンショット 2013-11-13 12.05.20.png

Build Phase(Copy Files)を追加

Targetの Build Phaseを修正する。 Editor → Add Build Phase → Add Copy Files Build Phase
このPhaseはLink Binary with Librariesの上に置く。
* Destination: Shared Support
* Subpath: 空白
* Copy only when installing: チェックなし
リストに libusb-1.0.0.dylibを追加。
スクリーンショット 2013-11-13 12.05.42.png

Build Phase(Run Script)を追加

Editor → Add Build Phase → Add Run Script Build Phase
このPhaseは上述の Copy Files Build Phaseの下に置く。

スクリプトは以下のとおり。これは参考にしたサイトからそのまま。

#!/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

Link Binary with Librariesの変更

libusb-1.0.0.dylibを追加する。今回はlibusbが依存している IOKITも追加している。
スクリーンショット 2013-11-13 12.20.12.png

最終的な Build Phaseの順番はこんな感じに。
スクリーンショット 2013-11-13 12.21.08.png

完成

これでバンドルとリンクの作業は完成。libusbのexampleを AppDelegateに入れて動かしてみたがちゃんと動いている。
スクリーンショット 2013-11-13 12.07.06.png

注意

リリース時にはライセンス表示を忘れずに。

参考

12
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
12