0

Since a longer time now it is possible to open a DynamicLibrary (dylib, dll, so) in Flutter. Those libraries are written in C or C++.

I've now tried to build a basic dart command line application, compiled it using dart compile exe and tried to load it in my Flutter application using DynamicLibrary.open(), as you would do with the native libraries in C/C++.

typedef HelloWorldFunc = Void Function();
typedef HelloWorld = void Function();

...

final dynLib = DynamicLibrary.open('/path/to/cli.exe');
final HelloWorld func = dynLib.lookup<NativeFunction<HelloWorldFunc>>('hello_world').asFunction();
func();

(I've followed this tutorial and just added an empty void function called hello_world https://dart.dev/tutorials/server/get-started#3-create-a-small-app)

But the symbol could not be found:

Failed to lookup symbol 'hello_world': dlsym(0x7fec2310e5a0, hello_world): symbol not found

Question

Is it generally possible to open dart-compiled libraries in Flutter, like DLLs written in C++? Since dart compile exe generates native machine code as well

If yes, how?

Thanks!

Richard Heap
  • 48,344
  • 9
  • 130
  • 112
  • 3
    An `exe` isn't a shared object / dll - so it's not going to export methods in the way a dll does. If you want to call Dart code from Dart code you'd want to put it in a package and `import` it. – Richard Heap Feb 05 '22 at 17:42

1 Answers1

0

Dart cannot create shared libraries like other languages can do because it needs to be run in an embedder/DartVM.

This issue has a good explanation:

https://github.com/dart-lang/sdk/issues/37480