Thread: Can someone explain "extern" to me (again)?

  1. #1
    Dragon Rider jas_atwal's Avatar
    Join Date
    Nov 2007
    Location
    India
    Posts
    54
    << split from this dead thread - http://cboard.cprogramming.com/showthread.php?t=1115 >>
    << read the rules about thread bumping! >>
    Quote Originally Posted by sh0x View Post
    ANSWER #1:
    extern is used to reassure the compiler that just because a variable is not resolved in the source file or #included headers, that it will be resolved at link time (after compile time). If I declare:

    int x;

    in main.c and I reference it in secondary.c, I must put the line:

    extern int x;

    in secondary.c so the compiler knows that this variable is originally declared in a separate source file. You cannot simply declare "int x" again in secondary.c because this would create a separate variable, not a shared one.

    So int x has to be global all the time??
    Last edited by Salem; 01-03-2008 at 01:15 AM. Reason: Split from a thread dated back from 2001

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Erm, well, I suppose you could look at it that way, but it doesn't have to be.
    It's just that unless a variable is global, another source file can't access that variable unless you pass it through a function.

    Code:
    // a.cpp
    int x = 5;
    
    // b.cpp
    extern int x;
    int main()
    {
    	x = 7;
    }
    Works.

    Code:
    // a.cpp
    void foo()
    {
    	int x = 5;
    }
    
    // b.cpp
    extern int x;
    int main()
    {
    	x = 7;
    }
    Does not work since x is local in the function (you'll get a linking error here).

    Although, in C++, it could also be a static class variable, though I'm not sure if declaring it extern works. You should include the class header instead.
    Variables can also be placed inside namespaces, in which it's still global, only inside a namespace.
    Last edited by Elysia; 01-02-2008 at 04:43 PM.
    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.

  3. #3
    Dragon Rider jas_atwal's Avatar
    Join Date
    Nov 2007
    Location
    India
    Posts
    54
    Quote Originally Posted by Elysia View Post
    Erm, well, I suppose you could look at it that way, but it doesn't have to be.
    It's just that unless a variable is global, another source file can't access that variable unless you pass it through a function.

    Code:
    // a.cpp
    int x = 5;
    
    // b.cpp
    extern int x;
    int main()
    {
    	x = 7;
    }
    Works.

    Code:
    // a.cpp
    void foo()
    {
    	int x = 5;
    }
    
    // b.cpp
    extern int x;
    int main()
    {
    	x = 7;
    }
    Does not work since x is local in the function (you'll get a linking error here).
    Gotya!! THank you!!

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    For completeness, the other use of extern is to declare and use functions and objects from other languages - usually c. The following declares 2 C functions for use in a c++ program:
    Code:
    extern "C" void func1();
    extern "C" {
    void func2();
    }
    Extern "C++" is also valid, though redundant.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Not really.
    Extern can be used to export classes, data and function from a DLL. However, it is perfectly valid to use just extern or extern "C++" if the DLL is going to be used from C++; otherwise extern "C" is probably used to avoid name mangling.
    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.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Not really.
    If that is in reference to the redundant comment, then yes, really. Plain 'extern' and 'extern "C++"' have exactly the same meaning unless embedded in an extern "C" block.

    Also, extern cannot be used for exporting classes, data or functions from a DLL. Point 1 is that C++ has no concept of DLLs. Point 2 is that it simply doesn't work that way even in the real world.

    *nix shared objects export every symbol with external linkage by default. There are linker commands to change that, but that's not the issue here. Since external linkage is the default for global variables and functions, they all get exported.

    Windows DLLs export nothing by default IIRC. You can get the linker to export things either by using a .def file, or by using the MS __declspec(dllexport) extension. Either will work. The former is more compatible with other languages, the latter more efficient. You can also combine the two.

    Classes don't exist on the binary level, so they're not exported. Their member functions and static member variables can be exported. It's up to the compiler to correctly generate linking information based on the class definitions in the header files.

    The extern keyword will get another new meaning in C++09, to do with template specializations.


    Oh, and C still does name mangling. Just a considerably simpler method. To avoid mangled names, give the exported symbols other names in the .def file.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by CornedBee View Post
    Classes don't exist on the binary level, so they're not exported. Their member functions and static member variables can be exported. It's up to the compiler to correctly generate linking information based on the class definitions in the header files.
    In a higher meaning, it's possible. With some compiler magic, it is definitely possible to export and create classes from a DLL. All it does is export all functions and members variables (AFAIK) and if you include the header, the compiler will generate correct code, since it's not the compiler's job to do the correct jump--that's the linker's job. And it doesn't matter if it's local or in a DLL, since it will be in the virtual memory of the process, and thus, the linker can generate the correct call instruction to call the functions of the class's inside the DLL.
    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.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    And this is different from what I said, how?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It was not clear in your original reply that you could export classes. You mentioned it didn't work, and while technically true, it's possible to do it alright.
    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. Can you explain these bitwise operations?
    By 6tr6tr in forum C++ Programming
    Replies: 6
    Last Post: 10-29-2008, 01:19 PM
  2. One Easy" C " Question. Please Solve and Explain.
    By RahulDhanpat in forum C Programming
    Replies: 18
    Last Post: 03-24-2008, 01:39 PM
  3. Please Explain me few terms that i have listed in here.
    By chottachatri in forum C++ Programming
    Replies: 3
    Last Post: 02-26-2008, 08:20 AM
  4. Pls explain how this program works...
    By Unregistered in forum C Programming
    Replies: 9
    Last Post: 01-05-2002, 09:53 AM
  5. Can someone explain "extern" to me?
    By valar_king in forum C++ Programming
    Replies: 3
    Last Post: 09-16-2001, 12:22 AM