This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: symbols not resolved... symbols not generated?
- From: Ian Lance Taylor <iant at google dot com>
- To: Brian Budge <brian dot budge at gmail dot com>
- Cc: gcc-help <gcc-help at gcc dot gnu dot org>
- Date: Fri, 17 Apr 2009 17:44:17 -0700
- Subject: Re: symbols not resolved... symbols not generated?
- References: <5b7094580904171624o2283e7a9k71bccbec358c44b3@mail.gmail.com>
Brian Budge <brian.budge@gmail.com> 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.
Ian