0

So I want add a C library into my C++ program and having issues with how to properly define structs that are declared in the C library. A sample program from the library defines the struct in the way you see below. Which already confuses me as its defining 2 structs at once, or at least that's what it looks like to me. I'm not experienced with C.

When I include this snippet from the sample program in my C++ code I get this compile error:

src/main.cpp:47:4: sorry, unimplemented: non-trivial designated initializers not supported
    };
    ^

My research so far brought me to the conclusion that its a simple matter of using C99 or higher. Though I'm unsure on how to define C99 or C11 when I'm compiling C++ code. I'm compiling with -std=c++0x with g++ 4.8. When I add a cflag in my make file with -std=c11 I get a warning that this flag doesn't apply to my code as its only valid with C code.

If Its not possible to use the struct this way how would I define it correctly in C++?

The Struct:

ws2811_t ledstring =
    {
        .freq = TARGET_FREQ,
        .dmanum = DMA,
        .channel =
        {
            [0] =
            {
                .gpionum = GPIO_PIN,
                .count = LED_COUNT,
                .invert = 0,
                .brightness = 255,
            },
            [1] =
            {
                .gpionum = 0,
                .count = 0,
                .invert = 0,
                .brightness = 0,
            },
        },
    };

The C library has the following structs in its header:

typedef struct
{
    int gpionum;                                 //< GPIO Pin with PWM alternate function, 0 if unused
    int invert;                                  //< Invert output signal
    int count;                                   //< Number of LEDs, 0 if channel is unused
    int brightness;                              //< Brightness value between 0 and 255
    ws2811_led_t *leds;                          //< LED buffers, allocated by driver based on count
} ws2811_channel_t;

typedef struct
{
    struct ws2811_device *device;                //< Private data for driver use
    uint32_t freq;                               //< Required output frequency
    int dmanum;                                  //< DMA number _not_ already in use
    ws2811_channel_t channel[RPI_PWM_CHANNELS];
} ws2811_t;
timonsku
  • 1,249
  • 2
  • 21
  • 45
  • Designated initializers are simply not part of C++, there might be an extension that enables them. – imreal May 05 '15 at 21:55
  • How would I define this struct then? I don't really know how to define it diffently. The whole setup of the structs is just confusing to me. – timonsku May 05 '15 at 21:58
  • Do it the C++ way, using constructors. If you don't want to change the existing code, put the definition in a .c file – imreal May 05 '15 at 22:00

0 Answers0