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: FILE *bla=stderr;



> The above line gives me the
> 
>   1.c:3: initializer element is not constant
> 
> error with egcs under Linux/AXP but it works with egcs under Linux/SPARC
> and Linux/Intel.  What's up?

Are you using glibc 2.1 snapshots on Linux/AXP (e.g. glibc 2.0.94)?
In that case everything is clear - just read the FAQ;-).  I've
appended the relevant section.

Andreas


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; }



-- 
 Andreas Jaeger   aj@arthur.rhein-neckar.de    jaeger@informatik.uni-kl.de
  for pgp-key finger ajaeger@alma.student.uni-kl.de


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