美文网首页
用Rust开发iOS静态库

用Rust开发iOS静态库

作者: 和谐共处 | 来源:发表于2021-01-06 11:18 被阅读0次

添加架构目标

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

#创建目录和库项目
mkdir rust_on_ios && cd rust_on_ios
cargo new rs --lib

编写lib.rs

use std::os::raw::{c_char};
use std::ffi::{CString};
#[no_mangle]
//#[no_mangle] 来告诉编译器不要破坏函数名,确保我们的函数名称被导入到 C 文件。
pub extern fn say_hello()-> *mut c_char{
    CString::new("Hello Rust").unwrap().into_raw()
}

#[cfg(test)]
mod tests {
    #[test]
    fn it_works() {
        assert_eq!(2 + 2, 4);
    }
}

手动添加头文件,可使用cbindgen自动创建,往下看

//hello.h

#include <stdio.h>
//兼容C++
#if defined (__cplusplus)
extern "C" {

//导出的行数名写在这里
char *say_hello(void);

#endif

#if defined (__cplusplus)
}
#endif

修改配置文件Cargo.toml

[package]
name = "hello"
version = "0.1.0"
authors = ["hhq <1710308677@q.com>"]
edition = "2018"
publish = false

#指定库名称和类型
[lib]
name = "hello"
crate-type = ["staticlib"]

编译库

  • target/aarch64-apple-ios:真机库
  • target/x86_64-apple-ios:模拟器库
  • target/universal:通用库
#编译指定架构库
# cargo build --target x86_64-apple-ios --release
#编译通用库
sudo cargo install cargo-lipo
cargo lipo --release
#生成头文件
sudo cargo install --force cbindgen

配置导出头文件

在根目录和Cargo.toml同级创建一个导出配置文件 cbindgen.toml
cbindgen.toml 配置如下

#language = "c++"或language = "c"
language = "c"
#是否不导入头文件
no_includes = false

[export]
prefix = "hq_"

开始导出头文件

# cbindgen 可以导出指定crate的pub方法或类型
# 以下命令自选一种
sudo cbindgen --crate hello --output ios/hello.h
sudo cbindgen --config cbindgen.toml --crate hello --output ios/hello.h
sudo RUST_BACKTRACE=1 cbindgen --config cbindgen.toml --crate hello --output ios/hello.h
sudo RUST_BACKTRACE=full cbindgen --config cbindgen.toml --crate hello --output ios/hello.h

相关文章

  • 用Rust开发iOS静态库

    添加架构目标 编写lib.rs 手动添加头文件,可使用cbindgen自动创建,往下看 修改配置文件Cargo.t...

  • iOS开发笔记---- 键盘、静态库、动画、Crash定位

    iOS开发笔记---- 键盘、静态库、动画、Crash定位 iOS开发笔记---- 键盘、静态库、动画、Crash定位

  • Framework 动态库 & 静态库

    关于 动态库 & 静态库 ,参考下面文章:iOS开发关于"框架"的那些事iOS 静态库,动态库与 Framewor...

  • iOS SDK(二):Bundle

    iOS SDK开发系列:iOS SDK(一):静态库、动态库创建&接口测试iOS SDK(二):Bundle......

  • 编译 Bitcode 版静态库掉过的坑

    在公司负责开发内部用的 iOS 的静态库工具。随着 BitCode 的诞生,静态库也需要更新一下。 先说一下 我掉...

  • iOS 静态库

    iOS 静态库 iOS 静态库

  • iOS 静态库开发

    iOS 静态库开发 本文旨在说明静态库制作中的一些常见问题和特殊处理1. 打包静态库需要的相关问题和设置 静态库中...

  • iOS 创建.a 静态库

    iOS开发中静态库(.a)制作 Xcode 9一般iOS 开发者都会引用第三的库 库根据根据源代码的公开情况 分为...

  • 技术文章收录

    iOS开发之静态库(三)—— 图片、界面xib等资源文件封装到.a静态库 React-Native:React-N...

  • iOS开发WorkSpace多工程联合编译

    参考链接 iOS开发之静态库.a的制作教程(有合并.a文件的方法)一篇较为详细的 ios静态动态库 的使用方法总结...

网友评论

      本文标题:用Rust开发iOS静态库

      本文链接:https://www.haomeiwen.com/subject/twdnoktx.html