知识卡片
条件编译按配置选择代码
cfg按配置决定代码是否进入编译,cfg_attr则按条件添加属性。配置可用test这样的单名,也可用target_os等键值;同一键可有多个有效值,例如目标同时支持32位与64位原子操作。它改变编译输入,不是运行时if,因此不同配置都需相应验证。
这张卡片看不懂?
这张卡片的反馈已满 10 条,暂时无法继续提交。
已有反馈 · 0 / 10 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- Chapter 4. Dependencies Conditional Compilation · 查看原书依据
Conditional Compilation Rust includes support for conditional compilation, which is controlled by cfg (and cfg_attr) attributes. These attributes govern whether the thing—function, line, block, etc.—that they are attached to is included in the compiled source code or not (which is in contrast to C/C++’s line-based preprocessor). The conditional inclusion is
参考来源
- 位置:《Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)》第4章《Conditional Compilation》(spine8;extracted/Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)/_epub-src/OEBPS/ch04.html;u130-ch04-021)。
- 结论依据:原文在“Conditional Compilation”附近的定义、例子或代码支撑本卡核心说明:cfg按配置决定代码是否进入编译,cfg_attr则按条件添加属性。边界按已读前提解释;示例未执行。
- 原始内容:Conditional Compilation Rust includes support for conditional compilation, which is controlled by cfg (and cfg_attr) attributes. These attributes govern whether the thing—function, line, block, etc.—that they are attached to is included in the compiled source code or not (which is in contrast to C/C++’s line-based preprocessor). The conditional inclusion is