-
November 23rd, 2009, 11:09 AM
#1
[RESOLVED] Global variable in a static lib
Suppose I have a global variable declared in a header file, something like this:-
Code:
extern bool myGlobalVar;
I can define and initialize the variable like this:-
Code:
bool myGlobalVar = false;
If I put that initialization into the global space for my executable, everything works fine - but if I put the initialization into the global space for a static library and then I link my executable (statically) to the lib, the linker complains about myGlobalVar being an unresolved external. Is there a way around this problem (i.e. a way of keeping the variable global - but also 'housing it' in the lib to which it belongs)
"A problem well stated is a problem half solved.” - Charles F. Kettering
-
November 23rd, 2009, 11:40 AM
#2
Re: Global variable in a static lib
Where is the actual declaration of the global variable located (the declaration without "extern")? That is what the linker can't find -- it has nothing to do with the initialization.
Regards,
Paul McKenzie
-
November 23rd, 2009, 11:48 AM
#3
Re: Global variable in a static lib
I'm declaring (and initializing) the variable in global space - but in a static lib, rather than in the executable's global space. The executable doesn't use the variable at all (except by virtue of the fact that it's linked to the static lib). In fact, nothing outside of the static lib even cares about the variable. That's why I thought I'd be able to locate it in the lib's global space.
"A problem well stated is a problem half solved.” - Charles F. Kettering
-
November 23rd, 2009, 12:54 PM
#4
Re: Global variable in a static lib
Hmmm... declaring it static instead of extern seems to do the trick.
"A problem well stated is a problem half solved.” - Charles F. Kettering
-
November 23rd, 2009, 01:11 PM
#5
Re: Global variable in a static lib
Originally Posted by John E
Hmmm... declaring it static instead of extern seems to do the trick.
There is a huge difference between static and extern.
A variable declared on static scope is not global. A static variable is only known within that particular module. If you have module A that has "static int x", and a module B that has "static int x", both x's are different variables, they are not the same.
Secondly, you still didn't really answer my original question -- where is the declaration of your global variable? You mention where you have it "externed", but where is the true declaration (the declaration without extern)? For the linker to not complain, it must see this:
in one and only one module. No "extern" in front of it.
All a static library has is a bunch of object code packed into a single file. It is no different than a bunch of .obj files that the compiler generates when compiling your code. There is no issue with global variables and static libraries. Therefore you didn't really solve the issue -- you just replaced your global with something that isn't a global (static).
Regards,
Paul McKenzie
-
November 23rd, 2009, 02:13 PM
#6
Re: Global variable in a static lib
I have about a dozen modules that build into a static lib. In one of the source modules (near to the top, before any functions are defined) I've put bool myGlobalVar = false;
But when I try to link my executable (even though I'm linking it to the lib) the linker says that myGlobalVar is an unresolved external.
If I put bool myGlobalVar = false; in one of the executable's modules (commenting out the other one) then the program links successfully. Are you saying it should have linked successfully the other way too?
"A problem well stated is a problem half solved.” - Charles F. Kettering
-
November 23rd, 2009, 02:28 PM
#7
Re: Global variable in a static lib
Originally Posted by John E
Are you saying it should have linked successfully the other way too?
Yes.
A static library is basically a directory of some object code -- nothing more than that. Maybe you are building the library with different options than the application.
Also, please post the exact linker message. Maybe you're adding decoration to your names in the static library, and the names generated when you compile your executable use a different decoration.
Regards,
Paul McKenzie
-
November 23rd, 2009, 02:52 PM
#8
Re: Global variable in a static lib
Sorry Paul, it looks like I've misled you here. This seems to have been an issue with incremental linking. When I did a full build, the problem went away. Apologies.
"A problem well stated is a problem half solved.” - Charles F. Kettering
-
November 23rd, 2009, 03:22 PM
#9
Re: Global variable in a static lib
Originally Posted by John E
Sorry Paul, it looks like I've misled you here. This seems to have been an issue with incremental linking. When I did a full build, the problem went away. Apologies.
No problem.
Regards,
Paul McKenzie
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|