warning in extern declaration

后端 未结 4 805
情歌与酒
情歌与酒 2020-11-29 12:12
#include
#include
#define GREY 1
#define BLACK 0
#define WHITE 2
typedef struct node * graph;
typedef struct stack * snode;

graph cno         


        
相关标签:
4条回答
  • 2020-11-29 12:30

    I just had this warning with gcc and it's a simple fix. stack_counter should not be initialised in the header, but rather in a source file that includes it. So the header should just use:

    extern int stack_counter;
    

    Then declare and initialise stack_counter in global scope of just one source file that includes that header:

    int stack_counter = 0;
    
    0 讨论(0)
  • 2020-11-29 12:42

    While your code contains a number of rather serious and obvious errors (already covered in other answers), the warning you put into the title of your question is a completely superfluous meaningless warning. GCC compiler is notorious for issuing useless warnings. Many of those warnings seem to be derived from someone's incompetent and completely unsubstantiated belief that doing something is somehow "wrong", while in reality there's nothing wrong with it.

    In your case the warning is triggered by

    extern int stack_counter = 0;
    

    declaration. Apparently, the "author" of the warning believed that extern specifier should be reserved for non-defining declarations. In this case the presence of initializer = 0 turns the declaration into a definition (and thus formally makes that extern optional). Nevertheless, there's no error in it and, in fact, extern might be quite welcome here to emphasize the fact that stack_counter is intended to be a global variable.

    Again, whether you need a global variable here or not is a different question and, again, your code contains a massive number of other errors. But the warning you seem to focus your attention on is not really worth it. Just disable this warning in compiler settings (and, please, write a rude letter about it to GCC team).

    0 讨论(0)
  • 2020-11-29 12:49

    The extern declaration in your header file lets modules other than the one in which the variable is defined use it. If it's supposed to be defined as int stack_counter = 0 and live in stack.c, define it like that and put an extern stack_counter in the header.

    On line 6 of stack.c, you didn't define a storage class for sroot. Since it's externed in the header, I assume you meant to type snode sroot=NULL.

    Fix those, then implement stackpush (make sure it doesn't return void) and deal with the rest of your warnings in order. Note that in C, you have to either use forward declarations of functions (with prototypes) or define your functions before they're used. The cstack function should probably be the last function in the file.

    0 讨论(0)
  • 2020-11-29 12:49

    Clang still issues a warning for this. The line

    extern int counter = 0;
    

    will trigger the warning:

    warning: 'extern' variable has an initializer [-Wextern-initializer]

    This warning is not important, as defining the variable with

    int counter = 0;
    

    still yields a static duration and external linkage by default. Indeed, if no storage-class specifier is provided, the defaults are:

    • extern for all functions
    • extern for objects at file scope
    • auto for objects at block scope

    There is also something called a tentative definition which is an external declaration without an initializer, and either without a storage-class specifier or with the specifier static.

    A tentative definition is a declaration that may or may not act as a definition. If an actual external definition is found earlier or later in the same translation unit, then the tentative definition just acts as a declaration.

    So the following line

    int counter;
    

    is a tentative definition that declares and defines counter with the implicit initializer = 0 (or, for array, structure, and union types, = {0}).

    0 讨论(0)
提交回复
热议问题