Bindgen #define with cast

Hello,

I am using bindgen in my project for a c source code. I am encountering difficulties with the different #define.

So this #define is not generated:

#define MY_DEFINE    ((int) 'c')

whereas this #define is generated:

#define MY_DEFINE 'c'

I am under the impression that the casting prevents bindgen from generating the equivalent rust code.

Is this intended or a bug ? Is there a workaround ?

Thank you very much in advance for any help

since #define in C can expand to arbitrary C code, some of which can only be correctly parsed within the expaned context, and when parsed alone, the C code snippet may not have rust equivalence at all.

so, bindgen's support for #define is at best effort, it tries to support common form of macros including constants and function-like, but unfortunately, your use case is not supproted yet, but there's an pull request for this particular use case:

for now, if you have many such macros which have similar format, you may be able to work around it using the modify_macro parsing callback, for example, when you reach #define X ((int) 'c'), you can evaluate the constant symbolically and simplify the macro definition into something like #define X 99 which can be parsed by bindgen.

1 Like

Named constant instead of a macro?

Thank you for your time and help.

Thank you for this.

The code isn't mine, and I don't really intend to modify it every time there's a new release.

Thank you for your help again.

1 Like