Re: [ROOT] Error: multiple definitions

From: Christian Holm Christensen (cholm@nbi.dk)
Date: Mon Dec 15 2003 - 18:38:45 MET


On Mon, 2003-12-15 at 17:19, cstrato wrote:
> Dear Justin
> 
> Thank you, but first I tested Eddie Offermann´s suggestion to use:
>     extern const char *kXPSName;
> in my header file, and it solved my problem.
> 
> Nevertheless I decided to wrap the #include statements in the #ifndef
> statements. Long time ago I did this all the time, but with root I
> have abandonded it because everythink seems to work fine w/o it.
You may save some compile time by agressively putting inclusion guards
around your `#include' directives, and putting headers guards in all
headers, like so:

  // -*- mode: c++ -*- Hi Emacs this is a C++ header file called foo.h
  #ifndef FOO_H
  #define FOO_H

  ....

  #endif
  //
  // EOF
  //


  // -*- mode: c++ -*- Hi Emacs this is a C++ source file called foo.cxx
  #ifndef FOO_H
  # include "foo.h"
  #endif

   ....

  // EOF

The reason is, if you only include the guards in the header file, the
file _will_ in fact be sought for _and_ opened by the preprocessor. 
However, if you include it with inclusion guards, the preprocessor won't
even bother go look for the file as the guard (preprocessor-) symbol is
already defined. 

BTW. Never _ever_ _define_ things in a header, unless it's a template
header: 

  template <typename T>
  class foo 
  {
    static foo* _instance;
    static T    _lock;
    foo() {}
    ~foo() {{
    foo(const foo& f) {}
  public:
    static& instance()
    {
       if (!_instance) {
          _lock.lock(); 
          if (!_instance) _instance = new foo<T>;
          _lock.unlock();
       }
       return *_instance;
    }
  };
  template <typename T>
  foo* foo<T>::_instance  = 0;
  tempalte <typename T>
  T     foo<T>::_lock = T();

or an inline (member) function:

  inline 
  void 
  function() {
    std::cout << "Hello World" << std::endl;
  }

  struct foo 
  {
    void function();
  };

  inline 
  void
  foo::function()
  { 
    std::cout << "Hello World" << std::endl;
  }
    
Everything else is defined in compiled code, and if needed declared as
`extern' in some header file.  `Hidden' global variables should be
static to some file with appropriate get/set functions - but why not use
a singleton instead?


 

Yours,

-- 
 ___  |  Christian Holm Christensen 
  |_| |	 -------------------------------------------------------------
    | |	 Address: Sankt Hansgade 23, 1. th.  Phone:  (+45) 35 35 96 91
     _|	          DK-2200 Copenhagen N       Cell:   (+45) 24 61 85 91
    _|	          Denmark                    Office: (+45) 353  25 404
 ____|	 Email:   cholm@nbi.dk               Web:    www.nbi.dk/~cholm
 | |



This archive was generated by hypermail 2b29 : Thu Jan 01 2004 - 17:50:17 MET