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

Invalid argument(s): Failed to load dynamic library 'cpp_library.dylib': dlopen failed: library "cpp_library.dylib" not found #489

Open
Okladnoj opened this issue Mar 6, 2023 · 12 comments

Comments

@Okladnoj
Copy link

Okladnoj commented Mar 6, 2023

image

I make library:

okji@Mac-mini-Dmytrij build % cmake ../
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/okji/self/flutter/vote_race/cpp/build
okji@Mac-mini-Dmytrij build % make
[ 33%] Building CXX object CMakeFiles/cpp_library.dir/src/add.cpp.o
[ 66%] Building CXX object CMakeFiles/cpp_library.dir/src/subtract.cpp.o
[100%] Linking CXX shared library cpp_library.dylib
[100%] Built target cpp_library
okji@Mac-mini-Dmytrij build % 

MR from repo:

https://github.com/Okladnoj/vote_race/pull/1/files

Use

 CppWrapper get cppWrapper {
    final nativeLib = Platform.isAndroid
        ? DynamicLibrary.open('cpp_library.dylib')
        : DynamicLibrary.process();

    return CppWrapper(nativeLib);
  }

adb shell getprop ro.product.cpu.abi

arm64-v8a

uname -m

arm64

@dcharkes
Copy link
Collaborator

dcharkes commented Mar 7, 2023

@Okladnoj nice to meet you!

Please use flutter create --template=plugin_ffi --platform=macos,ios,android,windows,linux it already has the correct setup for building C code and bundling it so that opening the dylib succeeds at runtime.

Side note: does this issue have anything to do with package:ffigen? Or is this a general dart:ffi issue?

@Okladnoj
Copy link
Author

Okladnoj commented Mar 7, 2023

using template is too cumbersome.

to be honest, I don’t know what the problem is, like the library (cpp_library.dylib) was able to be generated, but it doesn’t work

@mahesh-hegde
Copy link
Contributor

You are building on mac, in a mac format (dylib). But apparently trying to run on android? (as seen from adb command).

Android libraries are in .so format. It's built by NDK during APK build, and building it yourself is not easy.

So, just use template.

@Okladnoj
Copy link
Author

somehow not a very convenient solution ...

it turns out that you cannot add a library in C ++ to an existing project

@Jos-Sea
Copy link

Jos-Sea commented Apr 2, 2023

@Okladnoj nice to meet you!

Please use flutter create --template=plugin_ffi --platform=macos,ios,android,windows,linux it already has the correct setup for building C code and bundling it so that opening the dylib succeeds at runtime.

Side note: does this issue have anything to do with package:ffigen? Or is this a general dart:ffi issue?

@dcharkes
I'm trying to create a FFI plugin. I have created the bindings and have a .dylib file, but I don't know how to configure qnd bundle the .dylib file ?

@dcharkes
Copy link
Collaborator

dcharkes commented Apr 3, 2023

@dcharkes
I'm trying to create a FFI plugin. I have created the bindings and have a .dylib file, but I don't know how to configure qnd bundle the .dylib file ?

In Flutter, the bundling of the C source code in the template is done automatically. If you have a prebuilt dylib, you'll need to modify the build files. For MacOS and iOS it's the <package_name>.podspec file with vendored_libraries:

MacOS:

#
# To learn more about a Podspec see http://guides.cocoapods.org/syntax/podspec.html.
# Run `pod lib lint mylib_dylib.podspec` to validate before publishing.
#
Pod::Spec.new do |s|
  s.name             = 'mylib_dylib'
  s.version          = '0.0.1'
  s.summary          = 'A new flutter plugin project.'
  s.description      = <<-DESC
A new flutter plugin project.
                       DESC
  s.homepage         = 'http://example.com'
  s.license          = { :file => '../LICENSE' }
  s.author           = { 'Your Company' => 'email@example.com' }
  s.source           = { :path => '.' }
  s.source_files     = 'Classes/**/*'
  s.dependency 'FlutterMacOS'
  s.platform = :osx, '10.11'
  s.pod_target_xcconfig = { 'DEFINES_MODULE' => 'YES' }
  s.vendored_libraries = 'Frameworks/libmylib_dylib.dylib', 'Frameworks/libmylib_dylib_dependency.dylib'
  s.swift_version = '5.0'
end

@Jos-Sea
Copy link

Jos-Sea commented Apr 3, 2023

@dcharkes Does modifying the build files mean adding a .dylib file to the example app using Xcode? Also, where should the .dylib file to be placed when configuring with the <package_name>.podspec file ?

@dcharkes
Copy link
Collaborator

dcharkes commented Apr 3, 2023

Not the example app, but the plugin.

It should be placed in the Frameworks/ folder e.g. mylib_dylib/macos/Frameworks/libmylib_dylib.dylib.

@Jos-Sea
Copy link

Jos-Sea commented Apr 4, 2023

@dcharkes I tried adding the 'my_dylib.dylib' file to the 'plugin/ios/Frameworks/mylib.dylib' folder and made changes to the podspec accordingly. but .dylib file is not loaded in the example application bundle.

# To learn more about a Podspec see http://guides.cocoapods.org/syntax/podspec.html.
# Run `pod lib lint mydylib.podspec` to validate before publishing.
#
Pod::Spec.new do |s|
  s.name             = 'mydylib'
  s.version          = '0.0.1'
  s.summary          = 'A new Flutter project.'
  s.description      = <<-DESC
A new Flutter project.
                       DESC
  s.homepage         = 'http://example.com'
  s.license          = { :file => '../LICENSE' }
  s.author           = { 'Your Company' => 'email@example.com' }

  # This will ensure the source files in Classes/ are included in the native
  # builds of apps using this FFI plugin. Podspec does not support relative
  # paths, so Classes contains a forwarder C file that relatively imports
  # `../src/*` so that the C sources can be shared among all target platforms.
  s.source           = { :path => '.' }
  s.source_files = 'Classes/**/*'
  s.dependency 'Flutter'
  s.platform = :ios, '9.0'
  s.vendored_libraries = 'Frameworks/mylib.dylib',

  # Flutter.framework does not contain a i386 slice.
  s.pod_target_xcconfig = { 'DEFINES_MODULE' => 'YES', 'EXCLUDED_ARCHS[sdk=iphonesimulator*]' => 'i386' }
  s.swift_version = '5.0'
end

@shoumo680
Copy link

i have the same problem

@dodgog
Copy link

dodgog commented Aug 31, 2023

same here! @Jos-Sea, how did you get around the issue?

@liamappelbe liamappelbe transferred this issue from dart-archive/ffigen Nov 15, 2023
@Sese-Schneider
Copy link

I face the same issue.
@shoumo680 @dodgog @Jos-Sea did you find any way to solve the issue?

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

7 participants