如何将条件编译与 `cfg` 和 Cargo 一起使用? [英] How do I use conditional compilation with `cfg` and Cargo?

查看:44
本文介绍了如何将条件编译与 `cfg` 和 Cargo 一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 cfg 和 Cargo 有条件地编译我的源代码,在谷歌搜索了一段时间后,似乎解决方案是使用 cargo --features.

I want to conditionally compile my source code using cfg with Cargo, after Googling for a while, it seems that the solution is to use cargo --features.

http://doc.crates.io/manifest.html

我尝试添加一些

#[cfg(feature = "foo")]

在源代码和

cargo build --features foo

,但它说

Package `xxx v0.0.1 (file:///C:/yyy/xxx)` does not have these features: `foo`

如何让货物识别特征?我是否必须在 Cargo.toml 中添加一些东西?

How can I let cargo identify the features? Do I have to add something in Cargo.toml?

这是我使用的 rustccargo 的版本:

Here's the version of rustc and cargo I am using:

C:\>rustc --version
rustc 0.13.0-nightly (42deaa5e4 2014-12-16 17:51:23 +0000)

C:\>cargo --version
cargo 0.0.1-pre-nightly (5af754d 2014-12-18 01:50:48 +0000)

推荐答案

您必须在 Cargo.toml 中引入现有功能.

You have to introduce the existing features in your Cargo.toml.

我能够通过执行以下操作有条件地编译:

I was able to conditionally compile by doing the following:

  • Cargo.toml中,创建一个features部分,并引入某个功能名称:

  • In Cargo.toml, create a features section and introduce a certain feature name:

[features]

customfeature = [] # feature has no explicit dependencies

如果您希望您的功能具有特定的依赖关系,请检查文档中的示例.

If you want your feature to have specific dependencies check the examples in the documentation.

在您的代码中,使用 #[cfg(feature="customfeature")]

运行cargo build --features customfeature

由于您的步骤 2 &3 好像没问题,肯定是你的 Cargo.toml 有问题.

Since your steps 2 & 3 seem to be fine, there must probably be a problem with your Cargo.toml.

这篇关于如何将条件编译与 `cfg` 和 Cargo 一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆