symbols not resolved... symbols not generated?

Ian Lance Taylor
Sat Apr 18 00:45:00 GMT 2009

Brian Budge <> writes:

> I'm running into an issue using static const variables in my template
> class, but only with -O0.  I have something like this:
> Bar.h
> template <typename Foo>
> class Bar {
> ...
> public:
>   typedef uint32_t SomeType;
>   static const SomeType A = 0;
>   static const SomeType B = 1;
>   static const SomeType C = 2;
> ...
> };
> These types are used in Bar member functions, as well as by Foo (who
> knows who he is an argument for).
> In -O1 and above, it looks like the symbols are never generated, and
> this is fine since it's probably folded into the code.  In O0, which
> I'm using for debugging, the symbols appear in my object file as 'U'
> (unresolved).

The declaration of a const in a class is only a declaration.  You also
need a definition.  The current C++ standard says that the definition is
always required, but that in some cases an implementation need not
diagnose it.  The new C++ standard will relax this in some cases, but
gcc does not yet implement that.

That is, in some .cc file, you need to do

const SomeType Bar::A;
const SomeType Bar::B;

Note that since the const was initialized in the declaration, it should
not be initialized in the definition.


More information about the Gcc-help mailing list