CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    [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

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    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

  3. #3
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    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

  4. #4
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    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

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Global variable in a static lib

    Quote Originally Posted by John E View Post
    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).

    Regards,

    Paul McKenzie

  6. #6
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    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

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Global variable in a static lib

    Quote Originally Posted by John E View Post
    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

  8. #8
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    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

  9. #9
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Global variable in a static lib

    Quote Originally Posted by John E View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured