Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compiling with GCC causes enum alignment issues #163

Closed
Tracked by #184
FloatyJellyfish opened this issue Mar 21, 2024 · 1 comment
Closed
Tracked by #184

Compiling with GCC causes enum alignment issues #163

FloatyJellyfish opened this issue Mar 21, 2024 · 1 comment

Comments

@FloatyJellyfish
Copy link

By default, GCC targeting embedded platform enables the -fshort-enums flag which dynamically sizes enums based on the greatest value the enum contains. This results in enums being represented by 1/2/3 bytes rather than the platforms default of 4 bytes.

However, bindgen uses clang which doesn't use -fshort-enums by default and so always generates bindings to enums with a size of 4 bytes.

As a result, when structures containing enums are passed between Rust and C they are miss aligned as Rust expects 4 bytes for each enum but it could be represented in a smaller number of bytes from the C side of things.

There are a couple of solutions:

  1. Don't use GCC as your default compiler and use the ARM Clang compiler instead
  2. Add cfg.flag("-fno-short-enums"); to cc Build in lvgl-sys/build.rs script when using GCC which stops the default behaviour of shrinking enums when lvgl-sys is built
  3. Add .clang_arg("-fshort-enums") to bindings Builder also in lvgl-sys/build.rs to make Clang in the bindgen to shrink enums in the same way

I'm not sure if any actions need to be taken off these findings, but I just wanted to put this here in case anyone else has a similar prolem.

@C47D
Copy link
Collaborator

C47D commented Jan 9, 2025

@FloatyJellyfish Thanks for pointing out those solutions.

C47D added a commit to C47D/lv_binding_rust that referenced this issue Jan 12, 2025
@C47D C47D closed this as completed Jan 12, 2025
C47D added a commit that referenced this issue Jan 17, 2025
* Update README with notes about safe and unsafe bindings

* Update build command for embedded

Remove $ from command
Add missing space in embedded builds

* README: Add note about enum issue when compiling with GCC

Fixes #163
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants
@C47D @FloatyJellyfish and others