initialize the global variables in .so files

V

Vinu

Hi,
I am facing one problem related to global variables in .so file. When
ever I access the variable the application is crashing

I have a class called services.
class services
{
services(){ }
void Add() { }
void Delete(){ }
};

In another file I declared my global variable like this
services gserviceObj;

Now I used this global variable 'gserviceObj' to access the member
function 'Add()',

gserviceObj.Add(); I am calling this Add() method of gserviceObj in
another file where the gserviceObj is declared as extern.

The application is crashing at gserviceObj.Add();. The constructor of
services is also not called till that point.

These all the files are in one .so. If I move the 'services
gserviceObj;' declaration to main and change the all the other
declarations in library(.so) to extern my application is working fine.

Is there is any way to initialize the global variables in .so files
other than declaring it in main.
Can anybody help me in this?

Thanks,
Vinu
 
J

Jack Klein

Hi,
I am facing one problem related to global variables in .so file. When
ever I access the variable the application is crashing

The C++ language does not know anything at all about .so files. This
is an implementation detail of your compiler and operating system
combination.

You need to ask in a group that specifically supports your platform
and compiler. Perhaps or
 
A

Axter

Vinu said:
Hi,
I am facing one problem related to global variables in .so file. When
ever I access the variable the application is crashing

I have a class called services.
class services
{
services(){ }
void Add() { }
void Delete(){ }
};

In another file I declared my global variable like this
services gserviceObj;

Now I used this global variable 'gserviceObj' to access the member
function 'Add()',

gserviceObj.Add(); I am calling this Add() method of gserviceObj in
another file where the gserviceObj is declared as extern.

The application is crashing at gserviceObj.Add();. The constructor of
services is also not called till that point.

These all the files are in one .so. If I move the 'services
gserviceObj;' declaration to main and change the all the other
declarations in library(.so) to extern my application is working fine.

Is there is any way to initialize the global variables in .so files
other than declaring it in main.
Can anybody help me in this?

Your problem is probably related to the order of global variable
initialization.

You can fix this by creating your object within a static function as a
static local variable.
Example:

gserviceObj
class services
{
services(){ }
void Add() { }
void Delete(){ }
static services& Get_gserviceObj();
};

//Cpp file
services& services::Get_gserviceObj()
{
static services * gserviceObj = new services;
return *gserviceOb;
}

You call the above via the following:
services::Get_gserviceObj().Add();

This will garantee that when you try to call this object, that it will
be initialized. This is garanteed to work IAW C++ standards.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top