global const variable not getting assigned properly

John Love-Jensen eljay@adobe.com
Mon Dec 11 12:43:00 GMT 2006


Hi Chad,

> Do you know why they are not getting initialized?

I presume because the .SO's global initializer is not being called.

> If I call dlopen instead will that help solve this problem?

Maybe.

> Is there something I can set to get those initialized before I use them?

Yes.

Instead of writing:
const int uno = 1;
const int dos = 2;
const int tres = 3;

Write this:

int GetUno()
{
  static int uno = 1;
  return uno;
}
int GetDos()
{
  static int dos = 1;
  return dos;
}
int GetTres()
{
  static int tres = 3;
  return tres;
}

Then use the accessor functions instead of globals.

In C/C++, there is no guarantee as to the order of construction betwixt
translation units.  This technique will guarantee order of construction
(provided you do not have a circular dependency).

HTH,
--Eljay



More information about the Gcc-help mailing list