Re: Compiling Error

2000-03-23 Thread Peter Cordes

 Date: Wed, 22 Mar 2000 14:55:58 +0100
 From: Marcus Brinkmann [EMAIL PROTECTED]
 To: Michal Fecanin Araujo [EMAIL PROTECTED]
 Cc: Debian Development List debian-devel@lists.debian.org
 Subject: Re: Compiling Error
 
 On Wed, Mar 22, 2000 at 01:15:01PM +, Michal Fecanin Araujo wrote:
  --
  #include stdio.h
  
  FILE *output=stderr;
  
  int main()
  {
fprintf(output,Hello World\n);
  }
  --
  
  The problem is that its not possible to initialize output with stderr
  because it is not a constant. Is there any solution to this without
  modification of the code, only with gcc options.
 
 No. Your code is invalid, and it should be fixed. You already know the fix.
 Now you just have to accept it.


 This is an interesting problem which was raised on the pgcc mailing list
last year.  Check out my reply to someone's question:
http://www.delorie.com/archives/browse.cgi?p=pgcc/1999/10/23/23:19:18
and Marc Lehmann's answer:
http://www.delorie.com/archives/browse.cgi?p=pgcc/1999/10/24/19:18:25

summary:  C is annoying like that, but if you want to have 

static FILE *errfile = stderr;

in a library, but have it be legal C (!), you can use GNU C constructors, or
you can 
static FILE *errfile = NULL;
#define errfile (errfile?errfile:stderr)

This is easier than explicitly checking at the top of every function which
uses it.  I ran into this problem when hacking the Linux-lab-project
http://www.llp.fu-berlin.de/ GPIB drivers for a glibc 2.1 system (rh6.0).

-- 
#define X(x,y) x##y
DUPS Secretary ; http://is2.dal.ca/~dups/
Peter Cordes ;  e-mail: X([EMAIL PROTECTED] , dal.ca)

The gods confound the man who first found out how to distinguish the hours!
 Confound him, too, who in this place set up a sundial, to cut and hack
 my day so wretchedly into small pieces! -- Plautus, 200 BCE



Re: Compiling Error

2000-03-22 Thread Ben Collins
Read the gnu libc FAQ, it contains info about why this happens, and it
pretty much explains that the first example you show is not valid ANSI C.
So in affect, it is a bug in the program to do that (since std{out,in,err}
are not really constants, but are runtime dependant).

The example you show below is the needed change, and I've had to do it
many times when we made the move to glibc 2.1 on sparc. It is correct, and
portable.

-- 
 ---===-=-==-=---==-=--
/  Ben Collins  --  ...on that fantastic voyage...  --  Debian GNU/Linux   \
` [EMAIL PROTECTED]  --  [EMAIL PROTECTED]  --  [EMAIL PROTECTED] '
 `---=--===-=-=-=-===-==---=--=---'



Re: Compiling Error

2000-03-22 Thread Marcus Brinkmann
On Wed, Mar 22, 2000 at 01:15:01PM +, Michal Fecanin Araujo wrote:
 --
 #include stdio.h
 
 FILE *output=stderr;
 
 int main()
 {
   fprintf(output,Hello World\n);
 }
 --
 
 The problem is that its not possible to initialize output with stderr
 because it is not a constant. Is there any solution to this without
 modification of the code, only with gcc options.

No. Your code is invalid, and it should be fixed. You already know the fix.
Now you just have to accept it.

 The following modification is obvius and Iam not interested in it.

Tough.

Thanks,
Marcus
 
 --
 #include stdio.h
 
 FILE *output;
 
 int main()
 {
output=stderr;
fprintf(output,Hello World\n);
 }
 ---