Thread: variable declaration in library

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    4

    variable declaration in library

    Hi all,

    I assume that the variables meant to be used within a library should be declared static so as to restrict there scope and prevent their access in the programs using the library.

    Now if my library is being compiled from two c files and am using a global variable in both of them, is there a way to do this without declaring the variable extern? i.e I would like to use the variable in two c files and still have the its scope restricted to the library only. Is there a standard way to do this?


    Thank you

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Use static functions to access the variable, and don't access it directly. That's pretty much the only way you're going to be able to it without using extern. Hide a static variable inside a function. Use other functions in your library to set or get it.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    4
    but how does that solve the problem? I still would not be able to access the same static function from other file....
    could you elaborate your point further, if I am wrong?

  4. #4
    Registered User
    Join Date
    Oct 2009
    Posts
    10
    could you use a header file that holds static getter/setter functions for the variable(s) you need, and include it in the two c files?

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    /* publicheader.h */
    void set( type t );
    type get( void );
    Make a header to access your library variables. Include it wherever you want to be able to use your library.
    Code:
    /* library.c */
    static type getset( type t, int s )
    {
        static type x;
        if( s )
            x = t;
    
        return x;
    }
    
    void set( type t )
    {
        getset( t, 1 );
    }
    
    type get( void )
    {
        return getset( 0, 0 );
    }
    There you go.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Nov 2009
    Posts
    4
    thank you guys....
    i think i got the idea

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Little bit of variable declaration syntax
    By TheNexumMan in forum C Programming
    Replies: 4
    Last Post: 11-25-2008, 04:35 AM
  2. Need some help...
    By darkconvoy in forum C Programming
    Replies: 32
    Last Post: 04-29-2008, 03:33 PM
  3. global variable at dynamic library
    By Mortissus in forum C++ Programming
    Replies: 2
    Last Post: 07-31-2007, 02:44 PM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM