1

I am including a .h file with a global boost signal in a the header file of a class which is, in turn included in the main function's file. The linker says the signal is declare multiple times. The signal declaration is wrapped in the #ifndef, #define and #endif block typical of C/C++ header files (used to avoid multiple declaration). I am using Eclipse with gcc.

#ifndef SIG_HEADER
#define SIG_HEADER
#include <boost/signal.hpp>

boost::signal0 <void> signal1;

#endif

what am I doing wrong?

Mat
  • 202,337
  • 40
  • 393
  • 406
Andrea Sindico
  • 7,358
  • 6
  • 47
  • 84

2 Answers2

5

You're getting a linker error, not a compiler error. So, preprocessor directives won't help you here.

What you need to do is define the variable inside the source file (as opposed to the header file), and use an extern declaration in the header file.

cha0site
  • 10,517
  • 3
  • 33
  • 51
3

Your linker is correct. Each time you include this header the symbol signal1 gets defined, resulting in a multiple definition error.

To your rescue comes the extern keyword, which will tell the compiler that this is an object that will be accessed by the entire program and requires external linkage. You will then have to give the compiler a definition of the variable somewhere else, like in the cpp file for this header.

This question offers some more information about external linkage.

Community
  • 1
  • 1
daramarak
  • 6,115
  • 1
  • 31
  • 50
  • thank you I am so not used to using global variables that I couldn't get the problem. However though I am going to define a Singleton containing the existing boost signals I was curious to understand What I was missing. – Andrea Sindico Jul 08 '12 at 21:31
  • Good that you are not used to global variables. – daramarak Jul 08 '12 at 22:19