我可以有条件地为Windows子系统编译Rust程序吗? [英] Can I conditionally compile my Rust program for a Windows subsystem?
问题描述
我有一个Rust程序,当我构建它以进行分发时,我想为 windows子系统编译该程序。目前,我正在main.rs中使用它:
I have a Rust program that I want to compile for the "windows" subsystem when I'm building it for distribution. Currently I am using this in my main.rs:
#![feature(windows_subsystem)]
#![windows_subsystem = "windows"]
这可行,但是当我在Windows计算机上运行测试时, Windows子系统无权访问控制台,因此我看不到输出。我需要注释掉上面的代码行才能查看测试结果。
This works, but when I run the tests on a Windows machine, the Windows subsystem does not have access to the console so I cannot see the output. I need to comment out the above lines of code in order to see the result of my tests.
是否可以有条件地编译我正在运行的子系统以使测试正常工作?
Is there a way to conditionally compile which subsystem I'm running on in order to get the tests to work?
推荐答案
您还可以基于
cfg $ c $设置另一个属性c>具有
cfg_attr
的变量:
#[cfg_attr(a, b)]
将与#[b] $ c相同$ c>如果
a
是通过 cfg
属性设置的,则
则没有其他设置。
Will be the same as #[b]
if a
is set by cfg
attribute, and
nothing otherwise.
在这种情况下,应该类似于
In this case, it should be something like
#![cfg_attr(my_feature_name_i_made_up, windows_subsystem = "windows")]
这篇关于我可以有条件地为Windows子系统编译Rust程序吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!