This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: ordering of constructors
Neal Becker <ndbecker2@verizon.net> writes:
| Gabriel Dos Reis wrote:
|
| > Neal Becker <ndbecker2@verizon.net> writes:
| >
| > | Hi. I'm trying to port CLN-1.1.6 to x86_64. Currently it segfaults on
| > | all
| > | tests. The problem appears to be some asm code whose purpose is to make
| > | sure global constructors are ordered.
| > |
| > | I'm not that familiar yet with this code, but I think the way it is used
| > | is that if a file F.cc needs a global object A, and A requires B, then a
| > | macro is used to ensure the constructor for B was called before A.
| >
| > Why not put a function call in the constructor for A that make sure B
| > initialized?
| >
|
| I'm not sure what you mean. Consider this example:
|
| A.H:
| struct A{ some constructor ...};
| extern A a;
|
| A.cc:
| A a;
|
| B.H:
| #include "A.H"
| struct B{...}
|
| B.cc:
| B b;
|
| Let's assume that B's constructor uses the global object 'a', so that the
| global cons for A must run before that for B.
[ In the scenario you just examplified, it is B who requires A,
contrary to previous statement of the problem. But, let's say that
is not an issue. ]
| What do you suggest?
Insert a function call in B constructor that makes sure that a is
constructed.
For example, it can be something like
A.H:
struct A { some constructor ... };
extern A a;
void make_sure_a_is_constructed();
A.C
A a;
void make_sure_a_is_constructed() { /* empty */ }
B.H:
#include "A.H"
struct B { ... }
B::B()
{
make_sure_a_is_constructed();
// use a...
}
B.cc;
B b;
The call to make_sure_a_is_constructed(), will provoke the
initialization of a, before that function is executed.
-- Gaby