getDyLibModule function

DynamicLibrary getDyLibModule(
  1. List<String> module
)

You can use the getDyLibModule function to open the dylib defined in the assets field of pubsepc.yaml.

For example: clang code

int add(int a, int b) {
  return a + b;
}

// gcc -shared -fPIC -o libadd.dll main.c     (Windows)
// gcc -shared -fPIC -o libadd.so main.c      (Linux)
// clang -shared -fPIC -o libadd.dylib main.c (MacOS)

// Finally: {PROJECT_ROOT}/assets/libadd.dll (must be declared in `pubsepc.yaml` file)

Dart code

import 'dart:ffi' as ffi;
import 'package:call/call.dart';

typedef FuncNative = ffi.Int32 Function(ffi.Int32, ffi.Int32);
typedef FuncDart = int Function(int, int);

var dll = getDyLibModule('assets/libadd.dll');
var add = dll.lookupFunction<FuncNative, FuncDart>('add');

print(add(999, 54639));     // Output: 55638

Implementation

ffi.DynamicLibrary getDyLibModule(List<String> module) {
  String p = '';
  if (Platform.isWindows) {
    p = Windows().getCurrentPath();
  } else if (Platform.isLinux) {
    p = Linux().getCurrentPath();
  } else if (Platform.isMacOS) {
    p = Macos().getCurrentPath();
  } else {
    throw 'The version lib doesn\'t support the platform';
  }
  for (String element in module) {
    p = join(p, element);
  }
  File f = File(p);
  if (f.existsSync()) {
    print("important message: find dll in $p");
    return ffi.DynamicLibrary.open(p);
  } else {
    print("important message: there is no dll in $p . please check.");
    return ffi.DynamicLibrary.process();
  }
}