470,594 Members | 1,120 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,594 developers. It's quick & easy.

problem compiling related to extern

I am trying to compile the program hull
http://cm.bell-labs.com/netlib/voronoi/hull.html

This is quite a complicated program that involves several .c files

I get several warnings during compilation and the error:
hullmain.c:40: initializer element is not constant

This is the offending line:
FILE *INFILE, *OUTFILE, *DFILE = stderr, *TFILE;

It is part of many lines of stuff that occur before main()

hullmain.c includes hull.h, and in hull.h I find:
FILE* efopen(char *, char *);
extern FILE *DFILE;

I think the mysterious "initializer element" error is related to
*DFILE = stderr
in hullmain.c

If I remove this bit I can get the program to compile (but it segfaults
when I run it :-)

What is the proper way to initialize the pointer *DFILE to be stderr?

If anyone can offer any tips on what is wrong and how to correct it,
I'd greatly appreciate it.

Thanks!
Bill

Nov 14 '05 #1
6 1371
johnnie...@techie.com wrote:
I am trying to compile the program hull
http://cm.bell-labs.com/netlib/voronoi/hull.html

This is quite a complicated program that involves several .c files

I get several warnings during compilation and the error:
hullmain.c:40: initializer element is not constant

This is the offending line:
FILE *INFILE, *OUTFILE, *DFILE = stderr, *TFILE;

It is part of many lines of stuff that occur before main()

hullmain.c includes hull.h, and in hull.h I find:
FILE* efopen(char *, char *);
extern FILE *DFILE;

I think the mysterious "initializer element" error is related to
*DFILE = stderr
in hullmain.c

If I remove this bit I can get the program to compile (but it segfaults when I run it :-)

What is the proper way to initialize the pointer *DFILE to be stderr?

If anyone can offer any tips on what is wrong and how to correct it,
I'd greatly appreciate it.

Thanks!
Bill


On some systems stderr is not a constant. The correct way to do this
would be to move the _initialization_ to main before DFILE is used.

Rob Gamble

Nov 14 '05 #2


jo********@techie.com wrote:
I am trying to compile the program hull
http://cm.bell-labs.com/netlib/voronoi/hull.html

This is quite a complicated program that involves several .c files

I get several warnings during compilation and the error:
hullmain.c:40: initializer element is not constant

This is the offending line:
FILE *INFILE, *OUTFILE, *DFILE = stderr, *TFILE;

It is part of many lines of stuff that occur before main()

hullmain.c includes hull.h, and in hull.h I find:
FILE* efopen(char *, char *);
extern FILE *DFILE;

I think the mysterious "initializer element" error is related to
*DFILE = stderr
in hullmain.c
You're right. The initializer for a file-scope variable
(or for a function-local `static' variable) must be a compile-
time constant, because it must produce its value before the
program actually starts running. All three `stdxxx' streams
are predefined by the implementation, but on some implementations
their definitions are not compile-time constants. Hence the
error.
If I remove this bit I can get the program to compile (but it segfaults
when I run it :-)
At a guess, this is because the program tries to use DFILE,
expecting it to be a valid `FILE*' pointer -- but without an
initiazlizer, DFILE will be initialized to NULL ...
What is the proper way to initialize the pointer *DFILE to be stderr?

If anyone can offer any tips on what is wrong and how to correct it,
I'd greatly appreciate it.


"Initialize" DFILE by assigning to it before it is used,
probably very early in main():

FILE *DFILE;
int main(...) {
DFILE = stderr;
...
}

An alternative approach would be to define DFILE as a macro
that expands to stderr:

/* FILE *DFILE = stderr; -- removed */
#define DFILE stderr

--
Er*********@sun.com

Nov 14 '05 #3
Eric Sosman wrote:
jo********@techie.com wrote:
I am trying to compile the program hull
http://cm.bell-labs.com/netlib/voronoi/hull.html

This is quite a complicated program that involves several .c files

I get several warnings during compilation and the error:
hullmain.c:40: initializer element is not constant

This is the offending line:
FILE *INFILE, *OUTFILE, *DFILE = stderr, *TFILE;

It is part of many lines of stuff that occur before main()

hullmain.c includes hull.h, and in hull.h I find:
FILE* efopen(char *, char *);
extern FILE *DFILE;

I think the mysterious "initializer element" error is related to
*DFILE = stderr
in hullmain.c


You're right. The initializer for a file-scope variable
(or for a function-local `static' variable) must be a compile-
time constant, because it must produce its value before the
program actually starts running. All three `stdxxx' streams
are predefined by the implementation, but on some implementations
their definitions are not compile-time constants. Hence the
error.
If I remove this bit I can get the program to compile (but it segfaults when I run it :-)


At a guess, this is because the program tries to use DFILE,
expecting it to be a valid `FILE*' pointer -- but without an
initiazlizer, DFILE will be initialized to NULL ...
What is the proper way to initialize the pointer *DFILE to be stderr?
If anyone can offer any tips on what is wrong and how to correct it, I'd greatly appreciate it.


"Initialize" DFILE by assigning to it before it is used,
probably very early in main():

FILE *DFILE;
int main(...) {
DFILE = stderr;
...
}

An alternative approach would be to define DFILE as a macro
that expands to stderr:

/* FILE *DFILE = stderr; -- removed */
#define DFILE stderr


Of course if the program ever attempts to modify DFILE this would cause
a problem.

Rob Gamble

Nov 14 '05 #4
jo********@techie.com wrote:
I am trying to compile the program hull
http://cm.bell-labs.com/netlib/voronoi/hull.html

This is quite a complicated program that involves several .c files

I get several warnings during compilation and the error:
hullmain.c:40: initializer element is not constant

This is the offending line:
FILE *INFILE, *OUTFILE, *DFILE = stderr, *TFILE;

It is part of many lines of stuff that occur before main()

hullmain.c includes hull.h, and in hull.h I find:
FILE* efopen(char *, char *);
extern FILE *DFILE;

I think the mysterious "initializer element" error is related to
*DFILE = stderr
in hullmain.c

If I remove this bit I can get the program to compile (but it segfaults
when I run it :-)

What is the proper way to initialize the pointer *DFILE to be stderr?

If anyone can offer any tips on what is wrong and how to correct it,
I'd greatly appreciate it.

Thanks!
Bill


Probably because we can't find stdio.h where stderr is '#define'ed.
--
Joe Wright mailto:jo********@comcast.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #5
Joe Wright <jo********@comcast.net> writes:
jo********@techie.com wrote:
I am trying to compile the program hull
http://cm.bell-labs.com/netlib/voronoi/hull.html
This is quite a complicated program that involves several .c files
I get several warnings during compilation and the error:
hullmain.c:40: initializer element is not constant
This is the offending line:
FILE *INFILE, *OUTFILE, *DFILE = stderr, *TFILE;
[...] Probably because we can't find stdio.h where stderr is '#define'ed.


Doubtful. If <stdio.h> weren't included, the compiler would complain
that FILE and stderr are undeclared. (Actually, due to the way
typedefs are handled, it would probably get a parse error.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #6
Thanks Eric, and everyone else who suggested this!
Now the program compiles properly.

"Initialize" DFILE by assigning to it before it is used,
probably very early in main():

FILE *DFILE;
int main(...) {
DFILE = stderr;
...
}


Cheers
Bill

Nov 14 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Gary Stephenson | last post: by
3 posts views Thread by Christopher M. Lusardi | last post: by
13 posts views Thread by Amadeus W. M. | last post: by
2 posts views Thread by ajikoe | last post: by
23 posts views Thread by Marco | last post: by
3 posts views Thread by Rene | last post: by
10 posts views Thread by Jaco Naude | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.