RUST笔记: 动态链接库的创建和使用

生成动态链接库

// https://github.com/vvvm23/funny-shapes
# 项目元信息
[package]
name = "funnyshapes"        # 项目名称
version = "0.1.0"           # 版本号
edition = "2021"            # Rust语言版本

# 更多配置信息可查阅:https://doc.rust-lang.org/cargo/reference/manifest.html

# 库配置,生成库文件
[lib]
name = "funnyshapes"        # 库的名称
path = "src/lib.rs"         # 源文件路径
crate-type = ["cdylib", "rlib"]  # 输出为动态库和静态库

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
# 还可添加 [[bin]] — 二进制目标设置。[[example]] — 目标设置示例。 [[test]] — 测试目标设置。[[bench]] — 基准目标设置
# [[bin]]
# name = "main"
# path = "src/main.rs"

# 依赖项配置
[dependencies]
clap = { version = "4.4.11", features = ["derive"] }  # 命令行参数解析库
image = "0.24.7"           # 图像处理库
indicatif = "0.17.7"        # 进度条库
ndarray = { version = "0.15.6", features = ["blas", "rayon"] }  # N维数组库
numpy = "0.20.0"           # 与NumPy接口的库
pyo3 = { version = "0.20.0", features = ["extension-module"] }  # 用于编写Python扩展的库
rand = "0.8.5"              # 随机数生成库
rayon = "1.8.0"             # 数据并行库

Rust 使用创建的动态链接库

  • 假设在 target/release/ 目录下存在一个动态链接库文件(funnyshapes.sofunnyshapes.dll)。
  • 创建一个新的Rust程序:
// src/main.rs
extern crate funnyshapes; // 库的名称

fn main() {
    let result = mylibrary::get_funnyshapes(1);
    println!("Result: {}", result);
}
  • 配置Cargo.toml:
    确保新程序的 Cargo.toml 文件中正确指定了依赖关系:

    [package]
    name = "myapp"
    version = "0.1.0"
    edition = "2021"
    
    [dependencies]
    mylibrary = "0.1.0"
    

CG

  • A spectrograph utility written in Rust:将文件转换为频谱
  • https://github.com/awelkie/terminal_spectrograph/blob/master/src/main.rs

你可能感兴趣的:(笔记,rust,笔记,开发语言)