Thread: static variables and includes?

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    244

    static variables and includes?

    hi!
    i need to have some static variables in one file. this file and another one have to access them. but whereever i put the static variable declaration in, the other file can not read it. not even when i include it's header file.
    i'm only including the cpp's header files. the static declarations are within the cpp file.

    thanks for reading and helping

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by MSDN
    When modifying a variable or function at file scope, the static keyword specifies that the variable or function has internal linkage (its name is not visible from outside the file in which it is declared).
    So if you have a variable declared static outside of a function, then you're telling the compiler that no other file can see it.

    So, don't declare it static. Details are a little fuzzy, but I'm guessing you need to make one of your declarations extern.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    244
    extern - what does that mean?
    in my structure, main.cpp includes engine.h and engine.h includes utility.h
    in utility.h AND in engine.h i need those static variables.
    where can i define them? (i may not define them in main.cpp)

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    static variables at file scope are not linked to variables in other translation units (read: files), so yeah, it wouldn't be the same variable.

    What are you trying to do?

    edit: define your variables in the source files, and put the extern declarations in the header files.

    http://www.cprogramming.com/tutorial/statickeyword.html
    http://faq.cprogramming.com/cgi-bin/...&id=1043284392
    Last edited by robwhit; 01-29-2008 at 12:50 PM.

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    244
    i'm trying to use those static variables in engine.cpp AND utility.cpp

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Static variables inside functions cannot be accessed outside the function, whatever you do. Global variables do not need to be static since they will retain their value until the end of the program, so specifying static to a global variable if you want a static-type global variable is wrong.

    So it comes down to the question of what you're trying to do, since the only other solution would be global variables which is to be avoided.
    Isn't it possible, for example, somehow to pass those variables as arguments to a function?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    244
    nope. it isnt.
    but maybee i have to define them before engine.cpp includes utility.h
    edit: nope, is not-good

    btw: i'm declaring those static variables in engine.cpp outside of any function and after i included utility.h

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Explain your situation! Maybe show some code of how it's used and done.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Jan 2008
    Posts
    244
    hm maybee i can post parts of the long code:

    main.cpp:
    #include "engine.h"
    engine.cpp:
    #include "utility.h"
    .....
    .....
    .....
    _____STATIC VARIABLES IN USE HERE____
    utility.cpp:
    #include "engine.h"
    static _____SOME VARIABLES HERE_____
    ......
    ......
    ......
    _____STATIC VARIABLES IN USE HERE____

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, so how are the internal linkage global variables used?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Jan 2008
    Posts
    244
    what variables? do you mean my static floats i declared in utility.h?
    they are declared there and used in both files.
    but where ever i include these variables, the other file can not access it...

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Your utility.cpp "static" variables which are actually internal linkage global variables.
    How are they used in your headers?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    >i'm trying to use those static variables in engine.cpp AND utility.cpp

    You can't use a static global variable in two files. You just can't. static has a different meaning when applied to variables in file scope, like a global variable, than to variables in a function, which have local scope.

    static in a variable in file scope (read: not in a function or a struct) is only linked in that file. If you try to use that variable in another file, you will get a undeclared identifier error.

    If you declare another variable in another file with the same name as your static variable, it will be another variable, with another memory location.

    The only way you could get a static variable in two source files to refer to the same variable is if you included one file in the other.
    Last edited by robwhit; 01-29-2008 at 01:25 PM.

  14. #14
    Registered User
    Join Date
    Jan 2008
    Posts
    244
    i included engine into utility and utility into engine.
    thats what you told me. and it doesnt work either

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Come on, work with us here! How do you use your said variables? Code, please!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. classes as member variables
    By Stonehambey in forum C++ Programming
    Replies: 13
    Last Post: 08-14-2008, 07:01 PM
  2. Includes making me insane >.<
    By IceDane in forum C Programming
    Replies: 14
    Last Post: 04-14-2008, 10:24 AM
  3. global variables
    By shadovv in forum C++ Programming
    Replies: 7
    Last Post: 10-24-2005, 02:21 PM
  4. MingW & ProperySheet
    By Unregistered in forum Windows Programming
    Replies: 4
    Last Post: 06-27-2002, 07:34 PM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM