flutter 调用c++,rust

简介: flutter 调用c++,rust
环境

Python 3.11.0

rustc 1.68.0 (2c8cc3432 2023-03-06)

Mac

调用流程

c++或rust

anoidrd生成so文件, ios生成.a文件

通过ffi间接调用

flutter 调用.cpp
native_lib.cpp
#include <stdint.h>
#include <cstring>
extern "C" {
    __attribute__((visibility("default"))) __attribute__((used))
    int32_t native_add(int32_t x, int32_t y) {
        return x + y;
    }
     __attribute__((visibility("default"))) __attribute__((used))
    int32_t native_add2(int32_t x, int32_t y) {
            return x + y;
    }
     __attribute__((visibility("default"))) __attribute__((used))
    char* concat(const char* str1, const char* str2) {
        int len1 = strlen(str1);
        int len2 = strlen(str2);
        char* result = new char[len1 + len2 + 1];
        strcpy(result, str1);
        strcat(result, str2);
        return result;
    }
}


extern "C" __attribute__((visibility("default"))) __attribute__((used))
int32_t native_add3(int32_t x, int32_t y) {
        return x + y;
 }

新建android\CMakeLists.txt
 cmake_minimum_required(VERSION 3.18.1) # for example
 add_library(native_lib
        # Sets the library as a shared library.
        SHARED
        # Provides a relative path to your source file(s).
        ../ios/Runner/native_lib.cpp )
app/build.gradle
    externalNativeBuild {
        // Encapsulates your CMake build configurations.
        cmake {
            // Provides a relative path to your CMake build script.
            path "../CMakeLists.txt"
        }
    }
local.properties

ndk.dir=C:\sdk\androidsdk\Android\Sdk\ndk\24.0.8215888

native_add.dart
import 'dart:ffi' as ffi;
import 'dart:ffi';
import 'dart:io'; // For Platform.isX

import 'package:ffi/ffi.dart';

final DynamicLibrary nativeAddLib = Platform.isAndroid
    ? DynamicLibrary.open("libnative_lib.so")
    : DynamicLibrary.process();


final int Function(int x, int y) nativeAdd = nativeAddLib
    .lookup<NativeFunction<Int32 Function(Int32, Int32)>>("native_add")
    .asFunction();


final int Function(int x, int y) nativeAdd2 = nativeAddLib
    .lookup<NativeFunction<Int32 Function(Int32, Int32)>>("native_add2")
    .asFunction();

final int Function(int x, int y) nativeAdd3 = nativeAddLib
    .lookup<NativeFunction<Int32 Function(Int32, Int32)>>("native_add3")
    .asFunction();

typedef A2 = Pointer<Utf8> Function(ffi.Pointer<Utf8>, ffi.Pointer<Utf8>);

String concatStr(String str1, String str2) {
  final privateKeyPtr = str1.toNativeUtf8();
  final hexCidMessagePtr = str2.toNativeUtf8();
  final concat =
      nativeAddLib.lookup<ffi.NativeFunction<A2>>('concat').asFunction<A2>();
  return concat(privateKeyPtr, hexCidMessagePtr).toDartString();
}


bool isExistSymbol(String symbol) {
  final isOk = nativeAddLib.providesSymbol(symbol);
  return isOk;
}

flutter 调用rust
创建 flutter项目或者插件
添加依赖

flutter_rust_bridge: ^1.71.0

ffi: ^2.0.1

ffigen: ^7.2.8

在根目录下创建rust项目

cargo new --lib native

拷贝api.rs,lib.rs

//lib.rs
mod api;
mod bridge_generated;

//api.ts
// This is the entry point of your Rust library.
// When adding new code to your project, note that only items used
// here will be transformed to their Dart equivalents.

// A plain enum without any fields. This is similar to Dart- or C-style enums.
// flutter_rust_bridge is capable of generating code for enums with fields
// (@freezed classes in Dart and tagged unions in C).
pub enum Platform {
    Unknown,
    Android,
    Ios,
    Windows,
    Unix,
    MacIntel,
    MacApple,
    Wasm,
}

// A function definition in Rust. Similar to Dart, the return type must always be named
// and is never inferred.
pub fn platform() -> Platform {
    // This is a macro, a special expression that expands into code. In Rust, all macros
    // end with an exclamation mark and can be invoked with all kinds of brackets (parentheses,
    // brackets and curly braces). However, certain conventions exist, for example the
    // vector macro is almost always invoked as vec![..].
    //
    // The cfg!() macro returns a boolean value based on the current compiler configuration.
    // When attached to expressions (#[cfg(..)] form), they show or hide the expression at compile time.
    // Here, however, they evaluate to runtime values, which may or may not be optimized out
    // by the compiler. A variety of configurations are demonstrated here which cover most of
    // the modern oeprating systems. Try running the Flutter application on different machines
    // and see if it matches your expected OS.
    //
    // Furthermore, in Rust, the last expression in a function is the return value and does
    // not have the trailing semicolon. This entire if-else chain forms a single expression.
    if cfg!(windows) {
        Platform::Windows
    } else if cfg!(target_os = "android") {
        Platform::Android
    } else if cfg!(target_os = "ios") {
        Platform::Ios
    } else if cfg!(all(target_os = "macos", target_arch = "aarch64")) {
        Platform::MacApple
    } else if cfg!(target_os = "macos") {
        Platform::MacIntel
    } else if cfg!(target_family = "wasm") {
        Platform::Wasm
    } else if cfg!(unix) {
        Platform::Unix
    } else {
        Platform::Unknown
    }
}

// The convention for Rust identifiers is the snake_case,
// and they are automatically converted to camelCase on the Dart side.
pub fn rust_release_mode() -> bool {
    cfg!(not(debug_assertions))
}

pub fn test() -> String {
    String::from("这是一个rust函数")
}

配置

Cargo.toml

[package]
name = "native"
version = "0.1.0"
edition = "2021"

[lib]
name = "native"
crate-type = ["staticlib", "cdylib"]

[build-dependencies]
flutter_rust_bridge_codegen = "=1.71.0"

[dependencies]
flutter_rust_bridge = "=1.71.0"
flutter_rust_bridge_macros = "=1.71.0"


添加run_gen.sh
flutter_rust_bridge_codegen \
        --rust-input native/src/api.rs \
        --dart-output lib/bridge_generated.dart \
        --c-output ios/Classes/bridge_generated.h
其他命令

cargo install flutter_rust_bridge_codegen

android相关

cargo install cargo-ndk

rustup target add aarch64-linux-android

rustup target add armv7-linux-androideabi

rustup target add x86_64-linux-android

rustup target add i686-linux-android

ios相关

64 bit targets (真机 & 模拟器):

rustup target add aarch64-apple-ios x86_64-apple-ios

New simulator target for Xcode 12 and later

ustup target add aarch64-apple-ios-sim

gradle.properties app/build.gradle

ANDROID_NDK=/Users/yujunlong/Library/Android/sdk/ndk/24.0.8215888


[
        Debug: null,
        Profile: '--release',
        Release: '--release'
].each {
    def taskPostfix = it.key
    def profileMode = it.value
    tasks.whenTaskAdded { task ->
        if (task.name == "javaPreCompile$taskPostfix") {
            task.dependsOn "cargoBuild$taskPostfix"
        }
    }
    tasks.register("cargoBuild$taskPostfix", Exec) {
        workingDir "../../native"
        environment ANDROID_NDK_HOME: "$ANDROID_NDK"
        commandLine 'cargo', 'ndk',
                // the 2 ABIs below are used by real Android devices
                '-t', 'armeabi-v7a',
                '-t', 'arm64-v8a',
                '-t', 'x86',
                '-t', 'x86_64',
                '-o', '../android/app/src/main/jniLibs', 'build'
        if (profileMode != null) {
            args profileMode
        }
    }
}
ios 配置

在native(rust项目下面)下面执行

cargo xcode

在 Xcode 中打开 ios/Runner.xcodeproj

xcode中选中Runner 然后File —> Add Files to “Runner”

native.xcodeproj

点击 Runner 根项目,TARGETS —> Build Phases —> Target Dependencies :请添加 native-staticlib

展开 Link Binary With Libraries:添加 libnative_static.a

Runner-Bridging-Header.h
#import "GeneratedPluginRegistrant.h"
#import "bridge_generated.h"

AppDelegate.swift 添加

print(“dummy_value=(dummy_method_to_enforce_bundling())”);

import UIKit
import Flutter

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
     print("dummy_value=\(dummy_method_to_enforce_bundling())");
    GeneratedPluginRegistrant.register(with: self)
    return super.application(application,       didFinishLaunchingWithOptions: launchOptions)
  }
}

相关文章
|
4月前
|
Rust 资源调度 安全
为什么使用 Rust over C++ 进行 IoT 解决方案开发
为什么使用 Rust over C++ 进行 IoT 解决方案开发
144 7
|
6月前
|
Rust 安全 C++
系统编程的未来之战:Rust能否撼动C++的王座?
【8月更文挑战第31天】Rust与C++:现代系统编程的新选择。C++长期主导系统编程,但内存安全问题频发。Rust以安全性为核心,通过所有权和生命周期概念避免内存泄漏和野指针等问题。Rust在编译时确保内存安全,简化并发编程,其生态系统虽不及C++成熟,但发展迅速,为现代系统编程提供了新选择。未来有望看到更多Rust驱动的系统级应用。
97 1
|
6月前
|
Rust 安全 Java
Java代码规范--排版,命名.:Rust能否撼动C++的王座?
系统编程是计算机科学的核心,C++长期占据主导地位,但其内存安全问题备受诟病。Rust以安全性为核心,通过所有权和生命周期概念避免了野指针和内存泄漏。此外,Rust的并发模型和日益丰富的生态系统使其成为现代系统编程的新选择,尤其在安全性和并发性方面表现出色。尽管C++依然强大,但Rust为开发者提供了更安全、易管理的选项,未来有望推动更多系统级应用的发展。
37 0
|
6月前
|
Rust 安全 C++
游戏引擎的未来:是Rust成为新王,还是C++仍占鳌头?
【8月更文挑战第31天】随着游戏行业的快速发展,对高性能、安全且易维护的游戏引擎需求日益增长。虽然C++长期占据主导地位,但Rust语言凭借其内存安全和高性能的特点,逐渐成为游戏引擎开发的新选择。Rust通过所有权机制和强大的类型系统,在保证内存安全的同时实现了与C++相当的性能,有助于提前发现潜在错误。尽管Rust在生态系统成熟度和学习曲线上仍面临挑战,其在游戏引擎领域的潜力正逐渐被认可。随着Rust社区的发展和工具链的完善,Rust有望成为游戏引擎开发的重要选项。
298 0
|
7月前
|
Web App开发 Rust 分布式计算
Rust与C++的区别及使用问题之对于大量使用C++实现的产品来说,迁移到Rust的问题如何解决
Rust与C++的区别及使用问题之对于大量使用C++实现的产品来说,迁移到Rust的问题如何解决
|
7月前
|
Rust 安全 编译器
Rust与C++的区别及使用问题之Rust中的bound check对性能产生影响的问题如何解决
Rust与C++的区别及使用问题之Rust中的bound check对性能产生影响的问题如何解决
|
7月前
|
Rust 测试技术 编译器
Rust与C++的区别及使用问题之Rust项目中组织目录结构的问题如何解决
Rust与C++的区别及使用问题之Rust项目中组织目录结构的问题如何解决
|
7月前
|
Rust 安全 程序员
Rust与C++的区别及使用问题之Rust解决多线程下的共享的问题如何解决
Rust与C++的区别及使用问题之Rust解决多线程下的共享的问题如何解决
|
7月前
|
Rust 编译器 程序员
Rust与C++的区别及使用问题之Rust避免多线程中的lifetime的问题如何解决
Rust与C++的区别及使用问题之Rust避免多线程中的lifetime的问题如何解决
|
7月前
|
Rust 编译器 测试技术
Rust与C++的区别及使用问题之Rust中函数参数传递的问题如何解决
Rust与C++的区别及使用问题之Rust中函数参数传递的问题如何解决