Mortgages | Mortgages | Mortgage Calculator | Reptile Store | Modded Xbox
statics on Solaris C++ Compiler ver. 5 [Archive] - PCMech Forums

PDA

View Full Version : statics on Solaris C++ Compiler ver. 5


earlboy
12-12-2000, 11:28 PM
I've got a static variable declared global to a file . Now whenever I link, the linker complains that it cannot resolve the static variable. If I remove the static qualifier everything links properly.

Any ideas why this is happening?

All help appreciated.

TechTeach
12-15-2000, 02:07 PM
Perhaps if you print some of the code, how it looks with the static qualifier and how it looks without it, it may be easier to help.

I think I have an idea of what you are talking about but I'm not sure.




[Edited by TechTeach on 12-15-2000 at 03:13 PM]

earlboy
12-20-2000, 12:57 AM
Hi Teach, this is not the exact code ( the company I'm working for is very strict about releasing code and it a small part of a very large code ). By the way I only get link errors on release mode where the code is optimized.
If I include debug information everything is fine. When building the release version ( no debug info ) I've tried using the option -O1 lowest optimisation, plus added volatile qualifier on the variables mentioned below but I still have link errors.


Okay here goes, I've got this variables declared in a CPP file.


typedef struct { int32 id; const char *errStr; } EventStruct;

static EventStruct *gEventTable = NULL;
static int gSize = 0;
static bool gEchoOn = false;


The file also contains an inline class which access the variables mentioned above which inherits from an abstract class which is on another separate file.

class Abstract : public Concrete
{
public :
.
.
void Access_g_EventTable()
{
if ( gEventTable )
{
( do what ever here also manipulate or
access gSize and gEcho here )
}
}
}

Now there is also a stand alone function which initialises the mentioned variable. This is declared as extern in another file where EventStruct is created. After EventStruct is created this function is called passing in the newly created EventStruct.

void setVarialbes( EventStruct* eventTable, int size )
{
gEventTable = eventTable;
gSize = size;
}


In NT ( This code is designed to run on multiple OS ) everything is OK (Debug and Release version ), but in Solaris I get a link error ( only when debugging is off ) saying that the variable gEventTable, gSize and gEchoOn is unreferenced in the file where they were declared.

Sorry if this is a bit long and I could not give all of the code.

Paul Victorey
12-30-2000, 03:37 AM
I don't see why you need to use the static keyword at all... Maybe I'm not thinking right as it's 3:30 AM here, but I don't think I've ever seen <b>static</b> variables used outside of a class/struct.

The only reason I know of to use static variables is, when used before a data member in a class, it forces the value of that data member to be the same amongst all instances of the class. I.e., if you use this code:

<pre>
class MyClass{
public static int data;
};

MyClass myObject1;
MyClass myObject2;

//At this point, myObject1.data and myObject2.data must
//always be the same, as they are 2 ways to refer to the
//same variable.
</pre>

By definition any global variable will always act this way -- as there can be only one instance of the global variable -- so what, then, is the <b>static</b> supposed to do, anyhow? Maybe if I knew why you needed it, I could offer more advice.

(Sorry, BTW, if this makes no sense, or is really dumb. It's 3:30 AM, that's my excuse and I'm sticking to it)

[Edited by Paul Victorey on 12-30-2000 at 04:40 AM]

earlboy
01-12-2001, 08:33 PM
Hi Paul,

Sorry for the late reply, I been out of the net for a while.

Youre right, I've already solved this problem by removing the static qualifier and everything runs OK.

My excuse is, the code was not mine and I was just porting it from NT to Solaris :).

Thanks for all the responses
and cheers!