Question

I have a file global.h which is included across many files in the project and contain general headers. The relevant contents of file is given below:

#define DEBUG
#ifdef DEBUG
extern int debug_level;
#endif

It has been included in main.c and there is a warning corresponding to the following line in main.c

#ifdef DEBUG            
debug_level = 6;   //compiler generates warning corresponding to this line.
#endif

The warning message issued by compiler is:

src/main.c:14:1: warning: data definition has no type or storage class [enabled by default]
src/main.c:14:1: warning: type defaults to ‘int’ in declaration of ‘debug_level’ [enabled by default]

I do not understand what is that I am doing wrong. Surprisingly the program works fine because I think that compiler assumes that the number is an int(by default).

Was it helpful?

Solution

You should define as int as

#ifdef DEBUG            
int debug_level = 6;   //define as int
#endif

With your code, its implicitly defined as int, hence the warning.

And extern int debug_level; is not definition but a declaration.

OTHER TIPS

You can't just set the variable in global scope, you actually have make a definition that matches the declaration in the header file:

#ifdef DEBUG            
int debug_level = 6;
#endif

Declare the variable debug_level as external if it is already declared some where else. Then the compiler will look for the declaration on other places also.

#ifdef DEBUG            
external int debug_level = 6;
#endif
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top