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

[native_assets_cli]: Could not open the dependent shared library #1713

Open
lindeer opened this issue Nov 11, 2024 · 3 comments
Open

[native_assets_cli]: Could not open the dependent shared library #1713

lindeer opened this issue Nov 11, 2024 · 3 comments

Comments

@lindeer
Copy link
Contributor

lindeer commented Nov 11, 2024

similar with #190 , my Dart library (no flutter) had 2 .so files: liba.so and libb.so, created by Process.start('make', ...) and I put them all in buildConfig.outputDirectory, it passed compilation, but run with an exception Failed to load dynamic library '.dart_tool/native_assets_builder/3e4910e7689af6c76b0eb2007f74b95f/out/liba.so': libb.so: cannot open shared object file: No such file or directory.
How should I write my build.dart?

const packageName = 'my_pkg';
const _repoLibName = 'liba.so';
const _depLibName = 'libb.so';

Future<void> _builder(BuildConfig buildConfig, BuildOutput buildOutput) async {
  final pkgRoot = buildConfig.packageRoot;

  final proc = await Process.start(
    'make',
    [
      '-j',
      _repoLibName,
    ],
    workingDirectory: srcDir.path,
  );
  stderr.addStream(proc.stdout);

  final linkMode = _linkMode(buildConfig.linkModePreference);
  final libName = buildConfig.targetOS.libraryFileName(packageName, linkMode);

  Uri moveLib(String name, String target) {
    final uri = pkgRoot.resolve(p.join('src', name));
    final file = File.fromUri(uri).resolveSymbolicLinksSync();
    final targetUri = buildConfig.outputDirectory.resolve(target);
    File(file).renameSync(targetUri.path);
    return targetUri;
  }

  final libUri = moveLib(_repoLibName, libName);
  moveLib(_depLibName, _depLibName);

  buildOutput.addAsset(NativeCodeAsset(
    package: packageName,
    name: 'src/lib_$packageName.dart',
    linkMode: linkMode,
    os: buildConfig.targetOS,
    file: libUri,
    architecture: buildConfig.targetArchitecture,
  ));

  final src = [
    'src/src/my_pkg.cpp',
    'src/a/a.cpp',
    'src/b/b.cpp',
  ];

  buildOutput.addDependencies([
    ...src.map((s) => pkgRoot.resolve(s)),
    pkgRoot.resolve('build.dart'),
  ]);
}

pubspec.yaml:

dependencies:
  ffi: ^2.1.3
  native_assets_cli: ^0.8.0

dev_dependencies:
  ffigen: ^11.0.0
  lints: ^3.0.0
  test: ^1.21.0
@lindeer lindeer changed the title Could not open the dependent shared library [native_assets_cli]: Could not open the dependent shared library Nov 11, 2024
@dcharkes
Copy link
Collaborator

How should I write my build.dart?

Report both dynamic libraries with addAsset in hook/build.dart.

Though, since #190 is not fully supported yet, it still might not work after that.

@lindeer
Copy link
Contributor Author

lindeer commented Nov 19, 2024

@dcharkes any example code? not figure out 'report bot dynamic libraries', would have a try...

@dcharkes
Copy link
Collaborator

@dcharkes any example code? not figure out 'report bot dynamic libraries', would have a try...

https://github.com/dart-lang/native/tree/main/pkgs/native_assets_builder/test_data/native_dynamic_linking

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants