rust中编译宏控
1. 简介
我们知道在C语言开发过程中,我们可以在makefile中定义cflags = -DCFG_XXX,这样在写C代码的时候,我们就可以使用:
#ifdef CFG_XXX
…
#else
#endif
而我们在rust编译过程中即没有makefile,而且rust语法也不支持#ifdef #else #endif这样的定义,那么在rust中中如何实现这种编译宏控呢。
2. 说明
rust中通过cargo.toml
[features]
来实现类似C中的-Dxxxx能力
在c代码中通过
#[cfg(feature = “feature_one”)]
来实现#ifdef的功能
3. 代码举例
main.rs
fn main() {#[cfg(feature = "feature_one")]{println!("Feature one is enabled.");}#[cfg(feature = "feature_two")]{println!("Feature two is enabled.");}#[cfg(all(not(feature = "feature_one"<