Using stderr in an initialization?

Bakul Shah bakul at bitblocks.com
Fri May 2 21:03:54 UTC 2008


On Fri, 02 May 2008 13:23:56 PDT Steve Kargl <sgk at troutmask.apl.washington.edu>  wrote:
> I'm porting a piece of code to FreeBSD, and I've run into
> a problem that I currently don't know how to solve. I scanned
> both the Porter's Handbook and the Developer's Handbook, but
> came up empty.
> 
> A reduce testcase is
> 
> #include <stdio.h>
> 
> typedef FILE *FILEP;
> 
> static FILEP outfile = {stderr};
> 
	...
> GCC gives
> 
> troutmask:sgk[204] cc -o z a.c
> a.c:5: error: initializer element is not constant
> a.c:5: error: (near initialization for 'outfile')
> 
>                                     So, anyone have a
> suggestion on how to change line 5 to satisfy gcc?

It *used* to be the case that stderr was a macro referring to
something like &_iob[2] which is a link time constant
expression.  As per section 7.19.1 in the C standard, the
stderr macro is an expression of type `pointer to file' but
not a constant.  You wouldn't expect the following to work,
would you?

    FILE* f;
    FILE* outfile = f;

It is the exact same thing.  But you can do

    static FILE** _outfile = &stderr;
    #define outfile (*_outfile)

to achive the effect you want.


More information about the freebsd-ports mailing list