将简单的C #define转换为Rust常量 [英] Convert simple C #define's into Rust constants

查看:205
本文介绍了将简单的C #define转换为Rust常量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个专有的库,其中包含一个很长的故事和30KLoC标头。我想为Rust生成绑定。而且我几乎成功地使用了 bindgen 板条箱。除了 bindgen 不能将宏常量转换为普通常量。因为这些常量是以模糊方式定义的,例如

I have a proprietary library with a long story and 30KLoC header. I'd like to generate bindings for Rust to it. And I've used bindgen crate almost successfully. Except bindgen can't transform macro constants into normal constants. Because those constants are defined in a fuzzy way, like

#define CONSTANT ((const_type)SOME_OTHER_CONSTANT)

因此,有什么方法可以将这些半常数转换为普通的Rust常数:

So, is there some way to translate such half-constants into normal Rust ones:

const Type name = value;

更新

显然,板条箱 bindgen 使用 clang 作为后端。如果向解析器提供了正确的标志, clang 能够处理宏定义和其他类似的东西。因此,有人可以建议使用 libclang API处理宏定义的好教程吗?

Apparently, crate bindgen uses clang as its backend. And clang is able to deal with macro definitions and other such stuff, if right flag is provided to parser. So, can anyone advice good tutorial on dealing with macro definitions using libclang API?

推荐答案

通过 gcc -E (即预处理器)运行标头将删除 #define s。

Running the headers through gcc -E (i.e. the preprocessor) will remove the #defines. It may remove more than you want though.

如果要选择性地进行预处理,以前有一个名为 scpp 做到了。除了要下载的一些tar文件外,我无法立即找到对其的引用,但是您可能还有更多的运气。

If you want to selectively preprocess, there used to be a utility called scpp that did that. I can't immediately find a reference to it other than to some tar files to download, but you may have more luck.

这篇关于将简单的C #define转换为Rust常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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