如何使用相同的配置选项构建二进制文件和库?

sbd*_*o85 7 rust rust-cargo

在询问时,如果你在同一个货物项目中有一个bin和一个lib,并且想要使用特定的rustc cfg选项构建bin和lib,那么它就不起作用了.

您可以使用rustc cfg选项构建一个或另一个,但不能同时构建两者.如果你尝试构建lib然后在bin编译bin时它重新编译lib而没有rustc选项.

有没有办法做到这两点,如果不是为什么?我注定要创建自己的构建脚本吗?如果是这样,货物的重点是什么?

编辑

好吧,也许我有点戏剧性

背景/扩展

说我有类似的东西:

SRC/lib.rs

pub mod mylib {

    #[cfg(not(dosomething))]
    pub use self::without_cfg::dosomething;

    #[cfg(dosomething)]
    pub use self::with_cfg::dosomething;


    mod with_cfg {
        pub fn dosomething() {
            println!("config option");
        }
    }

    mod without_cfg {
        pub fn dosomething() {
            println!("no config option");
        }
    }

} 
Run Code Online (Sandbox Code Playgroud)

SRC/main.rs

extern crate modules;

use modules::mylib::dosomething;

fn main() {
    dosomething();
}
Run Code Online (Sandbox Code Playgroud)

因此,如果我使用dosome的cfg选项编译,我会得到一个函数的版本,但如果我没有配置,我会得到"默认"行为或其他什么.

现在,如果我尝试使用货物rustc进行编译,我将永远无法使用lib中设置的cfg dosomething来获取bin的版本.

我能够在货物上做到最接近的是:

cargo rustc -v --lib -- --cfg dosomething
cargo rustc -v --bin [bin name] -- --cfg dosomething
Run Code Online (Sandbox Code Playgroud)

第一个命令将使用cfg编译lib,但第二个命令将重新编译 lib而不使用cfg以创建bin.

我提出的唯一解决方法是:

cargo rustc -v --bin [bin name] -- --cfg dosomething
Run Code Online (Sandbox Code Playgroud)

复制它为编译命令吐出的内容,例如:

rustc src/main.rs --crate-name [bin name] --crate-type bin -g --cfg dosomething --out-dir [/path/to/project]/target/debug --emit=dep-info,link -L dependency=[/path/to/project]/target/debug -L dependency=[/path/to/project]/target/debug/deps --extern modules=[/path/to/project]/target/debug/libmodules.rlib`
Run Code Online (Sandbox Code Playgroud)

然后运行:

cargo rustc -v --lib -- --cfg dosomething
Run Code Online (Sandbox Code Playgroud)

最后复制并粘贴之前的rustc命令,以便使用设置了cfg选项的lib编译bin.

这是唯一的方法吗?为什么我不能以某种方式指定哪些库/箱获得我想要的rustc cfg选项,即使它在Cargo.toml中?或者我和我都没有意识到这一点?

对于那些问...

Cargo.toml:

[package]
name = "[bin name]"
version = "0.1.0"
authors = ["[Me] <[my email]>"]

[lib]
name = "modules"
path = "src/lib.rs"
Run Code Online (Sandbox Code Playgroud)

PS感谢所有从事过铁锈和货物运输的人,总而言之,我觉得这是一个愉快的工作环境,并且喜欢这门语言.保持良好的工作.

She*_*ter 6

如果我理解正确,那么Cargos 功能应该在这里提供帮助:

SRC/lib.rs

#[cfg(feature = "dosomething")]
pub use self::with_cfg::dosomething;

#[cfg(not(feature = "dosomething"))]
pub use self::without_cfg::dosomething;

#[cfg(feature = "dosomething")]
mod with_cfg {
    pub fn dosomething() {
        println!("config option");
    }
}

#[cfg(not(feature = "dosomething"))]
mod without_cfg {
    pub fn dosomething() {
        println!("no config option");
    }
}
Run Code Online (Sandbox Code Playgroud)

SRC/main.rs

extern crate what;

use what::dosomething;

fn main() {
    dosomething();
}
Run Code Online (Sandbox Code Playgroud)

Cargo.toml

[package]
name = "what"
version = "0.1.0"
authors = ["An Devloper <an.devloper@example.com>"]

[features]
dosomething = []
Run Code Online (Sandbox Code Playgroud)

现在,当我可以在任一模式下编译或运行时:

$ cargo run
   Compiling what v0.1.0 (file:///private/tmp/what)
     Running `target/debug/what`
no config option

$ cargo run --features dosomething
   Compiling what v0.1.0 (file:///private/tmp/what)
     Running `target/debug/what`
config option
Run Code Online (Sandbox Code Playgroud)