LoginSignup
3
2

More than 3 years have passed since last update.

Unity用のdylibを使ったC++Pluginを作るときに困った

Last updated at Posted at 2020-03-03

dylib使ったUnity用のC++Pluginを作った際に、DllNotFoundExceptionエラーが出て困ったのでメモ

Xcode Projectの作成

  1. create new Xcode project > Bundleで新しいプロジェクトを作る
  2. C++のソースコードを適当に書いて追加する
  3. 使いたいdylib(hoge.dylib)もプロジェクトに追加する
  4. dylibを Build PhasesCopy Bundle Resources にも追加する
  5. ビルドする

スクリーンショット 2020-03-03 16.50.48.png

Unity Projectにの作成

  1. Unityで新しいプロジェクトを作る
  2. ビルドしたBundleをプロジェクトに追加する
  3. 適当にスクリプトを書く
  4. 実行すると DllNotFoundException error が出てしまう

スクリーンショット 2020-03-03 17.25.53.png

ススクリーンショット 2020-03-03 17.27.43.png

TestNativeが依存ライブラリのhoge.dylibを読み込めずエラーがでてしまうらしい

プラグインの依存パスを書き換える

TestNativeがhoge.dylibを読み込めずエラーが出るので依存パスを書き換える

プラグインの依存パス確認

パスの確認には otool を使う

$ cd TestNative.bundle/Contents/MacOS
$ otool -L TestNative

スクリーンショット 2020-03-03 17.45.42.png

プラグインの依存パス設定

パスの書き換えには install_name_tool を使う

$ install_name_tool -add_rpath @loader_path/.. TestNative
$ install_name_tool -change "@executable_path/hoge.dylib" "@rpath/Resources/hoge.dylib" TestNative

プラグインの依存パスをもう一度確認

$ otool -L NativePlugin

スクリーンショット 2020-03-03 17.37.13.png

Unityを実行

書き換わっていることを確認できたらUnityを再起動して実行する

参考サイト

http://blog.shogonir.jp/entry/2017/10/26/030028
https://qiita.com/omochimetaru/items/21d662b8df8bce1bc5ca
https://qiita.com/itoru257/items/d36b4e3908a39bfbbb18
https://xanadu6291.stars.ne.jp/?p=726

3
2
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
3
2