Union initialization in C++ and C

后端 未结 3 1732
太阳男子
太阳男子 2021-02-12 01:17

I have built a working C library, that uses constants, in header files defined as

typedef struct Y {
  union {
    struct bit_field bits;
    uint8_t raw[4];
  }         


        
3条回答
  •  醉话见心
    2021-02-12 02:14

    I decided to choose the following path.

    • Do not use .member initialization.
    • do nost use static const struct Foobar initialization of members

    Instead declare the global variable:

    extern "C" {
      extern const struct Foobar foobar;
    }
    

    and initialize it in a global section:

    struct Foobar foobar = { 0, 0, 0, 0 };
    

    and instead of bugging the C++ compiler with modern ANSI C99 syntax I let the linker do the work be demangling C symbols.

提交回复
热议问题