libusb_stm32 icon indicating copy to clipboard operation
libusb_stm32 copied to clipboard

[Discussion] C++ compilers really hate flexible array member.

Open apricot-azumi opened this issue 1 year ago • 2 comments

C++ compilers really hate struct {...; char vla[];};. It gives warning for many struct declarations, which can be fortunately suppressed with:

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic"
#include <usb.h>
#pragma GCC diagnostic pop

But USB_ARRAY_DESC and USB_STRING_DESC_CXX fails with no rescue.

One workaround is to use this in another C file, and use extern "C" in c++. LTO gives warning about type mismatch between units.

I personally use this https://godbolt.org/z/aTG1aEP96 in my project. This looks somewhat overkill and it requires C++17/20.

What's your opinion on compatibility with C++?

apricot-azumi avatar Jul 22 '24 18:07 apricot-azumi

Unfortunately, there are a lot of flexible arrays in the USB by design. I don't have much experience with pluses, but I think it would be a good idea to place all descriptors and the descriptor handling callback into a separate C file, compile it using C to object, and let the linker do its job. P.S. USB_ARRAY_DESC and USB_STRING_DESC are just helper macros. All descriptors are just byte arrays, usually located in the .rodata section. I don't see how the linker might try to optimize this.

dmitrystu avatar Jul 22 '24 22:07 dmitrystu

Found a good array generator https://teenydt.github.io/

mrx23dot avatar Feb 21 '25 11:02 mrx23dot