This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c++/83271] const variable previously declared "extern" results in "weak declaration must be public" error


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83271

--- Comment #2 from Alexey Salmin <alexey.salmin at gmail dot com> ---
(In reply to Jakub Jelinek from comment #1)
> I must say I fail to see usefulness of adding the attribute to the
> definition rather than declaration though.

Here's my case. There's a const bool flag in a static library that should be
reliably available before the main() entry point, therefore the static
initialization is used (see C++11 3.6.2.2). By default the flag is set to true
but it's overridden to false in a custom build.

flag.h:
extern const bool flag;

enabled.cpp:
#include "flag.h"
const bool flag __attribute__((weak)) = true;

disabled.cpp:
#include "flag.h"
const bool flag = false; // strong override

Library behaves differently depending on whether the "disabled.o" was provided
to the linker or not, while the "enabled.o" is always ar-ed into the static
library. I include the same header file in both cpp files to make sure both
definitions correspond to the same declaration.

Let me know if there's a better way to achieve this behavior. The one I can
think of involves a macro and two different builds of the library but it's not
welcome in our build system. Another option would be to keep both "enabled.o"
and "disabled.o" out of the static library and don't use weak symbols at all,
but that breaks the compatibility.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]