gpt4 book ai didi

Rust 不支持条件编译的自定义 cfg 功能标志

转载 作者:行者123 更新时间:2023-11-29 08:11:23 25 4
gpt4 key购买 nike

<分区>

我正在尝试使用 Rust 的条件编译功能,但它根本不起作用。我正在尝试使用它在默认库和不同库之间切换,使用 cfg 根据是否设置功能标志重新导出两个不同子模块之一。代码:

lib.rs:

pub mod ffi;
#[cfg(ffiv1)]
mod ffiv1;
#[cfg(not(ffiv1))]
mod ffiv2;

#[test]
fn test_ffi_struct() {
let _fs = ffi::FFIStruct{ x: 42};
}


#[cfg(ffiv1)]
#[test]
fn test_v1() {
println!("v1 enabled");
}

ffi.rs:

//re-export as ffi::FFIStruct
#[cfg(ffiv1)]
pub use ffiv1::FFIStruct;

#[cfg(not(ffiv1))]
pub use ffiv2::FFIStruct;

ffiv1.rs:

pub struct FFIStruct {
pub x: i32,
y: IShouldFail
}

ffiv2.rs:

pub struct FFIStruct {
pub x: i64
}

cargo .toml:

[features]
ffiv1 = []

使用默认的 cargo build/test,这会按预期构建和工作,一切正常。

使用 cargo build --features ffiv1,它的行为就像根本没有传递任何功能标志一样。使用 --verbose 运行 cargo 显示 --cfg 'feature="ffiv1"' 转到 rustc

我希望编译在 ffiv1.rs 中的 undefined symbol IShouldFail 上失败。 (以及编译后要运行的额外测试)。

这里有什么问题吗?

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com