0

I recently ran into the problem described in Weird undefined symbols of static constants inside a struct/class and I am trying to bring my code into compliance by adding definitions to the corresponding .cpp files for all of my static const class members, not just the ones that were causing linker errors.

In the cases where the constant is used in multiple compilation units, I am getting multiple definition errors, even though the definition is only in one of the compliation units.

Moving the initializers to the definitions prevent the errors, but I would rather not do that.

For what it's worth, while I am currently working in Visual Studio, this code needs to build on several platforms.

Community
  • 1
  • 1
IronMensan
  • 6,761
  • 1
  • 26
  • 35
  • 6
    Can you show us your code? – wkl May 05 '11 at 14:59
  • 1
    To add to what @birryree said - please reduce your code to the smallest complete example that still demonstrates your problem. In reducing your code, either you'll spot the problem (and you won't need us), or you'll make it easy for us to spot the problem (and you'll get the answer sooner.) For more information about short, self-contained complete example programs, consult http://sscce.org. – Robᵩ May 05 '11 at 15:11
  • @birryree and @Rob - The code does not differ significantly from what is in the linked question and answers. – IronMensan May 05 '11 at 15:30
  • if it does not differ significantly from the linked question, then you wouldn't be having this problem from the advice in there. Since you're asking, you should probably show us your code so we can point out what's wrong. – wkl May 05 '11 at 15:49
  • @birryree The problem had to do with Visual Studio language extensions (see Lindydancer's answser) which is why it was not a problem in the linked question. – IronMensan May 05 '11 at 16:08

4 Answers4

3

Static member variables are declared in the class body and defined once outside the class body. The general way of doing this is:

class MyClass

{
    static int i;

};

int MyClass::i = 0;

The definition is done in the C++ source files and not in header(.h). If it is done so, the variable will defined everywhere the header file being included. It seems you are facing this very same problem.

Robᵩ
  • 163,533
  • 20
  • 239
  • 308
Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • I tried to make it clear in the question that this is not the problem by including "the corresponding .cpp" and "definition is only in one of the compliation units" – IronMensan May 05 '11 at 15:29
1

According to one of the posts on http://bytes.com/topic/c/answers/710704-const-static-initialization-visual-studio this may actually be a visual studio bug, preventing you from using that form of initialization.

Unfortunately I think you may be stuck doing the initialization in the source file to maintain portability.

I created a simple example that compiled and linked fine in g++ 4.2.

Mark B
  • 95,107
  • 10
  • 109
  • 188
1

If you have language extensions enabled, Visual Studio will allow you to use static const objects without defining the in an implementation file. Unfortunately, it will issue an error (if I remember correctly) for correct C++ programs, when there is an explicit definition.

Try to disable language extensions.

Lindydancer
  • 25,428
  • 4
  • 49
  • 68
  • Disabling language extensions is not an option since the Windows7 SDK does not compile without them. However the selectany option described in http://msdn.microsoft.com/en-us/library/34h23df8(v=VS.80).aspx seems to do the trick. – IronMensan May 05 '11 at 16:06
0

I think if you want your code to work on multiple platforms, you should move the initialisation to the definition (in the .cpp file). While it might work otherwise on one or more compilers, don't rely on it to be portable.

Axel
  • 13,939
  • 5
  • 50
  • 79