-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Does FFI support iOS static library? #44328
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
/cc @dcharkes |
Hi @JackPanda8,
Yes, following the steps from the documentation locally works for me. Please take a look at flutter/flutter#62666 (comment) And please double check that Xcode statically links the C code:
https://flutter.dev/docs/development/platform-integration/c-interop#step-2-add-cc-sources If the problem still persists, please provide a minimal reproduction. |
Here are the necessary files need to to replicate |
Any update or comment? |
Hi @bitsydarel, I have not yet had time to look into this. The last time I tried to do static linking for iOS and MacOS I ran into similar problems. I ended up using dynamic libraries instead. Does using dynamic libraries work for you in the mean time? (Until I have time to dig into this.) |
Hi @dcharkes not really as those libraries are provided to us, and I have not seen usecase of creating fat library as dynamic libraries (for simulator and device). |
@dcharkes Does it have to do with how dart lookup for library ? Is there anyway I could help to speed up the résolution of this issue ? As it’s one of our blocker in an ongoing project. |
I don't have access to this. Could you upload it is a GitHub repository instead?
I have succeeded in looking up symbols of a statically linked library by creating the podspec of the framework as the following.
Modify Pod::Spec.new do |s|
s.name = 'mylib_staticlib'
s.version = '0.0.1'
s.summary = 'A new flutter plugin project.'
s.description = <<-DESC
A new flutter plugin project.
DESC
s.homepage = 'http://example.com'
s.license = { :file => '../LICENSE' }
s.author = { 'Your Company' => '[email protected]' }
s.source = { :path => '.' }
s.source_files = 'Classes/**/*'
s.dependency 'FlutterMacOS'
s.platform = :osx, '10.11'
s.pod_target_xcconfig = { 'DEFINES_MODULE' => 'YES' }
s.vendored_libraries = 'Frameworks/libmylib_staticlib.a'
s.pod_target_xcconfig = { "OTHER_LDFLAGS" => "-force_load $(PODS_TARGET_SRCROOT)/Frameworks/libmylib_staticlib.a" }
s.swift_version = '5.0'
end And copy the static library in to For iOS, a similar approach.
Pod::Spec.new do |s|
s.name = 'mylib_staticlib'
s.version = '0.0.1'
s.summary = 'A new flutter plugin project.'
s.description = <<-DESC
A new flutter plugin project.
DESC
s.homepage = 'http://example.com'
s.license = { :file => '../LICENSE' }
s.author = { 'Your Company' => '[email protected]' }
s.source = { :path => '.' }
s.source_files = 'Classes/**/*'
s.dependency 'Flutter'
s.platform = :ios, '8.0'
# Flutter.framework does not contain a i386 slice.
s.pod_target_xcconfig = { 'DEFINES_MODULE' => 'YES', 'EXCLUDED_ARCHS[sdk=iphonesimulator*]' => 'i386' }
s.vendored_libraries = 'Frameworks/libmylib_staticlib.a'
s.pod_target_xcconfig = { "OTHER_LDFLAGS" => "-force_load $(PODS_TARGET_SRCROOT)/Frameworks/libmylib_staticlib.a" }
s.swift_version = '5.0'
end And the fat static library (arm64 for device and x64 for simulator) in So the 2 important lines in the podspec: s.vendored_libraries = 'Frameworks/libmylib_staticlib.a'
s.pod_target_xcconfig = { "OTHER_LDFLAGS" => "-force_load $(PODS_TARGET_SRCROOT)/Frameworks/libmylib_staticlib.a" } The first statically links the static library, and the second line is the correct invocation to tell the linker to not strip the symbols. |
Here's the repository with the necessary files |
@dcharkes Would be nice to have this test on iOS and on template=app |
To troubleshoot your problem, we need a complete flutter project, not just the static libraries and header files. Create a new one with |
You are always going to need a |
@dcharkes So basically meaning adding it in an application directly won't do ? is there a reason for the why the app can't create those build configuration and if possible what are those build configuration ? |
One could possibly. I am not familiar with the how the Flutter app/plugin builds are setup to tell you how. A good starting point for reverse engineering that would be to take a look at what files are generated when doing Any specific reason why you don't want to have a Flutter app + Flutter plugin right next to it in the same repository? And then have a |
@dcharkes project pushed, also added the lookup functions and on the podspec file also added the required setup as suggested. https://github.com/bitsydarel/dart_ffi_static_link_issue |
|
Your reproduction doesn't work here. The static library is missing symbols. Did you mean to include a static library with the standard c/c++ libs as well?
|
@dcharkes if you check the android pdfium folder, you will see other libraries that the library would need |
https://github.com/bitsydarel/dart_ffi_static_link_issue/tree/main/pdfium/android Those are all shared libraries, not static libraries, and not compiled in a way MacOS/iOS can understand them. Please provide a working reproduction. |
Hi @bitsydarel, This is simple. Add on "OTHER_LDFLAGS" the link to "c++" as i showed before in xcode sample. File: dart_ffii_static_link_issue.podspec
|
Hi @paulo-coutinho compilation now's working but it's still can't find the symbol @dcharkes Now the sample is reproducible, i pushed the changes so you could check if it's dart that can't load the symbols. Thank you guys for the help! |
How do you testing it? |
When you run the app if you check the console you should see logs about about the running app. |
Hi, Im investigating the generated binary from the Runner app and Xcode don't link with it:
And i make the same thing with that my sample using the same library and it is linked correctly:
The library is not the problem, but the link process. |
You can use any library name in podspec that it don't will generate error and will open the app:
So you problem is masked. |
If your sample was using podspec, how would you link the library ? |
It is easy. I made it work here: You need:
|
@dcharkes any update because the suggested changes by @paulo-coutinho does actually make the code compile and also make it available in swift or objective c code. But dart can't see it... Latest configuration: s.libraries = ["c++", "z"] Flutter.framework does not contain a i386 slice.s.pod_target_xcconfig = { 'DEFINES_MODULE' => 'YES', 'EXCLUDED_ARCHS[sdk=iphonesimulator*]' => 'i386' } |
Hi @bitsydarel, I am also struggling with the exact same issue, for PDFium too! Did you make any progress since your last message? Jean-Marie |
hi, @jmgeffroy Waiting on @dcharkes, because it’s seems to be a dart ffi issue as swift and obj can see and use the library. |
Hi @bitsydarel, |
@bitsydarel I am able to look up symbols fine on iOS/MacOS with a small static library built by CMake using the podspec from #44328 (comment). I presume either the static library is built in a different way, or your project setup is not the same. I haven't found the time to dig into your repo. I'll see if I can upload my working repro somewhere so you can work from there. |
@dcharkes strangely we tried with other libraries that have dependency with other c/c++ libraries and combined the libraries using lipo. Dart can’t see the symbols but rust and swift can, is there something different in how dart look for symbols for static libraries ? |
Dart just uses |
Without additional information, we are unfortunately not sure how to resolve this issue. We are therefore reluctantly going to close this bug for now. Please don't hesitate to comment on the bug if you have any more information for us; we will reopen it right away! |
any solution? |
I have the same issue, how to add Closed-source third-party library, such as libskrmc.a and skrmc.h by using ffigen for ios app |
as the official doc :
Symbols from a statically linked library can be loaded using DynamicLibrary.executable or DynamicLibrary.process.
but i got the error: Invalid argument(s): Failed to lookup symbol (dlsym(RTLD_DEFAULT, pause_all_task): symbol not found)
-------------here is my code--------------------------------
The text was updated successfully, but these errors were encountered: