On Sun, Aug 20, 2017 at 1:50 PM, Aleksander Morgado < [email protected]> wrote:
> On Sun, Aug 20, 2017 at 7:09 PM, Ben Chan <[email protected]> wrote: > > > > > > On Sun, Aug 20, 2017 at 9:37 AM, Aleksander Morgado > > <[email protected]> wrote: > >> > >> Using an intermediate constant variable breaks compilation with C > >> compilers, as these variables cannot be used as initializers. > >> > > > > Aleksander, do you have the compiler version and the code snippet that > > causes the compiler error? > > > > Here's an example: > > /* test.c */ > #include <stdio.h> > #include <ModemManager.h> > > typedef struct { > MMModemBand band; > char *str; > } BandName; > > static const BandName names[] = { > { .band = MM_MODEM_BAND_EUTRAN_XLII, "e-utran 43" }, > }; > > int main(int argc, char **argv) > { > guint i; > > for (i = 0; i < G_N_ELEMENTS (names); i++) > printf ("band '%s' has id %u\n", names[i].str, names[i].band); > return 0; > } > > Compilation fails with "error: initializer element is not constant": > $ gcc -o test `pkg-config --cflags --libs ModemManager glib-2.0` test.c > test.c:12:2: warning: ‘MM_DEPRECATED_MODEM_BAND_EUTRAN_XLII’ is > deprecated: Use 'MM_MODEM_BAND_EUTRAN_42' instead > [-Wdeprecated-declarations] > { .band = MM_MODEM_BAND_EUTRAN_XLII, "e-utran 43" }, > ^ > In file included from /usr/include/ModemManager/ModemManager.h:41:0, > from test.c:4: > /usr/include/ModemManager/ModemManager-compat.h:441:18: note: declared > here > static const int MM_DEPRECATED_MODEM_BAND_EUTRAN_XLII = > MM_MODEM_BAND_EUTRAN_42; > ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > /usr/include/ModemManager/ModemManager-compat.h:442:35: error: > initializer element is not constant > #define MM_MODEM_BAND_EUTRAN_XLII MM_DEPRECATED_MODEM_BAND_EUTRAN_XLII > ^ > /usr/include/ModemManager/ModemManager-compat.h:442:35: note: in > definition of macro ‘MM_MODEM_BAND_EUTRAN_XLII’ > #define MM_MODEM_BAND_EUTRAN_XLII MM_DEPRECATED_MODEM_BAND_EUTRAN_XLII > ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > /usr/include/ModemManager/ModemManager-compat.h:442:35: note: (near > initialization for ‘names[0].band’) > #define MM_MODEM_BAND_EUTRAN_XLII MM_DEPRECATED_MODEM_BAND_EUTRAN_XLII > ^ > /usr/include/ModemManager/ModemManager-compat.h:442:35: note: in > definition of macro ‘MM_MODEM_BAND_EUTRAN_XLII’ > #define MM_MODEM_BAND_EUTRAN_XLII MM_DEPRECATED_MODEM_BAND_EUTRAN_XLII > ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > > $ gcc --version > gcc (GCC) 7.1.1 20170630 > > For what I could gather browsing; using constant variables as > initializers is valid C++, but not C. The approach I took for the > patch is what the GTK+ did when deprecating GtkStock macros (e.g. > https://github.com/GNOME/gtk/blob/7173df1dd1a8f45c47b75d30bfbfaa > 67f33d8e9b/gtk/deprecated/gtkstock.h#L102) > lgtm. Yeah, unfortunately, it seems like C has a different rule for constant expressions (than C++). A simple expression like this fails to compile with gcc (even in c99, c11), but succeeds with clang: static const int a = 1; static const int b = a; /* error: initializer element is not constant */ Seems a bit subtle that `static const int a = 1` isn't regarded as a constant expression. It may be related to whether there is any guarantee on the initialization order. > > -- > Aleksander > https://aleksander.es >
_______________________________________________ ModemManager-devel mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/modemmanager-devel
