Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Static const variables cause linker problems under Qt

3 views
Skip to first unread message

Rune Allnor

unread,
Feb 5, 2010, 10:24:47 AM2/5/10
to
Hi all.

I have a template class where I have defined some
constants in a header file, along the lines of

////////////////////////////////////////////
class flags{
static const size_t flag;
};

const size_t flags::flag = 1;
////////////////////////////////////////////

This compiles and runs nicely in the Boost.Test program,
but when I try to use the template class in a Qt GUI program,
the linker starts complaining about the constants as being
multiple defined objects:

- in main.obj
- in moc_myfile.obj
- in myfile.obj

I have included the standard guard about multiple inclucions
of the header files, and I can see why these constants would
appear in myfile.obj, as I include the file where they are declared
from myfile.h. However, I have no idea why the constants should
be available in main.obj or moc-myfile.obj - that's where Qt come
in to do its thing.

There isn't much I can do about the ways Qt twists and turns
the code into an executable, so I suppose my only hope
is to be able to resolve the 'multiple definition' problem.

Any hints on how to do this?

The flags class will exist in millions of instances, so I can not
afford to include the flag definintions as const variables in the
class.

Rune

Victor Bazarov

unread,
Feb 5, 2010, 10:51:33 AM2/5/10
to
Rune Allnor wrote:
> I have a template class where I have defined some
> constants in a header file,

*Never* define your static data that you might want to link to, in a
header. *Always* define them in *one of the source files*. As soon as
you do that, your linker problems will disappear.

Const or no const plays no role here.

> along the lines of
> [..]

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

Yu Han

unread,
Feb 6, 2010, 8:13:08 PM2/6/10
to
On 02/05/2010 11:24 PM, Rune Allnor wrote:
> Hi all.
>
> I have a template class where I have defined some
> constants in a header file, along the lines of
>
> ////////////////////////////////////////////
> class flags{
> static const size_t flag;
> };
>
> const size_t flags::flag = 1;
> ////////////////////////////////////////////
try this:
class flags {
static const size_t flag = 1;
};

>
> This compiles and runs nicely in the Boost.Test program,
> but when I try to use the template class in a Qt GUI program,
> the linker starts complaining about the constants as being
> multiple defined objects:
>
> - in main.obj
> - in moc_myfile.obj
> - in myfile.obj
>
> I have included the standard guard about multiple inclucions
> of the header files, and I can see why these constants would
> appear in myfile.obj, as I include the file where they are declared
> from myfile.h. However, I have no idea why the constants should
> be available in main.obj or moc-myfile.obj - that's where Qt come
> in to do its thing.
>
> There isn't much I can do about the ways Qt twists and turns
> the code into an executable, so I suppose my only hope
> is to be able to resolve the 'multiple definition' problem.
>
> Any hints on how to do this?
>
> The flags class will exist in millions of instances, so I can not
> afford to include the flag definintions as const variables in the
> class.
>
> Rune


--
Yu Han

--- news://freenews.netfront.net/ - complaints: ne...@netfront.net ---

0 new messages