当库使用模板(泛型)时,是否可以使用Rust的C ++库? [英] Is it possible to use a C++ library from Rust when the library uses templates (generics)?

查看:58
本文介绍了当库使用模板(泛型)时,是否可以使用Rust的C ++库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当库(例如 Boost )使用模板(泛型)时,是否可以使用Rust提供的C ++库?

Is it possible to use a C++ library from Rust when the library (e.g. Boost) uses templates (generics)?

推荐答案

,但可能不是实用.

D编程语言是提供某种程度的C ++互操作性的极少数语言之一.您可以在 dlang 上了解有关此内容的更多信息.

The D programming language is one of the very few providing some degree of C++ interoperability; you can read more about it on dlang.

请注意模板部分的限制:

Note the limitation for the template section:

请注意,必须通过链接到C ++目标代码或包含这些实例的共享库来提供D代码中使用的所有实例.

Note that all instantiations used in D code must be provided by linking to C++ object code or shared libraries containing the instantiations.

从本质上讲,这意味着您必须使用C ++代码来生成具有正确类型的模板的实例,然后D编译器将针对这些实例进行链接.

which essentially means that you must use C++ code to cause the instantiation of the templates with the right types, and then the D compiler will link against those instantiations.

您可以对Rust进行相同的操作.如果没有编译器支持,则意味着手动修改名称.在"FFI"部分中,您会找到链接属性:

You could do the same for Rust. Without compiler support, this means mangling the names manually. In the FFI section, you will find the link attribute:

#[link(name = "snappy")]
extern {
    fn snappy_max_compressed_length(source_length: size_t) -> size_t;
}

告诉编译器哪个链接库将提供符号,您还将支持各种调用约定

which tells the compiler which linked library will provide the symbol, you will also support for various calling conventions and the no_mangle attribute.

您可能需要适当地应用#[allow(non_snake_case)] .

You may need to apply #[allow(non_snake_case)] as appropriate.

Servo使用 bindgen 为C和C ++代码生成Rust绑定;我对C ++的支持程度还不清楚,并且对它是否可以处理模板有些怀疑.

Servo uses bindgen to generate Rust bindings for C and C++ code; I am unclear on the level of C++ support, and somewhat doubtful that it can handle templates.

这篇关于当库使用模板(泛型)时,是否可以使用Rust的C ++库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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