Maintaining Global Vars Between Libraries

This is a discussion on Maintaining Global Vars Between Libraries within the C Programming forums, part of the General Programming Boards category; Hi, I've got a static library, a dynamic library, and a main program. In the main program, I define an ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    52

    Maintaining Global Vars Between Libraries

    Hi,

    I've got a static library, a dynamic library, and a main program. In the main program, I define an int variable called gotoOption. In the main() function I initialize the value of gotoOption. In my dynamic library, I declare 'extern int gotoOption;" at the top but when I refer to it later on in a function it has a value of zero even though I initialized it to another value.

    Anyone know what I'm doing wrong?

    Thanks,

    Canadian0469

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Can you provide a small code example?

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    52
    **Main Program**
    Code:
    ...
    #include <dirent.h>
    
    void executeExternally(char* []);
    void changeDir(char const * const);
    
    void show_file(char const * const);
    
    #define MAXSIZE 	 256
    #define BUFSIZE      4096
    #define MAXLINESIZE  256
    
    int nread;
    
    int lnOption;
    int gotoOption;
    char lwString[MAXLINESIZE] = "";
    **Dynamic Library**
    Code:
    ...
    #define MAXLINESIZE  256
    
    int linesInScreen;
    int readbkSet;
    
    char screen[SCREENSIZE];
    
    extern int lnOption;
    extern int gotoOption;
    ...
    if(nread){          // something in buffer
    	bufferNum_ecp = 1;
    	offset_ecp = 0;
        }
    	    int rv;
                char msg[256];
    	    rv=sprintf(msg,"%d\n",gotoOption);
    	    write(1,msg, rv);
        if(gotoOption)
        linesShown = gotoOption;
        else
        linesShown = 20;
    ...

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Why not have a function "setGotoOption(int x)" that does the thing - then there is no complicated global variables to deal with, and the implementation inside the DLL is also more "hidden", so that you can change it [e.g. if you wish to use the same DLL with multiple instances of an executable, you can implement per-process/thread instances].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,671
    How about passing said variable as a parameter, and not with a messy global variable.

    What error messages did you get when you tried to link the program?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    52
    Thanks for the advice everyone.

    For reference, I found out that the code I posted actually works I just made a silly mistake. I had a function that was resetting the value every time I executed the desired function.

    Canadian0469

  7. #7
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,676
    >> I've got a static library, a dynamic library
    What exactly are you calling a "dynamic library"? Because a reall DLL will not link with just an "extern int" declaration.

    >> the code I posted actually works I just made a silly mistake
    That tells me that you're creating a static LIB, not a DLL.

    gg

  8. #8
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    That tells me that you're creating a static LIB
    And still just asking for troubles when the main module changes the address of the global var for some reason and the library is not recompiled...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  9. #9
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,249
    Quote Originally Posted by Codeplug View Post
    >> I've got a static library, a dynamic library
    What exactly are you calling a "dynamic library"? Because a reall DLL will not link with just an "extern int" declaration.
    Maybe not on Windows, but it works fine with ELF (e.g. on Linux). You don't need anything special to allow a shared object to refer to other global symbols.

  10. #10
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,676
    >> Maybe not on Windows...
    Ah yes - I'm brainwashed

    >> ...and the library is not recompiled
    Shouldn't the dynamic loader resolve all the SO's extern data at runtime? The some thing goes for static libraries - except the linking is done at link-time instead of run-time.

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. basic question about global variables
    By radeberger in forum C++ Programming
    Replies: 0
    Last Post: 04-06-2009, 12:54 AM
  2. About using global vars
    By Tibor in forum C Programming
    Replies: 66
    Last Post: 12-18-2008, 08:19 PM
  3. Best way to avoid using global variables
    By Canadian0469 in forum C++ Programming
    Replies: 7
    Last Post: 12-18-2008, 11:02 AM
  4. MinGW thread-safe runtime libraries
    By Mario F. in forum C++ Programming
    Replies: 3
    Last Post: 08-21-2006, 08:15 AM
  5. Global Vars
    By ihsir in forum C++ Programming
    Replies: 4
    Last Post: 04-18-2002, 10:40 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21