Click here to Skip to main content
15,041,788 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to call a native C function from Dart. I have made my C files, CMakeLists.txt and required Dart file. but when I run my code it shows:



Execution failed for task ':flutter_plugin3:generateJsonModelDebug'.
> C:\Users\Mubin\AndroidStudioProjects\flutter_plugin3\android\CMakeLists.txt : C/C++ debug|armeabi-v7a : CMake Error at C:\Users\Mubin\AndroidStudioProjects\flutter_plugin3\android\CMakeLists.txt:3 (add_library):
    Cannot find source file:

      hello.c

    Tried extensions .c .C .c++ .cc .cpp .cxx .m .M .mm .h .hh .h++ .hm .hpp
    .hxx .in .txx


* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
"


re.png - Google Drive[^]

What I have tried:

My CMakeLists:

cmake_minimum_required(VERSION 3.7 FATAL_ERROR)
project(hello_library VERSION 1.0.0 LANGUAGES C)
add_library(hello_library SHARED hello.c hello.def)
add_executable(hello_test hello.c)

set_target_properties(hello_library PROPERTIES
    PUBLIC_HEADER hello.h
    VERSION ${PROJECT_VERSION}
    SOVERSION 1
    OUTPUT_NAME "hello"
    XCODE_ATTRIBUTE_CODE_SIGN_IDENTITY "Hex_Identity_ID_Goes_Here"
)



Dart code:

Dart
import 'dart:ffi' as ffi;
import 'dart:io' show Platform, Directory;
import 'package:path/path.dart' as path;
// For Platform.isX
import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart';

class FlutterPlugin3 {
  static const MethodChannel _channel =
      const MethodChannel('flutter_plugin3');

  static Future<String> get platformVersion async {
    final String version = await _channel.invokeMethod('getPlatformVersion');
    return version;
  }
}


  // Open the dynamic library
typedef hello_world_func = ffi.Void Function();
// Dart type definition for calling the C foreign function
typedef HelloWorld = void Function();



 greet() {
   // Open the dynamic library
   var libraryPath = path.join(Directory.current.path, 'hello_library',
       'libhello.so');
   if (Platform.isMacOS) {
     libraryPath = path.join(Directory.current.path, 'hello_library',
         'libhello.dylib');
   } else if (Platform.isWindows) {
     libraryPath = path.join(Directory.current.path, 'hello_library',
         'Debug', 'hello.dll');
   }

   final dylib = ffi.DynamicLibrary.open(libraryPath);

   // Look up the C function 'hello_world'
   final HelloWorld hello = dylib
       .lookup<ffi.NativeFunction<hello_world_func>>('hello_world')
       .asFunction();


   // Call the function
   hello();
 }
Posted
Updated 8-May-21 4:15am
v2
Comments
Richard MacCutchan 8-May-21 9:38am
   
The message is quite clear as to which file it cannot find.
Md. Mubin 8-May-21 10:07am
   
the file which containing the C code and the desired function.

here's the link of the image: https://drive.google.com/file/d/1Yu8t4J2oMFcj9P4zyvP4WwD-OBiJt7e8/view?usp=sharing

I have also edited it in my question.
Richard MacCutchan 8-May-21 10:34am
   
That does not alter the fact that the process cannot find it. I do not know flutter so I cannot suggest anything. Check the settings and rules for your make file.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900