[go: nahoru, domu]

Skip to content
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

Update description of Static vs Dynamic LIbraries on 'Binding to native code using dart:ffi' pages #5334

Open
Timmmm opened this issue Feb 20, 2021 · 8 comments
Labels
dev.ffi Relates to foreign function interface dev.integration Relates to platform integration e1-hours Effort: < 8 hrs from.page-issue Reported in a reader-filed concern lang.native-code Involves Swift, ObjC, Java, or Kotlin code for mobile apps p2-medium Necessary but not urgent concern. Resolve when possible. st.triage.ltw Indicates Lead Tech Writer has triaged target.Android Target apps on the Android Platform target.iOS Target apps on the iOS Platform

Comments

@Timmmm
Copy link
Timmmm commented Feb 20, 2021

Page URLs

Page source

https://github.com/flutter/website/tree/master/src/docs/development/platform-integration/c-interop.md

Description of issue

The introduction isn't very clear about static vs dynamic linking. It explains what they are - fine, I expect most people reading it already know - but then it just jumps into dynamic linking on Android and static linking on iOS with no explanation as to why.

I presume that dynamic linking is forbidden on iOS and static linking doesn't work on Android? Or something like that? I would be good to explain that.

@sfshaza2 sfshaza2 added e2-days Effort: < 5 days p2-medium Necessary but not urgent concern. Resolve when possible. labels Mar 10, 2021
@sfshaza2
Copy link
Contributor

Thoughts, @mit-mit?

@mit-mit
Copy link
Member
mit-mit commented Mar 11, 2021

Yes, I agree that this page would benefit from some elaboration. cc @dcharkes

@dcharkes
Copy link
Contributor

Similar issue flutter/flutter#75537.

I presume that dynamic linking is forbidden on iOS and static linking doesn't work on Android? Or something like that? I would be good to explain that.

We have not explored the non-documented ways. They might be possible. If they are, PRs are welcome to update the documentation.

We should update the page to reflect this. @sfshaza2 @mit-mit any suggestions for wording?

@sfshaza2
Copy link
Contributor

@chinmaygarde, do you have any insight into what we could say about this distinction?

@chinmaygarde
Copy link
Member

There is no magic in the way symbol resolution works for Dart FFI. The symbols need to be resolved from a library using dlsym. dlsym needs a library handle. This handle is obtained by using dlopen with a specified path to a dynamic library. If symbol is already present in the current global scope, RTLD_DEFAULT can be used instead.

In Dart code, DynamicLibrary.open() is used to obtain the handle from a path and DyanmicLibrary.process() for the RTLD_DEFAULT handle.

The specific guidance for iOS and Android differs because of the way the applications are packaged.

On iOS, there is an executable that needs to be built already and it is generally easier to link static libraries into this executable. Once in the executable, DynamicLibrary.process() will work fine. However, there is nothing preventing the user from resolving the symbols from a dynamic library. I created a sample project that demonstrates this https://github.com/chinmaygarde/flutter_dylinkios_sample. Notice that in the Build Phases, I explicitly specified that the library be linked. This way, the symbols from that library are available in the global scope and I can use DynamicLibrary.process. But, I didn't need to do that. I could have skipped that step and loaded the library myself at runtime using DynamicLibrary.open() with the path returned by [NSBundle exectuablePath] for the framework bundle. It would have achieved the same thing. Using static libraries on iOS avoids having to remember to link the library and make sure to embed it in the final bundle, or, figure out some way of finding where the dynamic library exists at runtime. But, as demonstrated, it is supported and works fine.
Screen Shot 2021-03-15 at 12 15 58 PM

Now, on Android, there no executable generated (the thing that contains the main entrypoint). So, we have to rely on dynamic libraries only on Android.

Hope this helps clarify things.

@sfshaza2
Copy link
Contributor

Wow, @chinmaygarde, thanks for that detailed answer!

@Timmmm
Copy link
Author
Timmmm commented Mar 15, 2021

Ah that makes sense. Although isn't Flutter AoT compiled now on Android? Does that not give a binary you can statically link into? I guess maybe it's just too much of a pain to do, and it wouldn't work with hot reload which would kind of suck.

@chinmaygarde
Copy link
Member

Right, it would be limited to specific modes. Also, the default Flutter workflow for Android doesn't require a native toolchain. So everyone would have to download the NDK or some other toolchain and have that versioned and updated. The assumption is that if you are writing native code, you already have a build system and toolchain in place for it that can generate a dynamic library.

@atsansone atsansone added from.page-issue Reported in a reader-filed concern dev.integration Relates to platform integration dev.ffi Relates to foreign function interface labels May 19, 2023
@atsansone atsansone added the lang.native-code Involves Swift, ObjC, Java, or Kotlin code for mobile apps label May 30, 2023
@atsansone atsansone changed the title 'Binding to native code using dart:ffi' page issue Update description of Static vs Dynamic LIbraries on 'Binding to native code using dart:ffi' pages Jun 2, 2023
@atsansone atsansone added target.iOS Target apps on the iOS Platform target.Android Target apps on the Android Platform e1-hours Effort: < 8 hrs st.triage.ltw Indicates Lead Tech Writer has triaged and removed e2-days Effort: < 5 days labels Jun 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dev.ffi Relates to foreign function interface dev.integration Relates to platform integration e1-hours Effort: < 8 hrs from.page-issue Reported in a reader-filed concern lang.native-code Involves Swift, ObjC, Java, or Kotlin code for mobile apps p2-medium Necessary but not urgent concern. Resolve when possible. st.triage.ltw Indicates Lead Tech Writer has triaged target.Android Target apps on the Android Platform target.iOS Target apps on the iOS Platform
Projects
None yet
Development

No branches or pull requests

6 participants