如何使本地依赖依赖于Cargo中的功能? [英] How to make a local dependency depend on a feature in Cargo?

查看:96
本文介绍了如何使本地依赖依赖于Cargo中的功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于这个在子目录中使用本地包装的小型图书馆,我将如何使依赖项之一成为可选项目,具体取决于是否启用了功能?

Given this small library which uses local crates in subdirectories, how would I make one of the dependencies optional, depending on if a feature is enabled?

[package]
name = "image_load"
description = "Small wrapper for image reading API's."
version = "0.1.0"

[features]

default = ["use_png"]

[dependencies]

[dependencies.image_load_ppm]
path = "ppm"

# How to make this build _only_ when 'use_png' feature is enabled?
[dependencies.image_load_png]
path = "png"

虽然我阅读了文档,但这说明了如何选择外部依赖项.在上面的示例中,我使用的是基于功能的本地子目录,该目录是否要构建.

While I read the documentation, this shows how to have optional external dependencies. In the example above I'm using a local subdirectory, which I want to build, or not - based on a feature.

仅当启用 use_png 功能时,如何才能使 image_load_png 生成.

How can I make image_load_png only build when the use_png feature is enabled.

推荐答案

这可以通过添加以下内容来完成:

This can be done by adding the following:

[package]
name = "image_load"
version = "0.1.0"
description = "Small wrapper for image reading API's."

[features]

default = ["use_png"]
use_png = ["image_load_png"]  # <-- new line

[dependencies]

[dependencies.image_load_ppm]
path = "ppm"

[dependencies.image_load_png]
path = "png"
optional = true  # <-- new line

使用板条箱可以是可选的.

Using the crate can be optional.

例如:

#[cfg(feature = "use_png")]  // <-- new line
extern crate image_load_png;

这篇关于如何使本地依赖依赖于Cargo中的功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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