Dart C互操作性。将整数数组传递给C函数 [英] Dart C Interoperability. Pass an array of integers to C function

查看:293
本文介绍了Dart C互操作性。将整数数组传递给C函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正在学习Dart,并且正在玩Dart与C的互操作性。我可以将C方法与两个int参数一起使用。下面的代码:

I am learning Dart now and I playing with Dart's Interoperability with C. I am able to use a C method with two int params. Code below:

hello.dart

hello.dart

import 'dart:ffi' as ffi;

typedef sum_func = ffi.Int32 Function(ffi.Int32 a, ffi.Int32 b);
typedef Sum = int Function(int a, int b);
...
final dylib = ffi.DynamicLibrary.open(path);
final sumPointer = dylib.lookup<ffi.NativeFunction<sum_func>>('sum');

final sum = sumPointer.asFunction<Sum>();
print('3 + 5 = ${sum(3, 5)}');

hello.c

hello.c

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

hello.h

hello.h

int add(int x, int y)

hello.def

hello.def

LIBRARY   hello
EXPORTS
   sum

这一切都很好,但我还想拥有一个 max C方法,该方法将一个int数组作为输入并返回最大数。我怎样才能做到这一点?我已经用C实现了所有必需的代码,但不确定如何将其与Dart链接。

This all works really well, but I also want to have an max C method, which takes an int array as an input and returns the biggest number. How can I do this? I've implemented all the required code in C, but I am not sure how do I "link" it with Dart. Could anyone help me please?

推荐答案

首先,我真的想说我不是C程序员,尤其是当我来时指针,我什至不假装我对如何以最佳方式完成这种事情有充分的了解。

First, I really want to say that I am not a C programmer and especially when it comes to pointers I am not even pretending I have a fully understanding of how to do this kind of things the most optimal way.

有了这种解决方法,这就是我的解决方案基于此处找到的原始示例: https://github.com/ dart-lang / samples / tree / master / ffi / primitives

With this out of the way here is my solution based on the primitives example found here: https://github.com/dart-lang/samples/tree/master/ffi/primitives

primitives.dart

import 'dart:ffi';
import 'dart:io' show Platform;

import 'package:ffi/ffi.dart';

typedef max_func = Int32 Function(Pointer<Int32> list, Int32 size);
typedef Max = int Function(Pointer<Int32> list, int size);

void main() {
  var path = './primitives_library/libprimitives.so';
  if (Platform.isMacOS) path = './primitives_library/libprimtives.dylib';
  if (Platform.isWindows) path = r'primitives_library\Debug\primitives.dll';
  final dylib = DynamicLibrary.open(path);

  final list = [1, 5, 3, 59030, 131000, 0];
  final listPtr = intListToArray(list);

  final maxPointer = dylib.lookup<NativeFunction<max_func>>('max');
  final max = maxPointer.asFunction<Max>();
  print('${max(listPtr, list.length)}'); // 131000
  free(listPtr);
}

Pointer<Int32> intListToArray(List<int> list) {
  final ptr = allocate<Int32>(count: list.length);
  for (var i = 0; i < list.length; i++) {
    ptr.elementAt(i).value = list[i];
  }
  return ptr;
}

基元。h

int max(int *listPtr, int size);

基元。c

#include "primitives.h"

int max(int *listPtr, int size)
{
  int currentMax = *listPtr;

  for (int i = 0; i < size; i++)
  {
    if (currentMax < *listPtr)
    {
      currentMax = *listPtr;
    }
    listPtr++;
  }

  return currentMax;
}

这篇关于Dart C互操作性。将整数数组传递给C函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆