Boost logo

Boost Users :

Subject: Re: [Boost-users] [CONFIG] static const definitions
From: Brian Budge (brian.budge_at_[hidden])
Date: 2013-02-28 11:39:31


On Thu, Feb 28, 2013 at 1:34 AM, John Maddock <boost.regex_at_[hidden]> wrote:
>> On 2/27/2013 5:09 AM, boost-users-request-at-lists.boost.org |Boost/Allow
>> to home| wrote:
>>>
>>> In fact even when initialized inline, an out of line definition should
>>> always be provided as different compilers have different ideas about what
>>> constitutes "use" of the variable (rather than just the value). MSVC
>>> pretty
>>> much never requires an out-of-line definition for example, where as GCC
>>> will
>>> often generate linker errors if you don't provide it.
>>
>> The problem is that MSVC is _prohibiting_ an out-of-line definition!
>> (Visual Studio 2010)
>
>
> I've not come across that one before - do you have an example?
>
> John.
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users

Simple example, does not link in VS 2012:

//example.h
#ifndef example_H_
#define example_H_

struct example {
        static const int foo0 = 0;
        static const int foo1 = 1;
};

#endif//example_H_

//example.cpp
#include "example.h"

const int example::foo0;
const int example::foo1;

//main.cpp
#include "example.h"

int main(int argc, char **args) {
        example e;

        return e.foo0;
}

//errors:
1> Compiling...
1> example.cpp
1> Generating Code...
1>example.obj : error LNK2005: "public: static int const
example::foo0" (?foo0_at_example@@2HB) already defined in main.obj
1>example.obj : error LNK2005: "public: static int const
example::foo1" (?foo1_at_example@@2HB) already defined in main.obj
1>c:\users\budgeb\documents\visual studio
2012\Projects\Project1\Debug\Project1.exe : fatal error LNK1169: one
or more multiply defined symbols found
1>


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net