Search code examples
cgccc89

warning: initializer element is not computable at load time


I fail to understand gcc -pedantic output in the following scenario:

$ gcc -pedantic parse.c -lpopt
parse.c: In function ‘main’:
parse.c:19:7: warning: initializer element is not computable at load time
       { "bps", 'b', POPT_ARG_INT, &speed, 0,
       ^
parse.c:20:7: warning: initializer element is not computable at load time
       "signaling rate in bits-per-second", "BPS" },
       ^
parse.c:27:7: warning: initializer element is not computable at load time
       { "raw", 'r', 0, &raw, 0,
       ^
parse.c:28:7: warning: initializer element is not computable at load time
       "don't perform any character conversions" },
       ^

Using the following C code (taken from here):

$ cat parse.c
#include <popt.h>

int main(int argc, const char *argv[]) {
  char    c;            /* used for argument parsing */
  int     speed = 0;    /* used in argument parsing to set speed */
  int     raw = 0;      /* raw mode? */
  struct poptOption optionsTable[] = {
      { "bps", 'b', POPT_ARG_INT, &speed, 0,
      "signaling rate in bits-per-second", "BPS" },
      { "crnl", 'c', 0, 0, 'c',
      "expand cr characters to cr/lf sequences" },
      { "hwflow", 'h', 0, 0, 'h',
      "use hardware (RTS/CTS) flow control" },
      { "noflow", 'n', 0, 0, 'n',
      "use no flow control" },
      { "raw", 'r', 0, &raw, 0,
      "don't perform any character conversions" },
      { "swflow", 's', 0, 0, 's',
      "use software (XON/XOF) flow control" } ,
      POPT_AUTOHELP
        { NULL, 0, 0, NULL, 0 }
  };
}

The very same code does compile with either -ansi or -std=c89. Why is the -pedantic option failing ?

Using:

$ gcc --version
gcc (Debian 4.9.1-19) 4.9.1
Copyright (C) 2014 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

and

$ apt-cache policy libpopt-dev
libpopt-dev:
  Installed: 1.16-10
  Candidate: 1.16-10
  Version table:
 *** 1.16-10 0
        500 http://ftp.fr.debian.org/debian/ jessie/main amd64 Packages
        100 /var/lib/dpkg/status

Solution

  • In C89 standard, initializer list must be compile time constants expressions, hence the warnings. However in C99 it is supported.

    In C89, you can do:

    struct poptOption optionsTable[2];
    
    optionsTable[0] = {"bps", 'b', POPT_ARG_INT, &speed, 0,
      "signaling rate in bits-per-second", "BPS" };
    optionsTable[1] = {"crnl", 'c', 0, 0, 'c',
      "expand cr characters to cr/lf sequences" };