problem compiling related to extern

J

johnnieboy

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
 
T

tigervamp

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
 
E

Eric Sosman

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
 
T

tigervamp

Eric said:
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.


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 ...


"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
 
J

Joe Wright

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.
 
K

Keith Thompson

Joe Wright said:
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.)
 
J

johnnieboy

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,162
Messages
2,570,896
Members
47,434
Latest member
TobiasLoan

Latest Threads

Top