This is the mail archive of the gcc-help@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]

Re: Method to specify initialization order across translation units?


On 12 August 2015 at 22:48, Jeffrey Walton wrote:
>
> Forgive my ignorance... what init_priority should I use?

It doesn't matter, only the relative values matter.

> I selected a
> base priority of 250, and then specified the order of 5 file scope
> objects. The objects were 3 std::strings and 2 C++ class objects.
> That's the extent of file scope C++ objects in this library.
>
> My thinking was 250 will give the standard runtime and other required
> libraries to initialize first. Then, initialize the five objects of
> interest. These are objects which have implicit dependencies that's we
> can't seem to express in C++ Then initialize "don't care" values,
> which don't appear to have a dependency with special needs.
>
> After the initialization of the 5 C++ objects, I specified 1
> [non-class] function as a constructor with a priority of 300.
>
> Combined, it resulted in the issue with the POD type initialization
> (like a bool is no longer initialized when used), and issues with
> libstdc++. So 250/300 is not the magic number...

Of course not, if there was a magic number then everything would be
initialized with that priority by default.

Did you give the bool an init_priority?

You need to work out which globals depend on each other and ensure
that each one is initialized before anything that depends on it. If
some of them depend on the bool then you need to ensure the bool is
initialized before the objects that depend on it.

You could solve the problem portably by just putting all your globals
in one translation unit and ordering their declarations, so you don't
need to use init_priority at all.

There's an open bug report about iostreams not being usable from
constructors of objects with init_priority, because using
init_priority can make your object get initialized before the
iostreams have been set up.


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