extern const?

This is a discussion on extern const? within the C++ Programming forums, part of the General Programming Boards category; In my main function I have a const integer declared as a global variable. When I try to use extern ...

  1. #1
    Registered User
    Join Date
    Dec 2001

    extern const?

    In my main function I have a const integer declared as a global variable. When I try to use extern const int <name> in another module, the linker tells me that it is an undefined reference. It will work if I pull out the const from the declarator and the module. Is there a way to share const data in the global namespace? I know it's a bad idea to use globals, but that's not the question.
    R. D. Nichols, Jr.

    -Why do I have to go EVERYWHERE in this handbasket?

  2. #2
    Registered User
    Join Date
    Dec 2002

    const has got internal linkage

    Below mentioned is an extract from "Thinking in C++". This should help you understand the problem you are facing

    To use const instead of #define, you must be able to place const definitions inside header files as you can with #define. This way, you can place the definition for a const in a single place and distribute it to translation units by including the header file. A const in C++ defaults to internal linkage; that is, it is visible only within the file where it is defined and cannot be seen at link time by other translation units. You must always assign a value to a const when you define it, except when you make an explicit declaration using extern:

    extern const int bufsize;
    Normally, the C++ compiler avoids creating storage for a const, but instead holds the definition in its symbol table. When you use extern with const, however, you force storage to be allocated (this is also true for certain other cases, such as taking the address of a const). Storage must be allocated because extern says “use external linkage,” which means that several translation units must be able to refer to the item, which requires it to have storage.

    In the ordinary case, when extern is not part of the definition, no storage is allocated. When the const is used, it is simply folded in at compile time.

    The goal of never allocating storage for a const also fails with complicated structures. Whenever the compiler must allocate storage, constant folding is prevented (since there’s no way for the compiler to know for sure what the value of that storage is – if it could know that, it wouldn’t need to allocate the storage).

    Because the compiler cannot always avoid allocating storage for a const, const definitions must default to internal linkage, that is, linkage only within that particular translation unit. Otherwise, linker errors would occur with complicated consts because they cause storage to be allocated in multiple cpp files. The linker would then see the same definition in multiple object files, and complain. Because a const defaults to internal linkage, the linker doesn’t try to link those definitions across translation units, and there are no collisions. With built-in types, which are used in the majority of cases involving constant expressions, the compiler can always perform constant folding.


    The gist of this is that you recreate the const in every module(file) you need

    PS: I ain't sure whether its bad practice to post extracts from book in here. If yes!! please let me know. I will avoid the same in the future.
    Last edited by shiv_tech_quest; 01-17-2003 at 01:57 AM.
    Have a wonderful day.... and keep smiling... you look terrific that way
    signing off...
    shiv... as i know him

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 04:34 PM
  3. Replies: 16
    Last Post: 10-29-2006, 04:04 AM
  4. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 12:26 AM
  5. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM

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