Re: How to...

From: Fons Rademakers (Fons.Rademakers@cern.ch)
Date: Fri Feb 27 1998 - 18:43:38 MET


Hi Robert,

   never put the global definition in a header file. In the header
always put an extern declaration. Only in the implementation you
should put the definition. In that way your headers are save for inclusion
in different source files and you are guaranteed to only have one
definition.

So:

<hades.h>

extern Hades *gHades;


<hades.cxx>

Hades *gHades;


Cheers, Fons.

PS: to make your headers Windows compatible ROOT defines the macro EXTERN.
    Using this macro the global will also be correctly exported from
    Windows DLL's. So best is to use:

    EXTERN Hades *gHades;

    in your header files.




Robert Casties wrote:
> 
> [Posted and mailed]
> 
> In article <34F6BE78.19188CE0@cern.ch>,
>         Fons Rademakers <Fons.Rademakers@cern.ch> writes:
> [...]
> >    to make a compiled global visibile from the interpreter add:
> >
> > #pragma link C++ global gHades;
> >
> > in your LinkDef.h file. See also:
> 
> You will also have to trick the linker when trying to compile this into a
> shared library to be used in cint.
> 
> Usually you have two object files, one from the dictionary generated by
> rootcint xxxdict.C and your code xxx.C which both include the global
> variable declaration in your header file xxx.h. The linker then complains
> about this doubly defined object (this is fine with classes and
> functions).
> 
> So you have to change your header file to declare your global variable as
> external when included from one of the header files (but not both!).
> 
> I used:
> 
> <xxx.h>:
> //
> // some globals
> //
> #ifndef __noglobals__
> Int_t gNevent = 0;  // number of events to process (0=all)
> #else
> extern Int_t gNevent;
> #endif
> 
> <xxx.C>:
> #define __noglobals__
> #include "xxx.h"
> 
> So xxxdict.o (wich by default includes your header file) declares the
> globals and xxx.o uses them.
> 
> Cheers
>         Robert

-- 
Org:    CERN, European Laboratory for Particle Physics.
Mail:   1211 Geneve 23, Switzerland          Phone: +41 22 7679248
E-Mail: Fons.Rademakers@cern.ch              Fax:   +41 22 7677910



This archive was generated by hypermail 2b29 : Tue Jan 04 2000 - 00:34:30 MET