1

I'm trying to link in a pre-compiled shared library file called librandomnumber.so in my project. code for CMakeLists.txt:

link_directories('lib/native_Library/librandomnumber.so')

add_library(native-lib librandomnumber.so)

target_link_libraries(native-lib librandomnumber.so)

load library code:

import 'dart:ffi';

import 'dart:io';



typedef fetch_number_func = Int32 Function();
typedef FetchNumber = int Function();



class FfiService {
  get updateLocationsCommand => null;
  Object fetchRandomNumber() {

      if (Platform.isAndroid){

        print("now going to load library");
        final DynamicLibrary nativeAddLib = Platform.isAndroid
            ? DynamicLibrary.open('native-lib.so')
            : DynamicLibrary.process();
        print("sdgahhgjhdgajh" + nativeAddLib.toString());
        final fetchNumberPointer =
        nativeAddLib.lookup<NativeFunction<fetch_number_func>>(
          'fetch_number',
        );
       
        return fetchNumberPointer;
      }
      else {
        (exc){
        DynamicLibrary.process();
        print('Something went wrong in fetchRandomNumber ${exc.toString()}');
      };
      }
      return 0;
  }


}

this error is shown

error: Invalid argument(s): Failed to load dynamic library 'native-lib.so': dlopen failed: library "native-lib.so" not found

can you please give me the solutions??

pub Dart
  • 19
  • 3
  • If you have a pre-compiled so, why do you have a CMakeLists? You'd probably only use that if you were compiling source. (Though, if you do have the source of the so, that might be the simplest solution - just include and compile it.) Answers like this https://stackoverflow.com/questions/60362243/how-to-import-a-prebuilt-so-library-and-call-it-from-the-new-project show how to include an so in your build so that it gets copied in, but you must then call `open` with the real name. (`native-lib.so` doesn't exists - you never built anything into it.) – Richard Heap Dec 23 '21 at 11:58
  • Another approach could be to put the so in assets, load it, write it to a temporary file and `open` that temporary file. As an aside, there are Dart features to generate random numbers, of course. – Richard Heap Dec 23 '21 at 12:00
  • can you please tell me the flow? – pub Dart Dec 23 '21 at 13:15
  • Need more details. Try something, edit question to show what you tried, where you got stuck. – Richard Heap Dec 23 '21 at 13:43
  • now i'm calling direct linked in open command, – pub Dart Dec 23 '21 at 13:46
  • final DynamicLibrary nativeAddLib = Platform.isAndroid ? DynamicLibrary.open('lib/native_Library/librandomnumber.so') : DynamicLibrary.process(); print("sdgahhgjhdgajh" + nativeAddLib.toString()); final fetchNumberPointer = nativeAddLib.lookup>( 'fetch_number', ); – pub Dart Dec 23 '21 at 13:47
  • Just because your so is in that folder in your source tree does NOT mean that it will end up in your build on the android device! See first two comments about possible ways to include it in the build. – Richard Heap Dec 23 '21 at 13:58
  • if you don't mind can you please sketch me the flow of your second approach , – pub Dart Dec 23 '21 at 14:03
  • Follow the normal instructions to package an asset ( https://docs.flutter.dev/development/ui/assets-and-images ), then this answer ( https://stackoverflow.com/questions/54427646/load-pdf-file-custom-file-from-flutter-assets/54427865#54427865 ) to load and save an asset as a temporary file, then `open` the temporary file's name. – Richard Heap Dec 23 '21 at 14:08
  • (I haven't ever tried this approach, and you may find that it violates a security policy, but worth a try before moving onto the other approaches.) – Richard Heap Dec 23 '21 at 14:10

0 Answers0