1

I'm trying to compile a binary with avr-g++ using this tutorial for UART, but during compilation, I get the following error:

main.cpp:50:20: error: designator order for field '__file::flags' does not match declaration order in 'FILE'
   50 | FILE uart_output = FDEV_SETUP_STREAM(uart_putchar, NULL, _FDEV_SETUP_WRITE);

I've made sure that I'm using /usr/avr/include/stdio.h, and attempted to use C++20 in light of the following:

Why does C++11 not support designated initializer lists as C99?

Yet I have not been able to have any success in compiling (or even understanding the problem). I have also attempted to replicate the example in the avr stdio.h implementation source file (line 152) but also had no success.

Any help is greatly appreciated. Thanks in advance.

Utku Boduroglu
  • 127
  • 1
  • 7
  • 1
    I would just take a look at the definition of that macro and assign the fields manually - it seems to be broken. – Paul Sanders Oct 18 '22 at 20:24
  • I swapped the declaration order in the macro and it is compiling now; my only concern is that this issue does not seem to be that commonplace -- I haven't been able to find any others having this issue and I suspect that this has more to do with my setup. – Utku Boduroglu Oct 18 '22 at 20:32
  • 2
    You are not the only one having a problem with that. Bug report from 2012: https://savannah.nongnu.org/bugs/?36970 (https://github.com/avrdudes/avr-libc/issues/532). It didn't receive any response though. I guess you can use the `fdev_setup_stream` (all lowercase) macro with a user-supplied pointer to a `FILE` in C++ instead (if I read the man page correctly: https://manpages.debian.org/testing/avr-libc/fdevopen.3avr.en.html) – user17732522 Oct 18 '22 at 21:26

1 Answers1

2

As it appears, FDEV_SETUP_STREAM is not ready for C++, see also example non-trivial designated initializers not supported for the reason behind the error.

What works for me is to re-define FDEV_SETUP_STREAM to include all components of struct __file in their respective order:

#undef FDEV_SETUP_STREAM
#define FDEV_SETUP_STREAM(p, g, f) \
    {                              \
        .buf = NULL,               \
        .unget = 0,                \
        .flags = f,                \
        .size = 0,                 \
        .len = 0,                  \
        .put = p,                  \
        .get = g,                  \
        .udata = 0                 \
    }

However, the behaviour of the compiler appears to be not really consistent about when that error is raised.

emacs drives me nuts
  • 2,785
  • 13
  • 23