This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: [Fwd: Initializer is not constant?]


At 05:23 04.03.99 , Jason Eager wrote:

>Message-ID: <36DE051E.A9B8C073@po.cwru.edu>
>Date: Thu, 04 Mar 1999 03:59:27 +0000
>From: Jason Eager <jce2@po.cwru.edu>
>X-Mailer: Mozilla 4.06 [en] (X11; U; Linux 2.2.1 i686)
>MIME-Version: 1.0
>To: egcs-bugs@egcs.cygus.com
>Subject: Initializer is not constant?
>Content-Type: text/plain; charset=us-ascii
>Content-Transfer-Encoding: 7bit
>
>This bug causes the new Shelf release from Applixware to not compile:
>
>FILE *yyin = {stdin}, *yyout = {stdout};
>
>causes this error message.
>
>/usr/src/Shelf/src/amake/cpr_lex.h:86: initializer element for `yyin' is
>not constant
>/usr/src/Shelf/src/amake/cpr_lex.h:86: initializer element for `yyout'
>is not constant.
>
>If this is "bad" code, how do I work around this?

This is no egcs issue, this is glibc-2.1 specific. This code breaks cause 
it makes invalid assumptions about stdin/stdout/stderr.

Section 3.9 of the glibc-2.1 FAQ reads:

3.9.    I get compiler messages "Initializer element not constant" with
        stdin/stdout/stderr. Why?

{RM,AJ} Constructs like:
static FILE *InPtr = stdin;

lead to this message.  This is correct behaviour with glibc since stdin is
not a constant expression.  Please note that a strict reading of ISO C does
not allow above constructs.

One of the advantages of this is that you can assign to stdin, stdout, and
stderr just like any other global variable (e.g. `stdout = my_stream;'),
which can be very useful with custom streams that you can write with libio
(but beware this is not necessarily portable).  The reason to implement it
this way were versioning problems with the size of the FILE structure.

To fix those programs you've got to initialize the variable at run time.
This can be done, e.g. in main, like:

static FILE *InPtr;
int main(void)
{
  InPtr = stdin;
}

or by constructors (beware this is gcc specific):

static FILE *InPtr;
static void inPtr_construct (void) __attribute__((constructor));
static void inPtr_construct (void) { InPtr = stdin; }


Franz.



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]