rajaram
asked on
static variables
This question might have to do with some knowledge of AIX4.2.x based C++ compiler(xlC_r). I am new to this environment. here it goes:
i've a C++ class file with a certain static variable(say "astat". this goes into a shared library and this library is linked into an executable.
now when I run this executable(the file that intializes/defines this static var is part of only the shared library), i find 2 addresses for the static varible "astat" when accessed from 2 different functions. why would this happen ??
if you are familiar with AIX's makeC++SharedLib_r/xlC_r to create shared libs and executables respectively, is there a link line option to supress these multiple copies ?
i've a C++ class file with a certain static variable(say "astat". this goes into a shared library and this library is linked into an executable.
now when I run this executable(the file that intializes/defines this static var is part of only the shared library), i find 2 addresses for the static varible "astat" when accessed from 2 different functions. why would this happen ??
if you are familiar with AIX's makeC++SharedLib_r/xlC_r to create shared libs and executables respectively, is there a link line option to supress these multiple copies ?
The definition/initialization isn't in a header file, right?
ASKER
here it goes:
file: hdr.h
-----
class A {
private:
static sp var1; // sp - a struct definition.
public:
..
..
stativ void initVars();
static void someOperation();
..
..
};
__________________________ __________ __________ __________ ________
file impl.cpp
--------
sp A::var1;
void A::initVars() {
// initialize the static varible
};
void A::someOperation() {
// this function is accessed by various routines to
// use/manipulate the stativ var
};
.
.
__________________________ __________ __________ __________ ____
file: hdr.h
-----
class A {
private:
static sp var1; // sp - a struct definition.
public:
..
..
stativ void initVars();
static void someOperation();
..
..
};
__________________________
file impl.cpp
--------
sp A::var1;
void A::initVars() {
// initialize the static varible
};
void A::someOperation() {
// this function is accessed by various routines to
// use/manipulate the stativ var
};
.
.
__________________________
Did You tell to the compiler & linker that class A is expoted/imported any way ???
I see additional problem in Your code ... You have to specify initializer to You static variable... nobody garanted order of initialization of statics
ASKER
1st comment :
i have not used any options which will tell the compiler/linker to export/import these files. frankly i dont know if I have to
do it or how to export/import files ?
2nd comment:
please see the .cpp file. the first line (sp A::var1;) should
do. atleast this works on SOLARIS/HPUX/WIN-NT.
do i've to equate it to NULL or something ??
i have not used any options which will tell the compiler/linker to export/import these files. frankly i dont know if I have to
do it or how to export/import files ?
2nd comment:
please see the .cpp file. the first line (sp A::var1;) should
do. atleast this works on SOLARIS/HPUX/WIN-NT.
do i've to equate it to NULL or something ??
2 nd comment... its good practice to specify initializer (constructor) for static variable (by stabdard it have to be zerroed, but i'v seen alredy 2 cases where it wasn't 0) :)
look at documentation on Your compiler for keywords export/import, _export/_import ... it may help You... I don't have compiler here so i can't check it ... but export/import is standard keyword... so check it.. and shoe me the results if any... i'll help You then
look at documentation on Your compiler for keywords export/import, _export/_import ... it may help You... I don't have compiler here so i can't check it ... but export/import is standard keyword... so check it.. and shoe me the results if any... i'll help You then
ASKER
i'll hunting for the compiler/linker documentation. once i get
them i'll try and let you know.
thanks.
them i'll try and let you know.
thanks.
the suggestion is the next try to do like that
in hdr.h
#ifndef HDR_DECLSPEC
#define HDR_DECLSPEC _export
#endif
class HDR_DECLSPEC A {
private:
static sp var1; // sp - a struct definition.
public:
..
..
stativ void initVars();
static void someOperation();
..
..
};
and before using in in the main (non-shared part of the program)
do
#define HDR_DECLSPEC _import
it may help
in hdr.h
#ifndef HDR_DECLSPEC
#define HDR_DECLSPEC _export
#endif
class HDR_DECLSPEC A {
private:
static sp var1; // sp - a struct definition.
public:
..
..
stativ void initVars();
static void someOperation();
..
..
};
and before using in in the main (non-shared part of the program)
do
#define HDR_DECLSPEC _import
it may help
ASKER
WHEN does one require to export/import files ?
btw, this is an encouraging suggestion. thanks!
Coming from a SOLARIS background, i've never understood the
logic/reason behind export/import of files(if possible please
answer the "WHEN" part).
great suggestion!!
btw, this is an encouraging suggestion. thanks!
Coming from a SOLARIS background, i've never understood the
logic/reason behind export/import of files(if possible please
answer the "WHEN" part).
great suggestion!!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.