当 -C target-cpu=native
标志传递给 rustc
时,我正在编写的程序运行得更快。我想在编译时为用户提供一种简单的、平台无关的方式来启用它,所以我在 Cargo.toml 中添加了一个 Cargo 特性 cpu_native = []
并在我的项目中创建了这个 Cargo 配置:
[target.'cfg(cpu_native)']
rustflags = ["-C", "target-cpu=native"]
但是,这对我的程序没有影响,将 --features cpu_native
传递给 Cargo 甚至不会触发重新编译。更改为以下 Cargo 配置确实使用更快的指令强制重新编译:
[build]
rustflags = ["-C", "target-cpu=native"]
但是,这将使用具有默认 Cargo 功能的 target-cpu=native
进行编译,这不是我想要的。从 Cargo 书中,我想要的似乎是可能的,但我看不出我做错了什么。
最佳答案
我认为这不受支持(还?)。我增强了 Cargo 以打印出解析时检查的配置标志:
[
Name("debug_assertions"),
Name("proc_macro"),
KeyPair("target_arch", "x86_64"),
KeyPair("target_endian", "little"),
KeyPair("target_env", ""),
KeyPair("target_family", "unix"),
KeyPair("target_os", "macos"),
KeyPair("target_pointer_width", "64"),
Name("unix"),
]
[target.'cfg(cpu_native)']
这是 Cargo 功能的错误语法;它通常是 cfg(feature = "cpu_native")
。
关于rust - 我如何根据 Cargo 功能选择性地传递 rustc 标志?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50669828/