我正在尝试按照示例将 C 文件与 Flutter 应用程序链接。不幸的是,我发现的所有示例都只提供了完整工具链的一部分。目前,我遇到了上述错误,我尝试按照此处 Xcode 12, building for iOS Simulator, but linking in object file built for iOS, for architecture arm64 的建议删除 arm64,但这没有帮助。
我正在尝试遵循 https://dart.dev/guides/libraries/c-interop
中的“原始”示例我所做的(正如我所说,我正在关注多个示例,所以不确定这条链是否有意义):
(1) 我新建了一个flutter项目
import 'dart:ffi';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:native_add_bin/primitives.dart';
import 'dart:io' show Platform;
import 'package:ffi/ffi.dart';
// C sum function - int sum(int a, int b);
// Example of how to pass parameters into C and use the returned result
typedef sum_func = Int32 Function(Int32 a, Int32 b);
typedef Sum = int Function(int a, int b);
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _platformVersion = 'Unknown';
var dylib, sumPointer, sum;
final path = './primitives_library/libprimitives.dylib';
@override
void initState() {
dylib = DynamicLibrary.open(path);
// calls int sum(int a, int b);
sumPointer = dylib.lookup<NativeFunction<sum_func>>('sum');
sum = sumPointer.asFunction<Sum>();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Column(
children: [Text('Running on: $_platformVersion\n'),
Text('1 + 2 == ${sum(1, 2)}')
]
),
),
);
}
}
(2) 然后,我用示例中的 CMakeLists.txt
文件编译了 C 文件
$ cmake . -GXcode -DCMAKE_SYSTEM_NAME=iOS
$ cmake -S. -B_builds -GXcode -DCMAKE_SYSTEM_NAME=iOS "-DCMAKE_OSX_ARCHITECTURES=armv7;armv7s;arm64;i386;x86_64" -DCMAKE_OSX_DEPLOYMENT_TARGET=9.3 -DCMAKE_INSTALL_PREFIX=../_install -DCMAKE_XCODE_ATTRIBUTE_ONLY_ACTIVE_ARCH=NO -DCMAKE_IOS_INSTALL_COMBINED=YES
$ cmake --build _builds --config Debug --target install
(3) 在 Xcode 中,我已经为项目 Runner 和目标 Runner 排除了(架构)arm64,用于调试、配置文件和发布,并带有选项 Any iOS Simulator SDK。
对于第 (3) 点,我不确定是否需要为 Pod 项目重复此操作,因为我没有 Pod 文件。
(4) 我正在从 Intellij Idea 启动调试器并收到以下结果:
Xcode build done. 7,0s
Failed to build iOS app
Error output from Xcode build:
↳
** BUILD FAILED **
Xcode's output:
↳
note: Using new build system
note: Building targets in parallel
note: Planning build
note: Constructing build description
error: Building for iOS Simulator, but the linked and embedded library 'libmyproject.dylib' was built for iOS. (in target 'Runner' from project 'Runner')
我需要对这个 pod 文件做些什么还是我有其他问题?