Convert simple C #define's into Rust constants

拥有回忆 提交于 2019-12-13 01:05:41

问题


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)

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

const Type name = value;

UPDATE

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?


回答1:


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

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.



来源:https://stackoverflow.com/questions/33529030/convert-simple-c-defines-into-rust-constants

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!