Compiling in GCC but not in Clang: "initializer element is not a compile-time constant"

46 views Asked by At

I'm having this error (in the title) when I use the compiler clang-15 (15.0.7). Below is a minimum reproducible code:

#include <stdio.h>
#include <stdint.h>

#define BUF_SIZE    255
#define ID_SIZE (sizeof(uint32_t))
#define F_SIZE      (sizeof(uint8_t))
#define TS_SIZE        (sizeof(uint64_t))
#define CRC_SIZE       (sizeof(uint16_t))
#define PL_LEN      (16)
#define MSG_SIZE       (ID_SIZE + PL_LEN + F_SIZE)
#define RST_FS         (0U)


typedef union {
    struct {
        uint32_t id;                  
        uint8_t  pl[PL_LEN];          
        uint8_t  fs;                           
    } __attribute__((packed));
    uint8_t msg_buf[MSG_SIZE];
} TM;

typedef union {
    struct {
        uint8_t  strt;
        TM msg;
        uint16_t crc;
        uint8_t  end;
    } __attribute__((packed));
    uint8_t msg_buf[BUF_SIZE];
} TM_BUF;


/* LOCAL CONSTANTS */
static const TM RST_MSG = {
    .id = 0x0000U,   
    .pl = {0U,0U,0U,0U,0U,0U,0U,0U,0U,0U,0U,0U,0U,0U,0U,0U},
    .fs = RST_FS,
};

static const TM_BUF RST_BUF = {
    .strt= 0U,
    .msg = RST_MSG,
    .crc = 0U,
    .end = 0U,
};

int main (void){

    printf("Hello World\n");

    return 0;
}

I have read in this link that this has something to do with compiler conflict and clearly looks like it. What's the best workaround here?

The code above compiles using GCC 9.4.0 compiler in Ubuntu.

0

There are 0 answers