Re: struct question

From: CBFalconer (cbfalconer_at_yahoo.com)
Date: 08/05/04


Date: Thu, 05 Aug 2004 13:09:49 GMT

pete wrote:
>
... snip ...
>
> For matched pairs of .c and .h files:
> I am inclined not to put extern object declarations in a header.
> I think they belong in the .c file.
> If a.c used "extern struct e d", any other .c file linking with a.c,
> wouldn't need to know which external objects were being used in a.c,
> so therefore "extern struct e d",
> should be declared in a.c, instead of in a.h.

You won't go wrong if you consider the .h file as a means of
publicizing those portions of the .c file you want to make
accessible to other compilation units. Thus you declare and
define the objects in the .c file. If the objects are NOT to be
accessible externally you mark them static. If they ARE to be
accessible externally you declare them in the .h file, and use the
extern keyword for objects other than function prototypes.

Similarly you put typedefs and macros in the .h file ONLY when you
need to have them available to other compilation units.

-- 
"I'm a war president.  I make decisions here in the Oval Office
 in foreign policy matters with war on my mind." -         Bush.
"Churchill and Bush can both be considered wartime leaders, just
 as Secretariat and Mr Ed were both horses." -     James Rhodes.
"If I knew then what I know today, I would still have invaded
 Iraq. It was the right decision" -       G.W. Bush, 2004-08-02


Relevant Pages

  • Re: const and extern...
    ... extern float bar; ... a declaration), but if you merely declare it, you do not define it. ... or the translation unit ends. ... an actual definition with an initializer of: ...
    (comp.lang.c)
  • 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: How do header files work?
    ... declare some constants, variables, and functions that have ... for is the "extern" linkage keyword (which nobody has mentioned ... following source files in my directory: ... That's because the compiler for your library.c file doesn't need ...
    (comp.lang.c)
  • Re: Exported function mangaled name
    ... extern "C" { ... You have otherwise defined a function unrelated to the header ... You have to declare the function the same way in your .cpp file as in your .h file ... #define LIBSPEC __declspec ...
    (microsoft.public.vc.mfc)
  • 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)