lundi 4 janvier 2016

GCC warn about declared but undefined variable

I know from these questions that if I declare a static const variable in a header file:

Foo
{
public:
    static const int BAR = 1234;
    ...
};

I must also define it in a source file:

const int Foo::BAR;

However, it gcc will happily ignore this requirement if the variable isn't used in the program. Furthermore, gcc seems to happily gloss over some simple uses (e.g. passing to std::min by const reference) if optimization is enabled.

Is there a way to force gcc to warn if I've forgotten to provide a definition even if the variable isn't used in my source code? If Foo above is part of a shared library and no definition for Foo::BAR is provided, users wouldn't be able to pass Foo:BAR by reference or get a pointer to it, but I as the library writer am given no indication that this is a problem.

More generally, is there a way to enforce that all declarations in a shared library have a corresponding definition? Or would this be sacrificing too much in the way of optimization opportunities to be worthwhile?

Aucun commentaire:

Enregistrer un commentaire