You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

已添加至Cargo.toml的本地crate opus_ffi仍无法解析的问题

本地依赖crate引用失败:failed to resolve: use of undeclared crate or module opus_ffi

问题背景

我正在开发一个作为Opus音频编解码器FFI的crate(命名为opus_ffi),希望将其作为本地依赖添加到现有项目中,但引用时始终报错。

已做操作

  • 在项目根目录的Cargo.toml中添加了本地依赖:
    [dependencies]
    opus_ffi = { path = "opus_ffi" }
    
  • 项目目录结构保持和flutter_rust_bridge要求一致,opus_ffi文件夹与顶级src同级。

目录结构

opus_ffi/
  opus/    <-- 原始C库
  src/
    lib.rs
    wrapper.h
  target/
  build.rs
  Cargo.toml
  Cargo.lock
src/
  api/
     mod.rs
     opus.rs
  lib.rs
build.rs
Cargo.toml  <-- 声明opus_ffi依赖的文件

代码尝试

在src/api/opus.rs中尝试引用opus_ffi:

use opus_ffi;

pub struct Encoder {
    encoder: *mut opus_ffi::OpusEncoder
}
impl Encoder {
    fn new() {}
}

pub struct Decoder {}
impl Decoder {}

错误信息

得到错误:

unresolved import `opus_ffi`
no external crate `opus_ffi`

尝试在mod.rs中添加extern crate opus_ffi;,依然报错:

can't find crate for `opus_ffi`
can't find crate

补充信息

opus_ffi/Cargo.toml

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

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

[target.'cfg(any(target_os = "ios", target_os = "macos"))'.dependencies]
core-foundation = "0.9"

[target.'cfg(target_os = "android")'.dependencies]
jni = "0.19"

[dependencies]
cmake = "0.1.50"
libc = "0.2.155"
pkg-config = "0.3.30"

[build-dependencies]
bindgen = "0.69.4"
cc = "1.1.7"

根目录Cargo.toml

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

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

[dependencies]
cpal = "0.15.3"
opus_ffi = { path = "opus_ffi" }
flutter_rust_bridge = "=2.1.0"

cargo check结果

zacharyhaslam@MacBook-Air-208 rust % cargo check
warning: opus_ffi@0.1.0: opus/src/repacketizer.c:305:11: warning: unused variable 'ret' [-Wunused-variable]
warning: opus_ffi@0.1.0:       int ret = opus_packet_extensions_generate(&data[ext_begin], ext_len, all_extensions, ext_count, 0);
warning: opus_ffi@0.1.0:           ^
warning: opus_ffi@0.1.0: 1 warning generated.
warning: unexpected `cfg` condition value: `test`
 --> build.rs:4:15
  |
4 |     #[cfg(not(feature = "test"))]
  |               ^^^^^^^^^^^^^^^^ help: remove the condition
  |
  = note: no expected values for `feature`
  = help: consider adding `test` as a feature in `Cargo.toml`
  = note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg/cargo-specifics.html> for more information about checking conditional configuration
  = note: `#[warn(unexpected_cfgs)]` on by default

warning: `rust_lib_flutter_rust_test` (build script) generated 1 warning
    Checking rust_lib_flutter_rust_test v0.1.0 (/Users/zacharyhaslam/FlutterApplications/flutter_rust_test/rust)
error[E0432]: unresolved import `opus_ffi`
 --> src/api/opus.rs:2:5
  |
2 | use opus_ffi;
  |     ^^^^^^^^ no external crate `opus_ffi`
For more information about this error, try `rustc --explain E0432`.
warning: `rust_lib_flutter_rust_test` (lib) generated 10 warnings
error: could not compile `rust_lib_flutter_rust_test` (lib) due to 1 previous error; 10 warnings emitted

注:其余10条警告均为项目中未使用变量的提示,已省略。

解决方案

问题出在opus_ffi的Cargo.toml配置上:你给opus_ffi设置的crate-type只有staticlib和cdylib,这两种类型是用于生成系统级库的,无法作为Rust crate被其他Rust项目直接引用。

要解决这个问题,需要修改opus_ffi/Cargo.toml中的[lib]部分,添加rlib类型(这是Rust库的默认类型,用于被其他Rust项目依赖):

[lib]
crate-type = ["staticlib", "cdylib", "rlib"]

修改后,重新执行cargo check,应该就能正常引用opus_ffi crate了。

另外,确认一下opus_ffi/src/lib.rs中是否正确导出了OpusEncoder类型,比如是否有pub struct OpusEncoder;或者通过bindgen生成后正确导出了符号。


内容的提问来源于stack exchange,提问作者zh24

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 00:43:18