Xamarin.iOSのプロジェクトでNative Static Libraryを利用したところコンパイルが通らなくなりiPhoneのシミュレータで動作の確認ができない。
解決済
回答 1
投稿
- 評価
- クリップ 0
- VIEW 515
前提・実現したいこと
Xamarin.FormsでOpenCV for iOSを利用したiOSアプリを作成したいと思っています。
まずOpenCVのサイトからiOS用のopencv2.frameworkを取得し、Xcodeを利用してOpenCV for iOSをラッピングするStatic LibraryとしてlibOpenCViOS.aを作成しました。
このスタティックライブラリにはOpenCVを利用して画像をグレイスケールにする関数toGrayImgを作成しました。
@interface OpenCViOS : NSObject
+ (UIImage *)toGrayImg:(UIImage *)src;
@end
次にXamarin.Formsのテンプレートを利用してプロジェクトHelloXamarinを作成し、そこにlibOpenCViOS.aをバインドするバインディングプロジェクトBindingOpenCViOSを追加しました。
Objective Sharpieを利用してinterfaceを作成しました。
[BaseType(typeof(NSObject))]
interface OpenCViOS
{
[Static]
[Export("toGrayImg:")]
UIImage ToGrayImg(UIImage src);
}
次にHelloXamarinプロジェクトのiOS依存プロジェクトHelloXamarin.iOSの参照にプロジェクトBindingOpenCViOSを追加し、toGrayImgを呼び出す処理を追加しました。
するとHelloXamarin.iOSのビルドでエラーが発生しビルド出来なくなってしまいました。
発生している問題・エラーメッセージ
error MT5209 : Native linking error : warning: ignoring file /Users/_develop/Documents/repos/HelloXamarin/HelloXamarin/HelloXamarin.iOS/obj/iPhoneSimulator/Release/mtouch-cache/libOpenCViOS.a, building for iOS Simulator-x86_64 but attempting to link with file built for iOS-arm64
MTOUCH : error MT5211: Native linking failed, undefined Objective-C class: OpenCViOS. The symbol '_OBJC_CLASS_$_OpenCViOS' could not be found in any of the libraries or frameworks linked with your application.
MTOUCH : warning MT5215: References to 'System' might require additional -framework=XXX or -lXXX instructions to the native linker
MTOUCH : warning MT5215: References to 'System' might require additional -framework=XXX or -lXXX instructions to the native linker
MTOUCH : warning MT5215: References to 'System.Net.Security' might require additional -framework=XXX or -lXXX instructions to the native linker
MTOUCH : warning MT5215: References to 'System' might require additional -framework=XXX or -lXXX instructions to the native linker
MTOUCH : warning MT5215: References to 'System' might require additional -framework=XXX or -lXXX instructions to the native linker
MTOUCH : error MT5202: Native linking failed. Please review the build log.
ld : warning : ignoring file /Users/_develop/Documents/repos/HelloXamarin/HelloXamarin/HelloXamarin.iOS/obj/iPhoneSimulator/Release/mtouch-cache/libOpenCViOS.a, building for iOS Simulator-x86_64 but attempting to link with file built for iOS-arm64
Undefined symbols for architecture x86_64:
"_OBJC_CLASS_$_OpenCViOS", referenced from:
-u command line option
ld: symbol(s) not found for architecture x86_64
clang : error : linker command failed with exit code 1 (use -v to see invocation)
プロジェクト "HelloXamarin.iOS.csproj" 内のターゲット "_CompileToNative" のビルドが終了しました -- 失敗。
プロジェクト "HelloXamarin.iOS.csproj" のビルドが終了しました -- 失敗。
ビルドに失敗しました。
MTOUCH : warning MT5215: References to 'System' might require additional -framework=XXX or -lXXX instructions to the native linker
MTOUCH : warning MT5215: References to 'System' might require additional -framework=XXX or -lXXX instructions to the native linker
MTOUCH : warning MT5215: References to 'System.Net.Security' might require additional -framework=XXX or -lXXX instructions to the native linker
MTOUCH : warning MT5215: References to 'System' might require additional -framework=XXX or -lXXX instructions to the native linker
MTOUCH : warning MT5215: References to 'System' might require additional -framework=XXX or -lXXX instructions to the native linker
ld : warning : ignoring file /Users/_develop/Documents/repos/HelloXamarin/HelloXamarin/HelloXamarin.iOS/obj/iPhoneSimulator/Release/mtouch-cache/libOpenCViOS.a, building for iOS Simulator-x86_64 but attempting to link with file built for iOS-arm64
error MT5209 : Native linking error : warning: ignoring file /Users/_develop/Documents/repos/HelloXamarin/HelloXamarin/HelloXamarin.iOS/obj/iPhoneSimulator/Release/mtouch-cache/libOpenCViOS.a, building for iOS Simulator-x86_64 but attempting to link with file built for iOS-arm64
MTOUCH : error MT5211: Native linking failed, undefined Objective-C class: OpenCViOS. The symbol '_OBJC_CLASS_$_OpenCViOS' could not be found in any of the libraries or frameworks linked with your application.
MTOUCH : error MT5202: Native linking failed. Please review the build log.
clang : error : linker command failed with exit code 1 (use -v to see invocation)
6 個の警告
4 エラー
試したこと
プラットフォームのアーキテクチャの食い違いでエラーが出ているようなのでReleseでビルドしていたのをRelease|ARMに変えてみたところコンパイルは通るようになりましたが、シミュレータでの動作確認ができません。
(iPhoneのシミュレータがx86_64のアーキテクチャしかないようなのですがそういうものなのでしょうか?iPhoneはARMではないのですか?)
iPhoneのシミュレータでこのアプリケーションを動作確認するにはどうしたらよいでしょうか?
補足情報(FW/ツールのバージョンなど)
iPhone実機なし
macOS Catalina v10.15.7
Visual Studio for mac v8.7.8(build 4)
Xcode v12.0.1(12A7300)
OpenCV v4.5.0
-
気になる質問をクリップする
クリップした質問は、後からいつでもマイページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
クリップを取り消します
-
良い質問の評価を上げる
以下のような質問は評価を上げましょう
- 質問内容が明確
- 自分も答えを知りたい
- 質問者以外のユーザにも役立つ
評価が高い質問は、TOPページの「注目」タブのフィードに表示されやすくなります。
質問の評価を上げたことを取り消します
-
評価を下げられる数の上限に達しました
評価を下げることができません
- 1日5回まで評価を下げられます
- 1日に1ユーザに対して2回まで評価を下げられます
質問の評価を下げる
teratailでは下記のような質問を「具体的に困っていることがない質問」、「サイトポリシーに違反する質問」と定義し、推奨していません。
- プログラミングに関係のない質問
- やってほしいことだけを記載した丸投げの質問
- 問題・課題が含まれていない質問
- 意図的に内容が抹消された質問
- 過去に投稿した質問と同じ内容の質問
- 広告と受け取られるような投稿
評価が下がると、TOPページの「アクティブ」「注目」タブのフィードに表示されにくくなります。
質問の評価を下げたことを取り消します
この機能は開放されていません
評価を下げる条件を満たしてません
質問の評価を下げる機能の利用条件
この機能を利用するためには、以下の事項を行う必要があります。
- 質問回答など一定の行動
-
メールアドレスの認証
メールアドレスの認証
-
質問評価に関するヘルプページの閲覧
質問評価に関するヘルプページの閲覧
checkベストアンサー
+1
シミュレータでも動かすには、スタティックライブラリをx86_64でもビルドする必要があります。arm64、x86_64両方対応したライブラリ(FATバイナリ)の作成は、lipo
コマンドを使用します。
xcodebuild -sdk iphoneos -project OpenCViOS.xcodeproj -configuration Release
xcodebuild -sdk iphonesimulator -project OpenCViOS.xcodeproj -arch x86_64 -configuration Release
lipo -create -output libOpenCViOS.a build/Release-iphoneos/libOpenCViOS.a build/Release-iphonesimulator/libOpenCViOS.a
投稿
-
回答の評価を上げる
以下のような回答は評価を上げましょう
- 正しい回答
- わかりやすい回答
- ためになる回答
評価が高い回答ほどページの上位に表示されます。
-
回答の評価を下げる
下記のような回答は推奨されていません。
- 間違っている回答
- 質問の回答になっていない投稿
- スパムや攻撃的な表現を用いた投稿
評価を下げる際はその理由を明確に伝え、適切な回答に修正してもらいましょう。
15分調べてもわからないことは、teratailで質問しよう!
- ただいまの回答率 88.10%
- 質問をまとめることで、思考を整理して素早く解決
- テンプレート機能で、簡単に質問をまとめられる
2020/10/01 13:58
ご指摘の方法でビルドいたしましたライブラリはXamarin.iosから参照してビルドが通り、シミュレータで動作しました。
-arch x86_64指定がXcodeのGUI上でどこになるのか、なぜx86_64を指定してiPhone向けのライブラリとリンクできるのかなどよくわかっていないことが多々ありますので一つずつ調べていこうと思います。
ありがとうございました。