Go Back   PCMech Forums > Help & Discussion > Web Design / Development

Need Some Help? Type Your Keywords Here:

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Old 12-13-2000, 12:28 AM   #1
Member (7 bit)
 
Join Date: Jul 2000
Location: Australia
Posts: 97
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.
earlboy is offline   Reply With Quote
Old 12-15-2000, 03:07 PM   #2
Member (8 bit)
 
TechTeach's Avatar
 
Join Date: Jul 1999
Posts: 141
smokin

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]
TechTeach is offline   Reply With Quote
Old 12-20-2000, 01:57 AM   #3
Member (7 bit)
 
Join Date: Jul 2000
Location: Australia
Posts: 97
Post

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.
earlboy is offline   Reply With Quote
Old 12-30-2000, 04:37 AM   #4
Member (12 bit)
 
Paul Victorey's Avatar
 
Join Date: Mar 1999
Location: MN or WI
Posts: 3,017
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 static 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:


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.


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 static 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]
__________________
Paul M. Victorey
------------------
I am not responsible for any problems that may arise as a result of following my advice. This includes, but is not limited to, computer failure, loss of data, nuclear war, famine, boils, no clean laundry, your daughter running off with a biker gang, or armageddon. Take my advice at your own risk.
Paul Victorey is offline   Reply With Quote
Old 01-12-2001, 09:33 PM   #5
Member (7 bit)
 
Join Date: Jul 2000
Location: Australia
Posts: 97
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!
earlboy is offline   Reply With Quote
Reply

Bookmarks

Still Need Help? Type Your Keywords Here:


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 08:24 AM.
Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.6.0