2

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?

Community
  • 1
  • 1
Matthew Cline
  • 2,312
  • 1
  • 19
  • 36

1 Answers1

0

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
$
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • Using `-std=c11` fixes the problem I wrote of, but introduces the problem that it now gives `implicit declaration of function ‘siginterrupt’` even though I'm including `signal.h`. – Matthew Cline Nov 08 '14 at 06:31
  • Then change it to `-std=gnu11`…that allows most other declarations to be created when the `c11` variant doesn't. – Jonathan Leffler Nov 08 '14 at 06:32