Re: keyword extern

From: Yashesh Bhatia (yasheshb_at_gmail.com)
Date: 06/26/04


Date: 26 Jun 2004 00:41:45 -0700

tweak <xbwaichunasx@cox.net> wrote in message news:<0a3Dc.3$151.1@fed1read02>...
> What's the best way to use extern when using multiplefiles that is
> easiest to maintain?
>
> Is it best to declare:
>
> extern int a;
>
> in a header file and include the header file in all files except
> where it's defined.
>
> Or is it better to write
>
> extern int a;
>
> in all the files except where it's originally defined?
>
> I'm just curious what the best practice is since I code
> as a hobby. And I'm still learning and making lotsa
> mistakes.
>
> Thanks,
>
> Brian
>
> P.S. Thanks to whomever recommended Advanced Unix Programming
> Second Edition. It's cleared up a lot for me. And it's
> making me question as to whether I should drop linux and
> move to openBSD.

Well, my preferred way would be to declare it in all the .c files that
need the extern variable rather than put the extern declaration in the
header file and include the header file in the c files. The reason
being.

 - If the variable was used by all c files, it would be better to make
it a global variable in the .h file.

 - Keeping the extern declaration in the .c file instead of the .h
file will make it easier for other .c files that dont need the extern
variable but need other declarations from the .h file to include it.



Relevant Pages

  • Re: confused about extern use
    ... going to achieve by declaring it as extern in the header file a.h. ... composed on one or more compilation units. ... Essentially you *declare* the type wherever it is ...
    (comp.lang.c)
  • Re: keyword extern
    ... > declared multiple times using extern in all the files file2.c ... NEVER, ever, define a variable in a header file. ... declare it with the "extern" qualifier in the header file for the ... misuse. ...
    (comp.lang.c)
  • Re: Exported function mangaled name
    ... with many DLLs and supporting this for Windows 32 bit ... that header file; ... You have to declare the function the same way in your .cpp file as in ... extern "C" { ...
    (microsoft.public.vc.mfc)
  • Re: How to declare functions in so to be accessable to applications
    ... >> But the application tells my, that it didn't find the entry point ... > Your header file that the application uses, ... > prototype normally, not as an extern. ... When you declare it extern, ...
    (comp.unix.programmer)
  • Re: keyword extern
    ... my preferred way would be to declare it in all the .c files that ... > need the extern variable rather than put the extern declaration in the ... > header file and include the header file in the c files. ... A header is certainely not the place for an object definition for an ...
    (comp.lang.c)