CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: keyword extern

  1. #1
    Join Date
    Mar 2010
    Location
    Melbourne Australia
    Posts
    454

    keyword extern

    Could someone please explain the C/C++ keyword ' extern ' , and it use , if possible please post some example.

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: keyword extern

    Search the Web for C keyword extern.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: keyword extern

    the short/incomplete version:

    extern means you are telling the compiler to assume that some object of a certain type exists but that it shouldn't actually create this variable.

    Some other .cpp file will declare (and create) the variable, or the variable will be part of an external linkage (.obj or .lib) you are linking with.
    - The first case works, but is messy. It's better to define the variable in a .h and declare it (once) in one of the .cpp files.
    - the 2nd case (external linkage) is the only 'clean' reason why you would have to use it.

  4. #4
    Join Date
    Mar 2010
    Location
    Melbourne Australia
    Posts
    454

    Re: keyword extern

    thank you

  5. #5
    Join Date
    May 2012
    Posts
    57

    Re: keyword extern

    Hi Paul,

    Could you explain the difference between using "extern" on a function verses a global function?

    Say I create two files named Globals.h and Globals.cpp.
    • I code a function in Globals.cpp.
    • I place the function declaration in Globals.h.
    • I insert "#include "Globals.h" at the start of every file that needs to use that function.

    This would be a global function would it not?

    Now I prefix the function declaration with "extern" in the Globals.h file.
    What's the difference with or without the "extern" prefix?

    Thanks,
    Raptor

  6. #6
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: keyword extern

    Function names have external linkage by default, so there is no difference.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  7. #7
    Join Date
    May 2012
    Posts
    57

    Re: keyword extern

    Quote Originally Posted by laserlight View Post
    Function names have external linkage by default, so there is no difference.
    Thanks for that clarification.
    Raptor

  8. #8
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: keyword extern

    except when you meant
    extern "C"

    In that case, the extra "C" means you will get external linkage using the C convention instead of the normal C++ convention (with mangling).

  9. #9
    Join Date
    May 2012
    Posts
    57

    Re: keyword extern

    Quote Originally Posted by OReubens View Post
    except when you meant
    extern "C"

    In that case, the extra "C" means you will get external linkage using the C convention instead of the normal C++ convention (with mangling).
    Thanks for your info.
    Raptor

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured