문제

I am trying to compile some C-code (written in 1993) and ironed out a few minor wrinkles but am stuck at one (15/16 objects compiled with 2 minor tweaks). Here's the code sending me an error:

#include "diagmesg.h"
#include <string.h>
#include <stdlib.h>

/* Definition of global variables */
FILE *  error_fp = stderr;

Here's diagmesg.h:

#ifndef DIAGMESG_H
#define DIAGMESG_H
#include <stdio.h>

/* extern int fprintf(); CHANGE 9/3 */

/* Global types */
/* Possible settings for the Diagnostic Reporting Level */
enum Diagnostic_Level   { NONE, ERROR, INFORM, DEBUG };

/* Declaration of global variables */
extern enum Diagnostic_Level    current_level;
extern FILE         *error_fp;

/* macros for doing Diagnostic Report */
#define ERROR_MSG( s )\
do{ if ( current_level >= ERROR  ) fprintf( error_fp, "(e)   %s\n", s); }while(0)
#define INFORM_MSG( s )\
do{ if ( current_level >= INFORM ) fprintf( error_fp, "(i)   %s\n", s); }while(0)
#define DEBUG_MSG( s )\
do{ if ( current_level >= DEBUG  ) fprintf( error_fp, "(d)   %s\n", s); }while(0)

/* buffer control */
#define BUFFER_BLOCK 32

extern void Buffer_MSG( enum Diagnostic_Level, char *);
extern void Flush_MSG( void );
#endif

Here's the (relevant) message from the compiler:

GNU C (GCC) version 4.5.3 (i686-pc-cygwin)
        compiled by GNU C version 4.5.3, GMP version 4.3.2, MPFR version 3.0.1-p4, MPC version 0.8
warning: MPFR header version 3.0.1-p4 differs from library version 3.1.2.
GGC heuristics: --param ggc-min-expand=99 --param ggc-min-heapsize=129520
ignoring nonexistent directory "/usr/local/include"
ignoring nonexistent directory "/usr/lib/gcc/i686-pc-cygwin/4.5.3/../../../../i686-pc-cygwin/include"
ignoring duplicate directory "/usr/lib/gcc/i686-pc-cygwin/4.5.3/../../../../i686-pc-cygwin/lib/../../include/w32api"
#include "..." search starts here:
#include <...> search starts here:
 /usr/lib/gcc/i686-pc-cygwin/4.5.3/include
 /usr/lib/gcc/i686-pc-cygwin/4.5.3/include-fixed
 /usr/include
 /usr/lib/gcc/i686-pc-cygwin/4.5.3/../../../../include/w32api
End of search list.
GNU C (GCC) version 4.5.3 (i686-pc-cygwin)
        compiled by GNU C version 4.5.3, GMP version 4.3.2, MPFR version 3.0.1-p4, MPC version 0.8
warning: MPFR header version 3.0.1-p4 differs from library version 3.1.2.
GGC heuristics: --param ggc-min-expand=99 --param ggc-min-heapsize=129520
Compiler executable checksum: 89d6774c1d510265da7d48b735ce61fb
diagmesg.c:17:1: error: initializer element is not constant
Makefile:101: recipe for target `diagmesg.o' failed
make: *** [diagmesg.o] Error 1

Line 17 in the source is the FILE * error_fp = stderr; statement shown above

I am not a routine C programmer. If anyone can clarify what "initializer element is not constant" means and/or a possible solution to compile would be very thankful.

도움이 되었습니까?

해결책 2

This is an issue with the system library.The file provided by the system library is providing a definition of stderr which is not a constant.

Earlier (i mean in old libraries) stderr was given as #define stderr _IO_stderr and now it is extern FILE *stderr. There is already some workaround exists like using custom stdio.h.For more info refer this.

다른 팁

It's up to your system how exactly stderr is defined but in most cases it will be a kind of variable and cannot be used to initialize error_fp in the declaration of a global variable the way it is done in your code. What you can do is find a place in your code before you use error_fp for the first time (by calling on of the ..._MSG() macros ore by using it directly) and initialize it there (e.g. in your main() function).

Edit: just for curiosity: I've just make a short test and could compile and run code like your's on two different solaris systems and on AIX, but not on Linux

The system library is giving a definition of stderr which is not constant. It is a problem when you assign stderr to a global variable error_fp, which requires a constant initializer.

As a solution, you can directly use stderr in the printf like:

fprintf( stderr, "(e)   %s\n", s);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top