How to remove warning in gcc 4.6: missing initializer [-Wmissing-field-initializers]?

Code:

GValue value = { 0 }; 

Provide the following warning:

missing initializer [-Input field indicators]

I know gcc BUG ; but is there any trick to remove it? it’s really not nice to see such unrealistic warnings. But I do not want to turn off the warning, because it also hides real warnings from me. Sorry, but I still can not upgrade the version of gcc to 4.7 (where it seems to be fixed).

+4
source share
3 answers

Use G_VALUE_INIT to initialize GValue -s. Their (private) structure is in /usr/include/glib-2.0/gobject/gvalue.h , which is #define G_VALUE_INIT respectively.

I strongly disagree with your assessment that this is a GCC error. You ask for a warning if the field is not explicitly initialized with -Wmissing-field-initializers , and you get the warning you deserve.

Sadly G_VALUE_INIT not documented, but it is here. Code with

 GValue value = G_VALUE_INIT; 

There is no one-stop solution to never receive a warning about the lack of field initialization if -Wmissing-field-initializers is specified. When you request such a warning, you require the compiler to warn about each incomplete initializer. In fact, the standard requires that all implicitly initialized struct fields be nullified and gcc obeys the standard.

You can use diagnostic pragmas , for example

 #pragma GCC diagnostic ignored "-Wmissing-field-initializers" 

But I feel that you should code with caution and explicitly initialize all fields. The warning you get is more likely to warn about the coding style (you may have forgotten the field!) Than the warning about the error.

I also believe that for your own (public) struct you must have a #define initializing macro if such struct should be initialized.

+7
source

You can use:

 -Wno-missing-field-initializers 

to block this warning. Conversely, you can do this with an error:

 -Werror=missing-field-initializers 

Both of them work with GCC 4.7.1; I believe that they also work with GCC 4.6.x, but they do not work with all earlier versions of GCC (GCC 4.1.2 recognizes -Wno-missing-field-initializers , but not -Werror=missing-field-intializers ).

Obviously, another way to suppress a warning is to explicitly initialize all fields. However, this can be painful.

+10
source

It also seems to use .field field style initialization, for example:

 GValue value = { .somefield = 0 }; 

will cause the compiler to not generate a warning. Unfortunately, if the structure is opaque, it is not a starter.

+1
source

Source: https://habr.com/ru/post/1445925/


All Articles