在 GCC 中编译,但不在 Clang 中编译:“initializer 元素不是编译时常量”

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

提问人:rammmmm_ 提问时间:11/14/2023 最后编辑:rammmmm_ 更新时间:11/14/2023 访问量:34

问:

当我使用编译器 clang-15 (15.0.7) 时,我遇到了这个错误(在标题中)。以下是最低限度的可重现代码:

#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;
}

在这个链接中读到,这与编译器冲突有关,显然看起来像它。这里最好的解决方法是什么?

上面的代码使用 Ubuntu 中的 GCC 9.4.0 编译器进行编译。

gcc 静态 编译时常量

评论


答: 暂无答案