Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Rust 发现当前默认目标?_Rust - Fatal编程技术网

Rust 发现当前默认目标?

Rust 发现当前默认目标?,rust,Rust,我想这样做,这意味着添加一个如下所示的配置部分: [target.x86_64-unknown-linux-gnu.foo] rustc-link-search = ["/path/to/foo"] rustc-link-lib = ["foo"] root = "/path/to/foo" key = "value" 但是我使用的是Mac,所以x86_64-unknown-linux-gnu不是正确的目标 我如何发现当前使用的是哪个target triple rustc或cargo rust

我想这样做,这意味着添加一个如下所示的配置部分:

[target.x86_64-unknown-linux-gnu.foo]
rustc-link-search = ["/path/to/foo"]
rustc-link-lib = ["foo"]
root = "/path/to/foo"
key = "value"
但是我使用的是Mac,所以x86_64-unknown-linux-gnu不是正确的目标

我如何发现当前使用的是哪个target triple rustc或cargo

rustc--print cfg
打印一个似乎与三元组不对应的值列表(特别是其中没有
未知的


rustc——打印目标列表
显示所有可用目标;我只想要默认值。

使用最新的rustc编译器:

$ rustc -Z unstable-options --print target-spec-json | grep llvm-target
什么对我有用(受罗德里戈回答的启发)


RUSTC_BOOTSTRAP=1会绕过通常只允许在夜间分支上使用某些功能的检查。我还使用了一个合适的json解析器,而不是grep。

它可能不是特别优雅,但我发现这是可行的:

rustup show | grep default | grep -Po "^[^-]+-\K\S+"
rustc--print cfg
将输出如下内容:

$ rustc --print cfg
debug_assertions
target_arch="x86_64"
target_endian="little"
target_env="gnu"
target_family="unix"
target_feature="fxsr"
target_feature="sse"
target_feature="sse2"
target_os="linux"
target_pointer_width="64"
target_vendor="unknown"
unix

目标是
--

cargo使用
rustc-vV
检测默认目标三重()。我们可以做同样的事情:

使用std::process::Command;
无论如何使用:{format_err,Context,Result};
使用std::str;
fn get_target()->结果{
让输出=命令::新建(“rustc”)
.arg(“-vV”)
.output()
.context(“未能运行rustc以获取主机目标”)?;
让output=str::from_utf8(&output.stdout).context(`rustc-vV`没有返回utf8输出”)?;
let field=“主机:”;
让主机=输出
.行()
.find(| l | l.以(字段)开头)
.map(| l |&l[field.len()…]))
.好的还是别的{
格式错误(
“`rustc-vV`没有`{}`的行,得到:\n{}”,
field.trim(),
输出
)
})?
.to_string();
Ok(主持人)
}
fn main()->结果{
让host=get_target()?;
println!(“目标三元组:{}”,主机);
好(())
}

您的Mac电脑的目标可能是
x86\u 64-apple-darwin
。中间部分是供应商,对于Linux来说是未知的,但是对于Mac来说是苹果的。但是,您似乎希望独立于目标配置构建脚本,因此根本不需要目标三元组。是的,我通过查看列表找到了正确答案——找到了两个
darwin
目标,并且知道我在
x86_64
,但这仍然需要我思考。你能扩展一下“根本不需要目标三元组”吗?嗯。
错误:选项'Z'只在夜间编译器上被接受。
(rustc 1.42.0)。谢谢,它对我有用。在shell中,您可以通过以下方式获得它:
(eval$(rustc--print cfg|grep target);echo$target_arch-$target_vendor-$target_os-$target_env)
$ rustc --print cfg
debug_assertions
target_arch="x86_64"
target_endian="little"
target_env="gnu"
target_family="unix"
target_feature="fxsr"
target_feature="sse"
target_feature="sse2"
target_os="linux"
target_pointer_width="64"
target_vendor="unknown"
unix