Rust条件编译属性支持的目标系统
【摘要】 在 Rust 中,#[cfg(target_os = "...")] 是一个条件编译属性,用于根据目标操作系统(Target OS)有条件地编译代码。Rust 支持多种目标操作系统,以下是完整的 target_os 列表及其对应的值: Rust 支持的 target_os 列表target_os 值对应的操作系统"android"Android"dragonfly"DragonFly BSD...
在 Rust 中,#[cfg(target_os = "...")]
是一个条件编译属性,用于根据目标操作系统(Target OS)有条件地编译代码。Rust 支持多种目标操作系统,以下是完整的 target_os
列表及其对应的值:
Rust 支持的 target_os
列表
target_os 值 |
对应的操作系统 |
---|---|
"android" |
Android |
"dragonfly" |
DragonFly BSD |
"freebsd" |
FreeBSD |
"fuchsia" |
Fuchsia |
"illumos" |
Illumos |
"ios" |
iOS |
"linux" |
Linux |
"macos" |
macOS |
"netbsd" |
NetBSD |
"openbsd" |
OpenBSD |
"redox" |
Redox |
"solaris" |
Solaris |
"tvos" |
tvOS |
"vxworks" |
VxWorks |
"windows" |
Windows |
"haiku" |
Haiku(较少见) |
"l4re" |
L4Re(较少见) |
"none" |
裸机环境(无操作系统) |
说明
-
常见操作系统:
"windows"
:Windows 系统。"linux"
:Linux 系统。"macos"
:macOS 系统。"android"
:Android 系统。"ios"
:iOS 系统。
-
Unix-like 系统:
"freebsd"
、"openbsd"
、"netbsd"
、"dragonfly"
、"illumos"
、"solaris"
等属于 Unix-like 操作系统。
-
其他系统:
"fuchsia"
:Google 的 Fuchsia 操作系统。"redox"
:Rust 编写的类 Unix 操作系统。"haiku"
和"l4re"
是较为少见的操作系统。"none"
:用于裸机环境(如嵌入式系统,无操作系统)。
-
移动操作系统:
"ios"
和"android"
分别用于 iOS 和 Android 平台的开发。
使用示例
#[cfg(target_os = "windows")]
fn main() {
println!("Running on Windows!");
}
#[cfg(target_os = "linux")]
fn main() {
println!("Running on Linux!");
}
#[cfg(target_os = "macos")]
fn main() {
println!("Running on macOS!");
}
// 默认情况(如果没有匹配的 target_os)
#[cfg(not(any(
target_os = "windows",
target_os = "linux",
target_os = "macos"
)))]
fn main() {
println!("Running on an unsupported OS!");
}
注意事项
- 可以通过
rustc --print target-list
命令查看所有支持的编译目标(包括target_os
)。 - 在跨平台开发中,合理使用条件编译可以确保代码在不同操作系统上的兼容性。
希望这个列表能帮助你更好地理解 Rust 中的条件编译和目标操作系统支持!
【声明】本内容来自华为云开发者社区博主,不代表华为云及华为云开发者社区的观点和立场。转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)