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
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.
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
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:
Code:
bool myGlobVar;
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).
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
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.
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
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.
Bookmarks