Thread: Why I can not "extern" a variable from header file?

  1. #16
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Yes, it seems rather strange to me. Add "const", and a variable is implicitly static, in C++. Of course it makes sense, because then the compiler can more easily do optimisations on such a variable, but I'm just used to C, I guess.

    (In C, the process that Elysia described does not work. You have to put "static" in.)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  2. #17
    Registered User
    Join Date
    Apr 2007
    Posts
    284
    Thanks all~
    BTW, what is the goodness to use static const against const ?
    I have seen my friends define static const in header files and included by cpp files (just like my example above). Why they don't just const? Any advantages?

  3. #18
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Perhaps they like C, like me?

    That's the only thing that I can think of. By using "static", you make the code compatible with C. (Assuming you don't use classes and other C++ stuff . . . .)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #19
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by meili100 View Post
    BTW, what is the goodness to use static const against const ?
    I have seen my friends define static const in header files and included by cpp files (just like my example above). Why they don't just const? Any advantages?
    None whatsoever.

    Quote Originally Posted by dwks View Post
    Perhaps they like C, like me?
    You really should give up C
    It's... deprecated today
    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.

  5. #20
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    There are currently 14 pages in the C++ programming forum here. The C one has 16. (I think this is the last 2 weeks or something, but it doesn't matter.)

    Yes, I should. We should probably all learn Scheme and write everything in it. They doesn't mean we will . . . .
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #21
    Registered User
    Join Date
    Apr 2007
    Posts
    284
    So let me summarize:

    1. Never define a variable in a header, unless you want a global constant to be used by cpp files that include this header file.

    2. If you do define a variable in a header, this makes this header only be included by one cpp file, or a linker error

    3. If you do define a variable in a header but don't include the header, you can not use extern to refer to the variable. (You can, if it's in a separated cpp file, not a header file)

  7. #22
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    1 and 2 are correct.
    3. If you do define a variable in a header but don't include the header, you can not use extern to refer to the variable. (You can, if it's in a separated cpp file, not a header file)
    That might be more clearly and generally stated as:

    You can only define a global variable once; all other references to the variable must be with "extern".

    It's basically the same thing. as #2. Two sides of the same coin
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #23
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by meili100 View Post
    So let me summarize:

    1. Never define a variable in a header, unless you want a global constant to be used by cpp files that include this header file.

    2. If you do define a variable in a header, this makes this header only be included by one cpp file, or a linker error
    Any why would you prefer to do this over putting said variable in the source file that includes the header file (and if necessary making a extern declaration in the header)?

    3. If you do define a variable in a header but don't include the header, you can not use extern to refer to the variable. (You can, if it's in a separated cpp file, not a header file)
    As above, why not define the variable in ONE source file (.c or .cpp file), then put an extern declaration in a suitable header file?

    --
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  3. Need help understanding Header Files
    By Kaidao in forum C++ Programming
    Replies: 11
    Last Post: 03-25-2008, 10:02 AM
  4. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM