0

Is there a flag I can pass to gcc to disable this warning? I am aware of what it does, but it does not matter for my program.

Edit: I would like to merely disable the warning, keeping the code as it is. Compiling the following code generates the warning:

struct post{
  unsigned int isImg : 1;
  struct img{
    char *name;
    char *url;
    int likes;
  };

  unsigned int isTxt : 1;
  struct text{
     char*text;
     int likes;
  };
  union Data{
    struct img Img;
    struct text Txt;
  } data;
};

The gcc version I am using is 5.4.0

Aneesh Durg
  • 464
  • 4
  • 14
  • @AnttiHaspala why would you need a code? – Michi Mar 18 '18 at 20:43
  • 1
    @Michi I was about to ask the same thing, but: I haven't succeeded in reproducing the OP's error message, so I haven't made any progress in figuring out how to turn it off. A sample of the OP's actual code, hopefully triggering the same specific warning, would help. – Steve Summit Mar 18 '18 at 21:02
  • @AneeshDurg Can you give us an example of a declaration that's triggering the warning? Can you tell us which version of gcc you're using? (Invoke `gcc --version` to find out.) – Steve Summit Mar 18 '18 at 21:15
  • Okay, I've added some code! – Aneesh Durg Mar 18 '18 at 21:23
  • Maybe you could `grep` the warning out of the result – M.M Mar 18 '18 at 22:00
  • Clang-LLVM shows the warning as “-Wmissing-declarations”, and using “-Wno-missing-declarations” disables it. [You could try the same switch with GCC.](https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html) That documentation says it is for global functions without prior declarations, but it is worth a try. – Eric Postpischil Mar 18 '18 at 22:20
  • 4
    You should fix your code [**as it has a constraint violation**](https://stackoverflow.com/questions/21610637/declaration-does-not-declare-anything-warning/49353894#49353894) i.e. it is not correct C code - and certainly not a compiler bug, @M.M. – Antti Haapala -- Слава Україні Mar 18 '18 at 23:35
  • @AnttiHaapala you are right... although I'd still say the behaviour following the message is somewhat unintuitive (it proceeds as if the struct type were declared and everything else is fine) – M.M Mar 18 '18 at 23:45
  • I see how this isn't correct C code, changing the way I'm generating this code is probably the best solution. – Aneesh Durg Mar 19 '18 at 00:56

1 Answers1

1

looking at:

struct post{
    unsigned int isImg : 1;
    struct img
    {
        char *name;
        char *url;
        int likes;
    };

    unsigned int isTxt : 1;

    struct text
    {
        char*text;
        int likes;
    };

    union Data
    {
        struct img Img;
        struct text Txt;
    } data;
};

I see a few problems:

  1. the definition of first 32 bit bitmap, only one bit defined
  2. the 'img' struct is 12 bytes+4bytes filler
  3. the definition of second 32 bit bitmap, only one bit defined
  4. the 'text' struct is 8 bytes+8bytes filler
  5. the union, composed of the two prior struct types
  6. all defined with an enclosing struct

This is very poor organization of the data declarations.

suggest:

struct img  //32bits
{
    char *name;
    char *url;
    int   likes;
};

struct text //32bits
{
     char *text;
     int   likes;
};

union Data  // 32bits
{
    struct img  Img;
    struct text Text;
};


struct post //5*32bits
{
  unsigned int isImg : 1;
  struct img   image;
  unsigned int isTxt : 1;
  struct text  Text;
  union Data   data;
};

even with the above (which compiles with no errors/warnings) there is still the problem that a bitmap ordering is implementation defined, so you do not know, without testing, if those defined bits are the MSB or the LSB of the bitmap.

user3629249
  • 16,402
  • 1
  • 16
  • 17