【问题标题】:How to mark use statements for conditional compilation? [duplicate]如何标记条件编译的使用语句? [复制]
【发布时间】:2019-06-28 04:25:52
【问题描述】:

是否可以将某些包含标记为仅包含在相关操作系统中?

例如,你能不能这样做:

#[cfg(unix)] {
    use std::os::unix::io::IntoRawFd;
}
#[cfg(windows)] {
   // https://doc.rust-lang.org/std/os/unix/io/trait.AsRawFd.html  suggests this is equivalent?
   use std::os::windows::io::AsRawHandle;
}

尝试编译上面的代码会出现语法错误(即error: expected item after attributes)。

我正在尝试修补我在 GitHub 上找到的 Rust 项目以在 Windows 上编译(同时仍使其保留在其现有目标上编译的能力 - 即 Unixes 和 WASM)。目前我遇到了一个问题,其中一些文件从std::os(例如use std::os::unix::io::IntoRawFd;)导入特定于平台的部分,最终破坏了Windows上的构建。

注意:我使用的是 Rust Stable (1.31.1),而不是每晚使用。

【问题讨论】:

标签: syntax rust conditional-compilation


【解决方案1】:

您要查找的语法是:

#[cfg(target_os = "unix")]
use std::os::unix::io::IntoRawFd;

#[cfg(target_os = "windows")]
use std::os::windows::io::AsRawHandle;

【讨论】:

    猜你喜欢
    • 2010-12-25
    • 2020-09-12
    • 1970-01-01
    • 2013-08-20
    • 2015-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-18
    相关资源
    最近更新 更多