[sheepdog] [PATCH] sheep: remove unnecessary extern keyword
MORITA Kazutaka
morita.kazutaka at lab.ntt.co.jp
Thu Jan 24 09:26:25 CET 2013
At Thu, 24 Jan 2013 16:03:43 +0800,
Liu Yuan wrote:
>
> On 01/24/2013 03:54 PM, MORITA Kazutaka wrote:
> > At Thu, 24 Jan 2013 15:43:30 +0800,
> > Liu Yuan wrote:
> >>
> >> On 01/24/2013 03:24 PM, Liu Yuan wrote:
> >>> On 01/24/2013 03:18 PM, MORITA Kazutaka wrote:
> >>>> Is removing extern from global variables really correct?. It means
> >>>> that the variables are defined in all .c files which include the
> >>>> header. Actually, GCC complains the error when the global variables
> >>>> are initialized.
> >>>
> >>> Umm, I used sed to replace all the 'extern' and fixed some warnings, and
> >>> yeah,global variables have this problem. My mistake.
> >>>
> >>
> >> But my GCC (4.6.3) don't complain on the missing extern for global
> >> variables. I guess new GCC might have some magic to handle this
> >> duplicate definition of global variables. Anyway, I've posted a patch to
> >> fix it.
> >
> > So even if we define the same global name in different sources, GCC
> > doesn't complain it? I think It can easily cause bugs and looks like
> > we need a mechanism to detect it.
> >
>
> I double checked it on my box, it seems that GCC don't complain it at
> all for such case:
>
> A.c: int test;
> B.c: int test = 1;
We usually omit zero initialization for global and static variables,
so developers may regards that 'test' is initialized with zero in A.c.
This can easily lead to bugs and I think should be detected by GCC,
but,
>
> But complains for:
> A.c: int test = 1;
> B.c: int test = 2;
>
>
> How about your environment? I guess both variable and functions are
unfortunately, the result on my environment is same to you.
> default as 'extern', then it seems that is syntax safe to move 'extern'
> for variable and functions. And multiple definition will be dected by
> GCC at link phase.
I tested the following patch:
==
diff --git a/sheep/sheep_priv.h b/sheep/sheep_priv.h
index b19ca03..6e49448 100644
--- a/sheep/sheep_priv.h
+++ b/sheep/sheep_priv.h
@@ -205,7 +205,7 @@ static inline struct store_driver *find_store_driver(const char *name)
extern struct cluster_info *sys;
extern struct store_driver *sd_store;
-extern char *obj_path;
+char *obj_path = NULL;
extern char *jrnl_path;
extern char *epoch_path;
extern mode_t def_fmode;
==
GCC complains the error about this since the global variable is
initialized in multiple .c files.
So we need a rule like "you cannot initialize variables in .h". Then,
I think adding extern is better in .h because no one tries to
initialize extern variables.
Thanks,
Kazutaka
More information about the sheepdog
mailing list