text stringlengths 1 372 |
|---|
this page focuses on compiling, packaging, |
and loading macOS native code within a flutter plugin or app. |
this tutorial demonstrates how to bundle C/C++ |
sources in a flutter plugin and bind to them using |
the dart FFI library on macOS. |
in this walkthrough, you’ll create a c function |
that implements 32-bit addition and then |
exposes it through a dart plugin named “native_add”. |
<topic_end> |
<topic_start> |
dynamic vs static linking |
a native library can be linked into an app either |
dynamically or statically. a statically linked library |
is embedded into the app’s executable image, |
and is loaded when the app starts. |
symbols from a statically linked library can be |
loaded using DynamicLibrary.executable or |
DynamicLibrary.process. |
a dynamically linked library, by contrast, is distributed |
in a separate file or folder within the app, |
and loaded on-demand. on macOS, the dynamically linked |
library is distributed as a .framework folder. |
a dynamically linked library can be loaded into |
dart using DynamicLibrary.open. |
API documentation is available from the dart dev channel: |
dart API reference documentation. |
<topic_end> |
<topic_start> |
create an FFI plugin |
if you already have a plugin, skip this step. |
to create a plugin called “native_add”, |
do the following: |
info note |
you can exclude platforms from --platforms that you don’t want |
to build to. however, you need to include the platform of |
the device you are testing on. |
this will create a plugin with C/C++ sources in native_add/src. |
these sources are built by the native build files in the various |
os build folders. |
the FFI library can only bind against c symbols, |
so in c++ these symbols are marked extern "c". |
you should also add attributes to indicate that the |
symbols are referenced from dart, |
to prevent the linker from discarding the symbols |
during link-time optimization. |
__attribute__((visibility("default"))) __attribute__((used)). |
on iOS, the native_add/macos/native_add.podspec links the code. |
the native code is invoked from dart in lib/native_add_bindings_generated.dart. |
the bindings are generated with package:ffigen. |
<topic_end> |
<topic_start> |
other use cases |
<topic_end> |
<topic_start> |
iOS and macOS |
dynamically linked libraries are automatically loaded by |
the dynamic linker when the app starts. their constituent |
symbols can be resolved using DynamicLibrary.process. |
you can also get a handle to the library with |
DynamicLibrary.open to restrict the scope of |
symbol resolution, but it’s unclear how apple’s |
review process handles this. |
symbols statically linked into the application binary |
can be resolved using DynamicLibrary.executable or |
DynamicLibrary.process. |
<topic_end> |
<topic_start> |
platform library |
to link against a platform library, |
use the following instructions: |
<topic_end> |
<topic_start> |
first-party library |
a first-party native library can be included either |
as source or as a (signed) .framework file. |
it’s probably possible to include statically linked |
archives as well, but it requires testing. |
<topic_end> |
<topic_start> |
source code |
to link directly to source code, |
use the following instructions: |
add the following prefix to the |
exported symbol declarations to ensure they |
are visible to dart: |
C/C++/Objective-C |
swift |
<topic_end> |
<topic_start> |
compiled (dynamic) library |
to link to a compiled dynamic library, |
use the following instructions: |
<topic_end> |
<topic_start> |
compiled (dynamic) library (macos) |
to add a closed source library to a |
flutter macOS desktop app, |
use the following instructions: |
<topic_end> |
<topic_start> |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.