cgccstructcompiler-warningsgcc-pedantic

gcc: fixing -pedantic "unnamed structure" warning


I'm trying to get some code from elsewhere (specifically, here), to compile without any warnings when gcc is given the -pedantic flag. The only problem is this bit of code:

struct __attribute__ ((aligned(NLMSG_ALIGNTO))) {
    struct nlmsghdr nl_hdr;
    /* Unnamed struct start. */
    struct __attribute__ ((__packed__)) {
        struct cn_msg cn_msg;
        struct proc_event proc_ev;
    };
    /* Unnamed struct end. */
} nlcn_msg;

No matter where I try to put in a name for the structure, it results in a compilation error. Is there some way to modify the given code to satisfy -pedantic? Or is there some way to tell gcc to not issues a warning just for that piece of code?


Solution

  • Which standard are you compiling to?

    Given this code:

    #define NLMSG_ALIGNTO 4
    
    struct nlmsghdr { int x; };
    struct cn_msg { int x; };
    struct proc_event { int x; };
    
    struct __attribute__ ((aligned(NLMSG_ALIGNTO))) {
        struct nlmsghdr nl_hdr;
        /* Unnamed struct start. */
        struct __attribute__ ((__packed__)) {
            struct cn_msg cn_msg;
            struct proc_event proc_ev;
        };
        /* Unnamed struct end. */
    } nlcn_msg;
    

    Compiling with C99 mode, I get errors:

    $ gcc -O3 -g -std=c99 -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes \
        -Wold-style-definition -Werror -pedantic -c x2.c
    x2.c:13:6: error: ISO C99 doesn’t support unnamed structs/unions [-Werror=pedantic]
         };
          ^
    cc1: all warnings being treated as errors
    $
    

    Compiling with C11 mode, I get no errors:

    $ gcc -O3 -g -std=c11 -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes \
          -Wold-style-definition -Werror -pedantic -c x2.c
    $